gitlab-ci.yml配置gitla pipline要达到持续集成的方法

官网参考地址
从版本7.12开始,gitlab CI使用yaml(.gitlab-ci.yml)文件进行项目配置,该文件位于代码仓库的根目录中,包含有关构建项目的pipline,以及pipline需要完成哪些工作。

pipline

一次pipline相当于一次构建任务,里面包含多个流程,如build、test、deploy测试服务器、部署生产服务器等流程,任何成员的commit push到gitlab上时都可以触发pipline.

接下来举个小例子:

 stages:
  - build
  - test
job1:
  stage: test
  script:
  - sh test.sh
  artifacts:
    paths:
    - data_test/
job2:
  stage: build
  script:
  - echo 'suceed test' >> txt1

上面的代码块定义了两个阶段(stages),一个是build,一个是test,按照书写的顺序进行,也就是先执行build阶段,再进行test阶段,上面的代码块有两个作业(job),job1和job2,job1是test阶段,job2是build阶段,所以先执行job2再执行job1,关键字script是必须要执行的命令。

下面是在gitlab -runner中的运行结果:

build:

Running with gitlab-ci-multi-runner 1.11.2 (0489844)
  on mac-runner (e7d87f13)
Using Shell executor...
Running on shuchaodeMacBook-Pro.local...
Fetching changes...
Removing data_test/190102_NS500203_0435_AHKHKWBGX9.Zscore.xls
Removing data_test/190102_NS500203_0435_AHKHKWBGX9.sum.txt.cv.csv
Removing data_test/190102_NS500203_0435_AHKHKWBGX9_mapChr.xls
HEAD is now at 7387b3c Delete 190102_NS500203_0435_AHKHKWBGX9.sum.txt.cv.csv
From http://192.168.4.168:10080/shujingchao/my_test
   7387b3c..07e1d96  master     -> origin/master
Checking out 07e1d961 as master...
Skipping Git submodules setup
$ echo 'suceed test' >> txt1
Job succeeded

test:

Running with gitlab-ci-multi-runner 1.11.2 (0489844)
  on mac-runner (e7d87f13)
Using Shell executor...
Running on shuchaodeMacBook-Pro.local...
Fetching changes...
HEAD is now at 07e1d96 Delete 190102_NS500203_0435_AHKHKWBGX9.Zscore.xls
Checking out 07e1d961 as master...
Skipping Git submodules setup
$ sh test.sh
Uploading artifacts...
data_test/: found 6 matching files                 
Uploading artifacts to coordinator... ok            id=2841 responseStatus=201 Created token=V9Z53_t3
Job succeeded

关键字参数

该文件是通过一系列的关键字参数来定义pipline的每一个任务的执行方式,下面是各项参数的函数以及用法:
jobs:
YAML文件定义了一组具有约束的作业,说明应该何时运行它们。可以指定无限数量的作业,这些作业被定义为任意名称的抬头,但是始终必须至少应包含script子句。

job1:
  script: "execute-script-for-job1"

job2:
  script: "execute-script-for-job2"

每个job都是独立进行的,每个job都有自己的阶段(stage),相同阶段的job会并行运行,job必须有自己独立的名字,但不能与关键字来定义:
image
services
stages
types
before_script
after_script
variables
cache

job内定义作业流程的参数列表

KeywordRequiredKeyword
scriptyes定义在runner中执行的命令
extendsnoDefines a configuration entry that this job is going to inherit from
includenoDefines a configuration entry that allows this job to include external YAML files
imagenoUse docker image, covered in Using Docker Images
servicesnoUse docker services, covered in Using Docker Images
stageno定义job属于哪个阶段,默认test阶段
typenostage别名(一般不会用)
variablesno定义job层次的变量
onlyno定义哪些分支或tag的修改触发该流程
exceptno定义哪些分支或tag的修改不触发该流程
tagsno定义哪个标签的runner来执行,该标签指runner配置时的名称,不是Git的tag分支
allow_failurenoAllow job to fail. Failed job doesn’t contribute to commit status
whennoDefine when to run job. Can be on_success, on_failure, always or manual
dependenciesno定义该job依赖于哪项job的结果,用于把之前job的附件传进来
artifactsno定义job产生的附件,可用于下载和保存以及传递,没有该项设置产生的过程文件都会被删除
cacheno定义缓存的文件或文件夹,如果是在job外定义则为全局变量
before_scriptno定义job执行前的操作
after_scriptno定义job执行后的操作
environmentno定义此作业将部署到的环境的名称
coveragenoDefine code coverage settings for a given job
retryno定义任务失败后的重复执行次数或时间
parallelno定义并行的任务数量,限于2~50
triggerno定义下载管道触发器

before_script and after_script

before_script and after_script定义作业前后的操作,可以定义全局作业前后的操作,也可以是job内部前后的操作。

before_script:
  - global before script

job:
  before_script:
    - execute this instead of global before script
  script:
    - my command
  after_script:
    - execute this after my script

stages

定义pipline的全部阶段(stage),阶段内所有的任务并行执行,全部执行成功再开始执行下一阶段的的任务,任何阶段的失败都会导致pipline的失败,所有stage,job执行成功后pipline会显示pass,如果未定义stages,会定义有build、test、deploy三个阶段,如果未定义stage,会默认是test阶段。

stages:
  - build
  - test
  - deploy
 

stage

定义job属于那个stage,默认为test阶段

stages:
  - build
  - test
  - deploy

job 1:
  stage: build
  script: make build dependencies

job 2:
  stage: build
  script: make build artifacts

job 3:
  stage: test
  script: make test

job 4:
  stage: deploy
  script: make deploy

script

job内唯一一项必须的关键字设置,配置runner执行的shell命令,可单行也可多行。

job1:
  script: "bundle exec rspec"
job2:
  script:
    - uname -a
    - bundle exec rspec

only and except

only 定义在那个分支或tag上的修改出发执行。
except定义哪个分支或tag的修改不触发任务的执行
允许使用正则表达式
允许制定的一些关键字:

ValueDescription
branchesWhen a git reference of a pipeline is a branch.
tagsWhen a git reference of a pipeline is a tag.
apiWhen pipeline has been triggered by a second pipelines API (not triggers API).
externalWhen using CI services other than GitLab.
pipelinesFor multi-project triggers, created using the API with CI_JOB_TOKEN.
pushesPipeline is triggered by a git push by the user.
schedulesFor scheduled pipelines.
triggersFor pipelines created using a trigger token.
webFor pipelines created using Run pipeline button in GitLab UI (under your project’s Pipelines).
merge_requestsWhen a merge request is created or updated (See pipelines for merge requests).

下面这个例子只执行issue-开头的refs,所有branch跳过

 job:
  # use regexp
  only:
    - /^issue-.*$/
  # use special keyword
  except:
    - branches

下面的例子执行gitlab-org/gitlab-ce上的所有分支,除了master

job:
  only:
    - branches@gitlab-org/gitlab-ce
  except:
    - master@gitlab-org/gitlab-ce

如果任务没有指定only参数,则默认[‘branches’,‘tags’],未指定except时,则默认为空。

tags

tags允许指定的runner来执行任务
下面的例子有指定的ruby和postgres来执行。

job:
  tags:
    - ruby
    - postgres

也可为不同的job指定不同的runner,例如windows和mac上的任务有不同的runner执行。

windows job:
  stage:
    - build
  tags:
    - windows
  script:
    - echo Hello, %USERNAME%!

osx job:
  stage:
    - build
  tags:
    - mac
  script:
    - echo "Hello, $USER!”

allow_failure

allow_failure关键字允许该项job失败后不影响其他任务的继续执行,如果其他任务全部执行成功也会显示pipeline pass,只是该任务提示橙色警告标示,该设置默认false不启动;下面的例子job1失败后不会影响后续任务job2/job3的执行

job1:
  stage: test
  script:
    - execute_script_that_will_fail
  allow_failure: true

job2:
  stage: test
  script:
    - execute_script_that_will_succeed

job3:
  stage: deploy
  script:
    - deploy_to_staging

when

when用于当前一阶段任务故障或者忽略故障的job任务,主要用于清理发生故障时的后续措施。
可选项:

  1. on_success - 默认参数,前一阶段任务成功时才执行
  2. on_failure - 前一阶段任务故障时才执行
  3. always - 无论前一阶段任务是否成功都执行
  4. manual - 手动设置,比较复杂,略过。
stages:
  - build
  - cleanup_build
  - test
  - deploy
  - cleanup

build_job:
  stage: build
  script:
    - make build

cleanup_build_job:
  stage: cleanup_build
  script:
    - cleanup build when failed
  when: on_failure

test_job:
  stage: test
  script:
    - make test

deploy_job:
  stage: deploy
  script:
    - make deploy
  when: manual

cleanup_job:
  stage: cleanup
  script:
    - cleanup after jobs
  when: always

上面的例子当build任务失败时才会执行cleanup_build任务;无论之前的任务是否成功,最后都会执行clean_up的任务;当执行到deploy任务时,会触发手动设置。

artifacts

artifacts指任务成功后可供下载的附件,可在pipeline-CI中下载

  1. artifacts:paths - 该路径下的文件作为附件,只可指定仓库内的路径
  2. artifacts:name - 下载附件时的名称,可利用内置变量作为附件的名字
  3. artifacts:when - 指定当job成功或失败时才会上传附件
  4. artifacts:expire_in - 指定附件上载后保存的时间,默认永久在Gitlab保存
  5. artifacts:untracked - 指定上传Git未跟踪的文件
job:
  artifacts:
    name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
    paths:
      - binaries/
    when: on_failure
    expire_in: 1 week
    untracked: true

上面的例子会在任务失败时上载binaries/路径下未被跟踪的文件,附件以job-ref为名字,保留一周的时间。

parallel

同时运行的job数

test:
  script: rspec
  parallel: 5

include

把其他文件中的内容包含进来,类似于Makefile中的include

include:
  - project: 'my-group/my-project'
    ref: master
    file: '/templates/.gitlab-ci-template.yml'

  - project: 'my-group/my-project'
    ref: v1.0.0
    file: '/templates/.gitlab-ci-template.yml'

  - project: 'my-group/my-project'
    ref: 787123b47f14b552955ca2786bc9542ae66fee5b # Git SHA
    file: '/templates/.gitlab-ci-template.yml'
include:
  - remote: 'https://gitlab.com/awesome-project/raw/master/.gitlab-ci-template.yml'

variables

用于定义全局或私有变量,同时内置了一些内置变量,如CI_COMMIT_REF_NAME表示构建任务的分支或tag,点此查看更多内置变量

variables:
  DATABASE_URL: "postgres://postgres@postgres/my_database"

Special YAML features

&定义调用的方式,.代表注释掉该job, aliases (*) 、map merging (<<),用于调用重复的内容

.deploy: &deploy_template
  stage: deploy
  tags:
    - bambnitest
  only:
    - tags
    - triggers
    - /^ci-deploy/
  allow_failure: true
  before_script:
    - ls -alh "bin"
  script:
    - rsync -luvr bin $HOST_IP:/work
job128:
  <<: *deploy_template
  variables:
    HOST_IP: "*****.128.2"
job129:
  <<: *deploy_template
  variables:
    HOST_IP: "******.129.2"

Skipping jobs

如果提交信息有[ci skip] 或者[skip ci]会执行提交,但是会跳过pipeline

git push -o ci.skip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值