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方法的单个字符串暗含:

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_USERPOSTGRES_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_dependenciesdeploy ,则它们将不会成为组合 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 
Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2021-03-27 13:48:24

results matching ""

    No results matching ""