GitLab CI/CD流水线配置
本教程指导基于GitLab配置CI/CD流水线。
安装gitlab-runner
如果GitLab所在服务器没有安装gitlab-runner,则执行以下命令安装。
安装gitlab-runner
# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner
# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
相关问题
ERROR: Job failed: prepare environment: exit status 1.
注释掉/home/gitlab-runner/.bash_logout
中的这段:
if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
给 gitlab-runner root权限
sudo usermod -aG docker gitlab-runner
执行 sudo systemctl restart docker 重启守护进程。
编写流水线配置文件
在项目的根目录下,创建.gitlab-ci.yml
文件,用于配置流水线。范例如下:
stages: # 定义阶段,就是流水线执行的顺序
- mvn-test
- mvn-package-docker-push
variables:
# 将重用的变量设置为常量
DOCKER_IMAGE_NAME_ENGINE: engine:1.0.0
DOCKER_IMAGE_NAME_ENGINE_SCHEDULER: engine-scheduler:1.0.0
DOCKER_IMAGE_NAME_ENGINE_SCHEDULER_UI: engine-scheduler-ui:1.0.0
DOCKER_IMAGE_PUSH_NAME_ENGINE: 192.168.1.76:8083/engine:1.0.0
DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER: 192.168.1.76:8083/engine-scheduler:1.0.0
DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER_UI: 192.168.1.76:8083/engine-scheduler-ui:1.0.0
mvn-test:
stage: mvn-test # 按事先定义好的阶段来指定
tags: # 按产品名称来打tag
- engine
script:
- echo "Start mvn-test... "
- mvn test
- echo "End mvn-test."
mvn-package-docker-push:
stage: mvn-package-docker-push # 按事先定义好的阶段来指定
tags: # 按产品名称来打tag
- engine
when: manual # 设置为手动发布
script: # 编写脚本,本质是把你本地的工作搬运过来
- echo "Start mvn-package... "
- mvn clean package
- echo "End mvn-package. "
- echo "Start docker-build... "
- cd engine/engine-app
- docker build -t $DOCKER_IMAGE_NAME_ENGINE .
- cd ../../scheduler/scheduler-server
- docker build -t $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER .
- - cd ../../scheduler/scheduler-ui
- docker build -t $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER_UI .
- echo "End docker-build. "
- echo "Start docker-push-sit... "
- docker tag $DOCKER_IMAGE_NAME_ENGINE $DOCKER_IMAGE_PUSH_NAME_ENGINE
- docker tag $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER
- docker tag $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER_UI $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER_UI
- docker login 192.168.1.76:8083 -u nexus_app -p lianyu_123
- docker push $DOCKER_IMAGE_PUSH_NAME_ENGINE
- docker push $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER
- docker push $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER_UI
- echo "End docker-push-sit. "
上面是一个相对来说比较复查的流水线,定义了2个后端产品和1个前端产品的构建、发布过程。实现的产品可以按上述范例剪裁。
- stages: 自定义定义阶段,就是流水线执行的顺序按照这个定义好的阶段运行。一般的项目,无外乎就是测试、编译、打包、发包这么几个阶段。上述案例,将编译、打包、发包合并为了一个阶段。
- variables:将重用的变量设置为常量。该配置是可选的。
- stage:当前执行的阶段。
- tags: 按产品名称来打tag。该tag是与要后台配置好的执行流水线的Runner密切相关的,不要乱填。可选:engine、integration、control-panel、planner-workbench。
when: manual
:设置为手动发布。有些阶段不是必须执行的,比如发布动作,因此设置为manual。- script:定义好自动化执行的动作,本质是把你本地的工作搬运过来交给Runner自动执行。如果是需要客户端VPN访问的网络,则不能写在该脚本里面,因为服务器上没有VPN!
创建Runner
Runner是流水线的实际执行者。
在“CI/CD Settings -> Runners”页面,点击“New project runner”,来创建Runner。主要配置项:
- Tags:给Runner命名,按产品来划分
- Runner description:同上
- Maximum job timeout:设计超时时间,最小600秒
注册Runner
创建Runner完成之后,需要拷贝注册信息到GitLab所在服务器上执行注册。
$ sudo gitlab-runner register --url http://192.168.1.74 --token glrt-WfmzN-7izBW81ZF_x7Xw
Runtime platform arch=amd64 os=linux pid=2085600 revision=81ab07f6 version=16.10.0
Running in system-mode.
There might be a problem with your config based on jsonschema annotations in common/config.go (experimental feature):
jsonschema: '/runners/0/Monitoring' does not validate with https://gitlab.com/gitlab-org/gitlab-runner/common/config#/$ref/properties/runners/items/$ref/properties/Monitoring/$ref/type: expected object, but got null
Enter the GitLab instance URL (for example, https://gitlab.com/):
[http://192.168.1.74]:
Verifying runner... is valid runner=WfmzN-7iz
Enter a name for the runner. This is stored only in the local config.toml file:
[ubuntu-gitlab]: integration
Enter an executor: kubernetes, docker-autoscaler, instance, custom, ssh, docker, docker-windows, docker+machine, shell, parallels, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
上述注册Runner的一个前提是GitLab所在服务器上已经安装了gitlab-runner。
查看流水线执行情况
在合并请求查看流水线状态。变绿代码执行成功。
点击流水线编号,可以看到详细的各阶段运行情况。
点击阶段,可以看到详细的阶段运行日志。