淘先锋技术网

首页 1 2 3 4 5 6 7

一、Gitlab-Runner的安装

1、Linux直接安装Gitlab Runner

curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
chmod +x /usr/local/bin/gitlab-runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

报错–> useradd:无法打开 /etc/shadow

# 查看权限
lsattr /etc/passwd
# 去除i权限 i表示只读
chattr -i /etc/passwd
gitlab-runner register
1.解决gitlab-runner执行docker命令提示权限不足的问题
将gitlab-runner用户添加到docker组
sudo usermod -aG docker gitlab-runner
验证是否生效:
sudo -u gitlab-runner -H docker info
2.给gitlab-runner sudo权限
sudo vi /etc/sudoers
在root ALL=(ALL:ALL) ALL下增加gitlab-runner ALL=(ALL) NOPASSWD: ALL

2、Docker容器方式搭建Gitlab-Runner

【链接】Docker容器版的Gitlab-Runnner官方文档
按照以下方法搭建三个Gitlab-Runner
· build 用于项目编译,创建Docker镜像
· deploy 用于部署
· push-tags 部署成功,测试通过,则可以手动将镜像推送到镜像仓库,作为一个版本保存

(1) 安装注册Gitlab-Runner

# 启动
docker run -d --name gitlab-runner --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:latest
# 注册
docker exec -it gitlab-runner gitlab-runner register

注册需要设置的runner参数

$ docker exec -it gitlab-runner gitlab-runner register
Runtime platform arch=amd64 os=linux pid=21 revision=8925d9a0 version=14.1.0
Running in system-mode.                            
                                                  
Enter the GitLab instance URL (for example, https://gitlab.com/):
# 填写下图中的URL
Enter the registration token:
# 填写下图中的toekn
Enter a description for the runner:
[fbfd0390404c]: description # runner的备注或描述信息
Enter tags for the runner (comma-separated):
# Runner的标签,对应.gitlab-ci.yml设置的tag
Registering runner... succeeded                     runner=-zPT6d-G
Enter an executor: docker-ssh+machine, kubernetes, docker, docker-ssh, ssh, docker+machine, custom, parallels, shell, virtualbox:
docker #选择docker
Enter the default Docker image (for example, ruby:2.6):
docker:latest # 选择使用的Docker镜像
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

在这里插入图片描述

(2) 配置config.toml

重启生效

cd /srv/gitlab-runner/config
vi config.toml

在这里插入图片描述

#修改
privileged = true
volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock", "/builds/:/builds/"]
# 添加
pull_policy = ["if-not-present"]

(3)查看Runner注册情况

在这里插入图片描述

二、测试部署

准备:
- maven-jdk8:v1.0 镜像,用于编译打包项目
- Dockerfile
- .gitlab-ci.yml 存放与项目根目录
- 远程代码仓库,此处使用的是阿里云镜像仓库

项目结构:
在这里插入图片描述

准备 .gitlab-ci.yml

stages:
  - build
  - deploy
  - produce
build-smc:
  stage: build
  script:
    - echo "$PWD"
    - echo "$HOME"
    - echo "$CI_COMMIT_REF_NAME"
    - docker run --rm
      -v "$PWD":/app
      -v "$HOME/.m2":/root/.m2
      -w /app maven-jdk8:v1.0
      mvn clean install
    - cd $PWD/alibaba-basic-parent/alibaba-middle-ware
    - ls -l
    - docker build -t middle-server:$CI_COMMIT_REF_NAME .
  tags: # 与gitlab-runner注册设置的tag一致,表示所使用的Runner
    - build3
  only:
    - dev
    - tags

deploy_dev:
  stage: deploy
  script:
    - echo "$PWD"
    - docker images
    - ls -l
    - docker run -dit --name middle-server -p 10000:10000 middle-server:$CI_COMMIT_REF_NAME
  tags:
    - deploy
  only:
    - dev
    - tags
push_tags_harbor:
  stage: produce
  script:
    - docker ps
    - docker login --username=xxx --password=xxx registry.cn-chengdu.aliyuncs.com
    - docker tag middle-server:$CI_COMMIT_REF_NAME registry.cn-chengdu.aliyuncs.com/multi-config/base-repository:middle-server-v1.0
    - docker push registry.cn-chengdu.aliyuncs.com/multi-config/base-repository:middle-server-v1.0
  when: manual
  tags:
    - push-tags
  only:
    - tags

之后,只要向Gitlab提交新的代码,三个Gitlab-Runner便会自动触发,根据.gitlab-ci.yaml的配置执行相关命令,完成项目的打包、编译、部署等工作

待解决问题:

gitlab-runner运行docker-compose命令,提示没有docker-compose命令,因此没有使用docker-compose进行部署