GitLab CI/CD include examples
- Single string or array of multiple values
- Re-using a
before_script
template - Overriding external template values
- Using nested includes
GitLab CI/CD include examples
除了GitLab CI YAML 参考中列出的includes
示例之外,此页面还列出了include
用法的更多变化.
Single string or array of multiple values
您可以将额外的 YAML 文件作为单个字符串或多个值的数组包含在内. 以下示例均有效.
include:local
方法的单个字符串暗含:
include: '/templates/.after-script-template.yml'
Array with include
method implied:
include:
- 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
- '/templates/.after-script-template.yml'
具有明确指定的include
方法的单个字符串:
include:
remote: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
include:remote
为单个项目的数组:
include:
- remote: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
具有多个显式指定的include
方法的数组:
include:
- remote: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
- local: '/templates/.after-script-template.yml'
- template: Auto-DevOps.gitlab-ci.yml
数组混合语法:
include:
- 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
- '/templates/.after-script-template.yml'
- template: Auto-DevOps.gitlab-ci.yml
- project: 'my-group/my-project'
ref: master
file: '/templates/.gitlab-ci-template.yml'
Re-using a before_script
template
在以下示例中, .before-script-template.yml
的内容将与.gitlab-ci.yml
的内容一起自动获取和评估.
https://gitlab.com/awesome-project/raw/master/.before-script-template.yml
内容:
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
- gem install bundler --no-document
- bundle install --jobs $(nproc) "${FLAGS[@]}"
.gitlab-ci.yml
内容:
include: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
rspec:
script:
- bundle exec rspec
Overriding external template values
以下示例显示了特定的 YAML 定义的变量以及.gitlab-ci.yml
自定义的包含文件中production
作业的详细信息.
https://company.com/autodevops-template.yml
内容:
variables:
POSTGRES_USER: user
POSTGRES_PASSWORD: testing_password
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
production:
stage: production
script:
- install_dependencies
- deploy
environment:
name: production
url: https://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN
only:
- master
.gitlab-ci.yml
内容:
include: 'https://company.com/autodevops-template.yml'
image: alpine:latest
variables:
POSTGRES_USER: root
POSTGRES_PASSWORD: secure_password
stages:
- build
- test
- production
production:
environment:
url: https://domain.com
在这种情况下,变量POSTGRES_USER
和POSTGRES_PASSWORD
以及autodevops-template.yml
定义的production
作业的环境 URL 已被.gitlab-ci.yml
定义的新值覆盖.
合并使您可以扩展和覆盖字典映射,但是不能向包含的数组添加或修改项目. 例如,要将其他项目添加到生产作业脚本中,必须重复现有的脚本项目:
https://company.com/autodevops-template.yml
内容:
production:
stage: production
script:
- install_dependencies
- deploy
.gitlab-ci.yml
内容:
include: 'https://company.com/autodevops-template.yml'
stages:
- production
production:
script:
- install_dependencies
- deploy
- notify_owner
在这种情况下,如果未在.gitlab-ci.yml
重复install_dependencies
和deploy
,则它们将不会成为组合 CI 配置中production
作业脚本的一部分.
Using nested includes
以下示例显示了如何使用不同方法的组合从不同来源嵌套包含对象.
在此示例中, .gitlab-ci.yml
本地包含文件/.gitlab-ci/another-config.yml
:
include:
- local: /.gitlab-ci/another-config.yml
/.gitlab-ci/another-config.yml
包含一个模板和另一个项目中的/templates/docker-workflow.yml
文件:
include:
- template: Bash.gitlab-ci.yml
- project: group/my-project
file: /templates/docker-workflow.yml
该/templates/docker-workflow.yml
出现在group/my-project
包括的两个本地文件group/my-project
:
include:
- local: /templates/docker-build.yml
- local: /templates/docker-testing.yml
我们在group/my-project
/templates/docker-build.yml
添加了一个docker-build
作业:
docker-build:
script: docker build -t my-image .
我们在group/my-project
出现的第二个/templates/docker-test.yml
添加了一个docker-test
作业:
docker-test:
script: docker run my-image /run/tests.sh