本文为https://gitlab.starcart.cn/help/ci/yaml/README.md的翻译。
在每个项目中使用一个叫.gitlab-ci.yml的YAML文件来配置GitLab CI/CD pipeline。该.gitlab-ci.yml文件定义了pipeline的结构和顺序,并确定:
- 使用GitLab Runner执行什么。
- 遇到特定条件时要做出什么决定。例如,当一个过程成功或失败时。
本主题涵盖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)是:
- 定义了约束,指出应在什么条件下执行它们。
- 具有任意名称的顶级元素,并且必须至少包含script子句。
- 不限制可以定义多少个。
例如:
job1:
script: "execute-script-for-job1"
job2:
script: "execute-script-for-job2"
上面的示例是具有两个单独作业的最简单的CI/CD配置,其中每个作业执行一个不同的命令。当然,命令可以在仓库中直接执行代码(./configure;make;make install)或运行脚本(test.sh)。
作业由Runners拾取并在Runner的环境中执行。重要的是,每个作业都是彼此独立运行的。
检验.gitlab-ci.yml
每个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)时出现检验错误,请尝试执行以下操作:
- 引用他们。
- 将它们更改为其他形式。例如,/bin/true。
配置参数
作业定义为一系列的定义作业行为的参数列表。
下表列出了作业的可用参数:
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的参数的详细说明。
- script
script是作业中所需的唯一必需关键字。这是一个由Runner执行的shell脚本。例如:
job:
script: "bundle exec rspec"
此参数还可以包含使用数组的几个命令:
job:
script:
- uname -a
- bundle exec rspec
- image
用于为作业指定Docker 映像。
对于:
简单的定义示例,请参见Define image和services来自.gitlab-ci.yml。
详细的使用信息,请参阅Docker集成文档。 - image:name
一个继承docker配置选项。
更多的信息,参阅映像的可用设置。 - image:entrypoint
一个继承docker配置选项。
更多的信息,参阅映像的可用设置。 - services
用于指定服务器Docker的映像,该映像链接到中指定的基本映像image。
简单的定义示例,请参见Define image和services来自.gitlab-ci.yml。
详细的使用信息,请参阅Docker集成文档。
有关示例服务,请参见GitLab CI服务。 - services:name,services:alias,services:entrypoint,services:command
一个继承docker配置选项。 - before_script 和 after_script
before_script用于定义在所有作业(包括部署作业)之前,但在还原工件之后运行的命令。这可以是数组或多行字符串。
after_script用于定义将在所有作业(包括失败的作业)之后运行的命令。这必须是数组或多行字符串。
指定的脚本before_script为:
待续。。。