GitLab CI/CD include examples

原文:https://docs.gitlab.com/ee/ci/yaml/includes.html

GitLab CI/CD include examples

除了GitLab CI YAML 参考中列出的includes示例之外,此页面还列出了include用法的更多变化.

Single string or array of multiple values

您可以将额外的 YAML 文件作为单个字符串或多个值的数组包含在内. 以下示例均有效.

include:local方法的单个字符串暗含:

  1. include: '/templates/.after-script-template.yml'

Array with include method implied:

  1. include:
  2. - 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
  3. - '/templates/.after-script-template.yml'

具有明确指定的include方法的单个字符串:

  1. include:
  2. remote: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'

include:remote为单个项目的数组:

  1. include:
  2. - remote: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'

具有多个显式指定的include方法的数组:

  1. include:
  2. - remote: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
  3. - local: '/templates/.after-script-template.yml'
  4. - template: Auto-DevOps.gitlab-ci.yml

数组混合语法:

  1. include:
  2. - 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
  3. - '/templates/.after-script-template.yml'
  4. - template: Auto-DevOps.gitlab-ci.yml
  5. - project: 'my-group/my-project'
  6. ref: master
  7. 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内容:

  1. before_script:
  2. - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
  3. - gem install bundler --no-document
  4. - bundle install --jobs $(nproc) "${FLAGS[@]}"

.gitlab-ci.yml内容:

  1. include: 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml'
  2. rspec:
  3. script:
  4. - bundle exec rspec

Overriding external template values

以下示例显示了特定的 YAML 定义的变量以及.gitlab-ci.yml自定义的包含文件中production作业的详细信息.

https://company.com/autodevops-template.yml内容:

  1. variables:
  2. POSTGRES_USER: user
  3. POSTGRES_PASSWORD: testing_password
  4. POSTGRES_DB: $CI_ENVIRONMENT_SLUG
  5. production:
  6. stage: production
  7. script:
  8. - install_dependencies
  9. - deploy
  10. environment:
  11. name: production
  12. url: https://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN
  13. only:
  14. - master

.gitlab-ci.yml内容:

  1. include: 'https://company.com/autodevops-template.yml'
  2. image: alpine:latest
  3. variables:
  4. POSTGRES_USER: root
  5. POSTGRES_PASSWORD: secure_password
  6. stages:
  7. - build
  8. - test
  9. - production
  10. production:
  11. environment:
  12. url: https://domain.com

在这种情况下,变量POSTGRES_USERPOSTGRES_PASSWORD以及autodevops-template.yml定义的production作业的环境 URL 已被.gitlab-ci.yml定义的新值覆盖.

合并使您可以扩展和覆盖字典映射,但是不能向包含的数组添加或修改项目. 例如,要将其他项目添加到生产作业脚本中,必须重复现有的脚本项目:

https://company.com/autodevops-template.yml内容:

  1. production:
  2. stage: production
  3. script:
  4. - install_dependencies
  5. - deploy

.gitlab-ci.yml内容:

  1. include: 'https://company.com/autodevops-template.yml'
  2. stages:
  3. - production
  4. production:
  5. script:
  6. - install_dependencies
  7. - deploy
  8. - notify_owner

在这种情况下,如果未在.gitlab-ci.yml重复install_dependenciesdeploy ,则它们将不会成为组合 CI 配置中production作业脚本的一部分.

Using nested includes

以下示例显示了如何使用不同方法的组合从不同来源嵌套包含对象.

在此示例中, .gitlab-ci.yml本地包含文件/.gitlab-ci/another-config.yml

  1. include:
  2. - local: /.gitlab-ci/another-config.yml

/.gitlab-ci/another-config.yml包含一个模板和另一个项目中的/templates/docker-workflow.yml文件:

  1. include:
  2. - template: Bash.gitlab-ci.yml
  3. - project: group/my-project
  4. file: /templates/docker-workflow.yml

/templates/docker-workflow.yml出现在group/my-project包括的两个本地文件group/my-project

  1. include:
  2. - local: /templates/docker-build.yml
  3. - local: /templates/docker-testing.yml

我们在group/my-project /templates/docker-build.yml添加了一个docker-build作业:

  1. docker-build:
  2. script: docker build -t my-image .

我们在group/my-project出现的第二个/templates/docker-test.yml添加了一个docker-test作业:

  1. docker-test:
  2. script: docker run my-image /run/tests.sh