🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# GitLab CI/CD include examples > 原文:[https://docs.gitlab.com/ee/ci/yaml/includes.html](https://docs.gitlab.com/ee/ci/yaml/includes.html) * [Single string or array of multiple values](#single-string-or-array-of-multiple-values) * [Re-using a `before_script` template](#re-using-a-before_script-template) * [Overriding external template values](#overriding-external-template-values) * [Using nested includes](#using-nested-includes) # GitLab CI/CD include examples[](#gitlab-cicd-include-examples "Permalink") 除了[GitLab CI YAML 参考中](README.html)列出的[`includes`示例](README.html#include)之外,此页面还列出了`include`用法的更多变化. ## Single string or array of multiple values[](#single-string-or-array-of-multiple-values "Permalink") 您可以将额外的 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[](#re-using-a-before_script-template "Permalink") 在以下示例中, `.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[](#overriding-external-template-values "Permalink") 以下示例显示了特定的 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[](#using-nested-includes "Permalink") 以下示例显示了如何使用不同方法的组合从不同来源嵌套包含对象. 在此示例中, `.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 ```