Gitlab-CI/CD部署集成

281 阅读6分钟

Gitlab 配置CI/CD

image.png

image.png

  • 提交修改之后会有提示

image.png

安装Gitlab Runner

Gitlab只是个代码仓库,想要实现CI/CD还需安装gitlab-runnergitlab-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

交互式注册GitLab Runner

image.png

[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 ~]#

注册成功后,查看界面中已经激活的运行器:

image.png

温情提示:新建的runner下图勾选默认关闭 打开后.gitlab-ci.yml文件中的作业不需要加tag标签绑定

image.png

GitLab CI流水线配置文件.gitlab-ci.yml详解

GitLab CI介绍

  • GitLab提交持续集成服务,当你在项目根目录中添加 .gitlab-ci.yml文件,并配置项目的运行器( GitLab Runner),那么后续的每次提交都会触发CI流水线(pipeline)的执行。
  • .gitlab-ci.yml文件告诉运行器需要做哪些事情,默认情况下,流水线有build 、test 、deploy三个阶段,即构建测试部署,未被使用的阶段将会被自动忽略。
  • 如果一切运行正常(没有非零返回值),您将获得与提交相关联的漂亮绿色复选标记(如下图所示)。这样可以在查看代码之前轻松查看提交是否导致任何测试失败。

gitlab_cicd_green_checkmark.png

.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 、testdeploy三个阶段,即 构建测试部署 。
  • 如果一个作业未定义 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

onlyexcept

onlyexcept用于在创建作业时对作业的限制策略。

  • only定义了哪些分支或标签(branches and tags)的作业会运行
  • except定义了哪些分支或标签(branches and tags)的作业不会运行

下面是策略规则:

  • onlyexcept可同时使用,如果在一个作业中同时定义了onlyexcept,则onlyexcept同时进行过滤(注意,不是忽略 except条件) 。
  • onlyexcept可以使用正则表达式。
  • onlyexcept允许指定用于过滤forks作业的存储库路径。
  • onlyexcept中可以使用特殊的关键字,如branchestagsapiexternalpipelinespushesschedulestriggerswebmerge_requestschats等。

onlyexcept中可以使用特殊的关键字:

关键字描述释义
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