gitlab-ci/cd(十)include关键词

1、include:local

使用include:local包含一个文件,是在同一个存储库中的.gitlab-ci.yml文件。使用相对于根目录 ( /)的完整路径。

如果使用include:local,请确保.gitlab-ci.yml文件和本地文件位于同一分支上

includ会被覆盖,例如参数和script

include:
  - local: '/templates/.gitlab-ci-template.yml'

您还可以使用较短的语法来定义路径:

include: '.gitlab-ci-production.yml'

例子:首先在项目中创建 .gitlab-ci.yml和template/.content.yml

 ====================.gitlab-ci.yml================
variables:
  POSTGRES_USER: user
include:
  - local: '/templates/.content.yml'
stages:
  - firstjob
  - production

firstjob:
  stage: firstjob
  script:
    - echo $POSTGRES_USER
    - echo $POSTGRES_password

production:
  stage: production
  script:
    - echo "This job runs in the build stage."
===================.content.yml ==================
variables:
  POSTGRES_USER: user2
  POSTGRES_password: password
production:
  stage: production
  before_script:
    - echo $POSTGRES_USER
    - echo $POSTGRES_password
  script:
    - echo content
新兴技术能力gitlab-ci/cd(十)include关键词插图

include:file

要在同一个 GitLab 实例上包含来自另一个私有项目的文件,请使用include:file. 您可以仅include:file与组合使用include:project。使用相对于根目录 ( /)的完整路径。

例如:

include:
  - project: 'my-group/my-project'
    file: '/templates/.gitlab-ci-template.yml'

您还可以指定一个ref. 如果不指定值,则 ref 默认为HEAD项目的 :

include:
  - project: 'my-group/my-project'
    ref: main
    file: '/templates/.gitlab-ci-template.yml'

  - project: 'my-group/my-project'
    ref: v1.0.0
    file: '/templates/.gitlab-ci-template.yml'

  - project: 'my-group/my-project'
    ref: 787123b47f14b552955ca2786bc9542ae66fee5b  # Git SHA
    file: '/templates/.gitlab-ci-template.yml'

include:remote

include:remote与完整 URL 一起使用以包含来自不同位置的文件。远程文件必须可通过 HTTP/HTTPSGET请求公开访问,因为不支持远程 URL 中的身份验证。例如:

include:
  - remote: 'https://gitlab.com/example-project/-/raw/main/.gitlab-ci.yml'

include:template

使用include:template包括.gitlab-ci.yml那些模板 随GitLab

例如:

# File sourced from the GitLab template collection
include:
  - template: Auto-DevOps.gitlab-ci.yml

多个include:template文件:

include:
  - template: Android-Fastlane.gitlab-ci.yml
  - template: Auto-DevOps.gitlab-ci.yml