200.md 5.5 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
# 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 
```