Gitlab 配置CI/CD
- 提交修改之后会有提示
安装Gitlab Runner
Gitlab只是个代码仓库,想要实现CI/CD还需安装
gitlab-runner,gitlab-runner相当于Gitlab中任务的执行器,Gitlab会在需要执行任务时调用它。
GitLab Runner可以通过二进制文件安装、Docker镜像安装、包仓库安装。
- 推荐docker镜像下载
gitlab-runner的Docker镜像
docker pull gitlab/gitlab-runner:latest
或者
docker run -d --name gitlab-runner --restart always -v /home/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
- 或者从官方直接下载 ps:注意与gitlab版本保持大致相同 可在下方或者相应官网查看版本下载hub.docker.com/r/gitlab/gi…
- 清华镜像 mirrors.tuna.tsinghua.edu.cn/gitlab-runn…
交互式注册GitLab Runner
[root@server ~]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=25074 revision=3001a600 version=11.10.0
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.10.246:8080/
Please enter the gitlab-ci token for this runner:
YwM3h1uK9Ngx7cK9kzvi
Please enter the gitlab-ci description for this runner:
[server.hopewait]: test
Please enter the gitlab-ci tags for this runner (comma separated):
test
Registering runner... succeeded runner=dwy_hDMp
Please enter the executor: docker, docker-ssh+machine, kubernetes, docker-ssh, parallels, shell, ssh, virtualbox, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@server ~]#
注册成功后,查看界面中已经激活的运行器:
温情提示:新建的runner下图勾选默认关闭 打开后.gitlab-ci.yml文件中的作业不需要加tag标签绑定
GitLab CI流水线配置文件.gitlab-ci.yml详解
GitLab CI介绍
- GitLab提交持续集成服务,当你在项目根目录中添加
.gitlab-ci.yml文件,并配置项目的运行器(GitLab Runner),那么后续的每次提交都会触发CI流水线(pipeline)的执行。 .gitlab-ci.yml文件告诉运行器需要做哪些事情,默认情况下,流水线有build、test、deploy三个阶段,即构建、测试、部署,未被使用的阶段将会被自动忽略。- 如果一切运行正常(没有非零返回值),您将获得与提交相关联的漂亮绿色复选标记(如下图所示)。这样可以在查看代码之前轻松查看提交是否导致任何测试失败。
.gitlab-ci.yml配置参数
| 关键字 | 描述 |
|---|---|
| script | 必须参数,运行器需要执行的脚本 |
| image | 使用Docker image镜像 |
| services | 使用Docker services镜像 |
| before_script | 作业执行前需要执行的命令 |
| after_script | 作业执行后需要执行的命令 |
| stages | 定义流水线所有的阶段 |
| stage | 定义作业所处流水线的阶段(默认test阶段) |
| only | 限制作业在什么时候创建 |
| except | 限制作业在什么时候不创建 |
| tags | 作用使用的Runner运行器的标签列表 |
| allow_failure | 允许作业失败,失败的作业不影响提交的状态 |
| when | 什么时候运行作业 |
| environment | 作用部署的环境名称 |
| cache | 指定需要在job之间缓存的文件或目录 |
| artifacts | 归档文件列表,指定成功后应附加到job的文件和目录的列表 |
| dependencies | 当前作业依赖的其他作业,你可以使用依赖作业的归档文件 |
| coverage | 作业的代码覆盖率 |
| retry | 作业失败时,可以自动执行多少次 |
| parallel | 指定并行运行的作业实例 |
| trigger | 定义下游流水线的触发器 |
| include | 作业加载其他YAML文件 |
| extends | 控制实体从哪里继承 |
| pages | 上传GitLab Pages的结果 |
| retry | 作业失败时,可以自动执行多少次 |
| variables | 定义环境变量 |
stages
stages定义流水线全局可使用的阶段,阶段允许有灵活的多级管道,阶段元素的排序定义了作业执行的顺序。
- 相同
stage阶段的作业并行运行。 - 默认情况下,上一阶段的作业全部运行成功后才执行下一阶段的作业。
- 默认有三个阶段,
build、test、deploy三个阶段,即构建、测试、部署。 - 如果一个作业未定义
stage阶段,则作业使用test测试阶段。 - 默认情况下,任何一个前置的作业失败了,commit提交会标记为
failed并且下一个stages的作业都不会执行。
stage
stage定义流水线中每个作业所处的阶段,处于相同阶段的作业并行执行。
示例:
yaml
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
stages:
- build
- code_check
- test
- deploy
build1:
stage: build
before_script:
- echo "Before script in build stage that overwrited the globally defined before_script"
- echo "Install cloc:A tool to count lines of code in various languages from a given
after_script:
- echo "After script in build stage that overwrited the globally defined after_script"
- cloc --version
- cloc .
script:
- echo "Do your build here"
- xxx.sh
tags:
- test
script
script是作业中唯一必须的关键字参数,是运行器需要执行的脚本,如:
build1:
script:
- echo "Do your build here"
- uname -a
only和except
only和except用于在创建作业时对作业的限制策略。
only定义了哪些分支或标签(branches and tags)的作业会运行except定义了哪些分支或标签(branches and tags)的作业不会运行
下面是策略规则:
only和except可同时使用,如果在一个作业中同时定义了only和except,则only和except同时进行过滤(注意,不是忽略except条件) 。only和except可以使用正则表达式。only和except允许指定用于过滤forks作业的存储库路径。only和except中可以使用特殊的关键字,如branches、tags、api、external、pipelines、pushes、schedules、triggers、web、merge_requests、chats等。
only和except中可以使用特殊的关键字:
| 关键字 | 描述释义 |
|---|---|
| branches | 当一个分支被push上来 |
| tags | 当一个打了tag标记的Release被提交时 |
| api | 当一个pipline被第二个piplines api所触发调起(不是触发器API) |
| external | 当使用了GitLab以外的外部CI服务,如Jenkins |
| pipelines | 针对多项目触发器而言,当使用CI_JOB_TOKEN, |
| 并使用gitlab所提供的api创建多个pipelines的时候 | |
| pushes | 当pipeline被用户的git push操作所触发的时候 |
| schedules | 针对预定好的pipline计划而言(每日构建一类) |
| triggers | 用触发器token创建piplines的时候 |
| web | 在GitLab WEB页面上Pipelines标签页下,按下run pipline的时候 |
| merge_requests | 当合并请求创建或更新的时候 |
| chats | 当使用GitLab ChatOps 创建作业的时候 |
在下面这个例子中,job将只会运行以issue-开始的refs(分支),然而except中指定分支不能执行,所以这个job将不会执行:
job:
# use regexp
only:
- /^issue-.*$/
# use special keyword
except:
- branches