本文为https://gitlab.starcart.cn/help/ci/yaml/README.md的翻译。
在每个项目中使用一个叫.gitlab-ci.yml的YAML文件来配置GitLab CI/CD pipeline。该.gitlab-ci.yml文件定义了pipeline的结构和顺序,并确定:
本主题涵盖CI/CD pipeline配置。有关其他CI/CD配置信息,请参阅:
GitLab CI / CD变量,用于配置运行pipeline的环境。
GitLab Runner高级配置,用于配置GitLab Runner。
我们有配置pipelines的完整示例:
有关GitLab CI的快速介绍,请遵循我们的快速入门指南。
有关示例集合,请参见GitLab CI / CD示例添加链接描述。
要查看.gitlab-ci.yml企业中使用的大文件,请参阅的.gitlab-ci.yml文件gitlab-ce。
pipeline配置从作业(jobs)开始。作业是.gitlab-ci.yml文件的最基本元素。
作业(jobs)是:
job1:
script: "execute-script-for-job1"
job2:
script: "execute-script-for-job2"
上面的示例是具有两个单独作业的最简单的CI/CD配置,其中每个作业执行一个不同的命令。当然,命令可以在仓库中直接执行代码(./configure;make;make install)或运行脚本(test.sh)。
作业由Runners拾取并在Runner的环境中执行。重要的是,每个作业都是彼此独立运行的。
每个GitLab CI实例都有一个称为Lint的嵌入式调试工具,该工具可以检验.gitlab-ci.yml文件的内容。您可以在ci/lint项目名称空间页面下找到Lint 。例如,https://gitlab.example.com/gitlab-org/project-123/-/ci/lint。
每个作业必须具有唯一的名称,但是有一些保留的关键字不能用作作业名称:
image
services
stages
types
before_script
after_script
variables
cache
如果使用特定值(例如true或false)时出现检验错误,请尝试执行以下操作:
作业定义为一系列的定义作业行为的参数列表。
下表列出了作业的可用参数:
Keyword | Description |
---|---|
script | Shell script which is executed by Runner. |
image | Use docker images. Also available: image:name and image:entrypoint. |
services | Use docker services images. Also available: services:name, services:alias, services:entrypoint, and services:command. |
services | Use docker services images. Also available: services:name, services:alias, services:entrypoint, and services:command. |
before_script | Override a set of commands that are executed before job. |
after_script | Override a set of commands that are executed after job. |
stages | Define stages in a pipeline. |
stage | Defines a job stage (default: test). |
only | Limit when jobs are created. Also available: only:refs, only:kubernetes, only:variables, and only:changes. |
except | Limit when jobs are not created. Also available: except:refs, except:kubernetes, except:variables, and except:changes. |
rules | List of conditions to evaluate and determine selected attributes of a job, and whether or not it is created. May not be used alongside only/except. |
rules | List of conditions to evaluate and determine selected attributes of a job, and whether or not it is created. May not be used alongside only/except. |
tags | List of tags which are used to select Runner. |
allow_failure | Allow job to fail. Failed job doesn’t contribute to commit status. |
when | When to run job. Also available: when:manual and when:delayed. |
environment | Name of an environment to which the job deploys. Also available: environment:name, environment:url, environment:on_stop, and environment:action. |
cache | List of files that should be cached between subsequent runs. Also available: cache:paths, cache:key, cache:untracked, and cache:policy. |
artifacts | List of files and directories to attach to a job on success. Also available: artifacts:paths, artifacts:name, artifacts:untracked, artifacts:when, artifacts:expire_in, artifacts:reports, and artifacts:reports:junit. |
dependencies | Other jobs that a job depends on so that you can pass artifacts between them. |
coverage | Code coverage settings for a given job. |
retry | When and how many times a job can be auto-retried in case of a failure. |
timeout | Define a custom timeout that would take precedence over the project-wide one. |
parallel | How many instances of a job should be run in parallel. |
trigger | Defines a downstream pipeline trigger. |
include | Allows this job to include external YAML files. Also available: include:local, include:file, include:template, and include:remote. |
extends | Configuration entries that this job is going to inherit from. |
pages | Upload the result of a job to use with GitLab Pages. |
variables | Define job variables on a job level. |
interruptible | Defines if a job can be canceled when made redundant by a newer run |
可以使用default:关键字将某些参数全局设置为所有作业的默认设置
。然后,作业的特定配置可以覆盖默认参数。
可以在default:块内定义以下作业参数:
image
services
before_script
after_script
cache
在以下示例中,ruby:2.5 image被设置为除rspec 2.6作业外,其他作业的默认值,rspec 2.6作业使用ruby:2.6 image:
default:
image: ruby:2.5
rspec:
script: bundle exec rspec
rspec 2.6:
image: ruby:2.6
script: bundle exec rspec
以下是用于配置CI/CD pipeline的参数的详细说明。
job:
script: "bundle exec rspec"
此参数还可以包含使用数组的几个命令:
job:
script:
- uname -a
- bundle exec rspec
待续。。。