让 Gitlab 项目调用存储在中央位置的相同 gitlab-ci.yml
Having Gitlab Projects calling the same gitlab-ci.yml stored in a central location
我有很多 Gitlab 项目都遵循相同的 CI 模板。每当 CI 脚本有一个小改动,我就不得不在每个项目中手动修改 CI 脚本。有没有一种方法可以将 CI 脚本存储在一个中央位置,并让您的项目调用该 CI 脚本并替换一些环境变量?例如,
gitlab-ci.yml每个项目
/bin/bash -c "$(curl -fsSL <link_to_the_central_location>.sh)"
gitlab-ci.yml在中心位置
stages:
- build
- test
build-code-job:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
test-code-job1:
stage: test
script:
- echo "If the files are built successfully, test some files with one command:"
- rake test1
test-code-job2:
stage: test
script:
- echo "If the files are built successfully, test other files with a different command:"
- rake test2
你不需要 curl,实际上 gitlab 通过 include 指令支持这个。
您需要一个存储库,用于存储您的通用 yml 文件。 (你可以选择它是一个完整的 ci 文件,还是只是部分文件。对于这个例子,让我们调用这个存储库 CI 并假设你的 gitlab 在 example.com 运行 - 所以项目 url 将是示例。com/ci。我们在其中创建两个文件只是为了展示可能性。
是一个完整的 CI 定义,随时可用 - 让我们调用文件 ci.yml
。这种方法不是很灵活
stages:
- build
- test
build-code-job:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
test-code-job1:
stage: test
script:
- echo "If the files are built successfully, test some files with one command:"
- rake test1
test-code-job2:
stage: test
script:
- echo "If the files are built successfully, test other files with a different command:"
- rake test2
是部分CI定义,更具可扩展性。让我们调用文件 includes.yml
.build:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
.test:
stage: test
script:
- echo "this script tag will be overwritten"
甚至可以选择使用来自 yaml 的模板字符串。请参考 gitlab 文档,但它类似于 2.
我们确实有我们的项目想要使用这样的定义。所以要么
对于整个 CI 文件
include:
- project: 'ci'
ref: master # think about tagging if you need it
file: 'ci.yml'
如您所见,我们正在引用cing 一个包含所有内容的 yml 文件。
部分扩展
include:
- project: 'ci'
ref: master # think about tagging if you need it
file: 'includes.yml'
stages:
- build
- test
build-code-job:
extends: .build
job1:
extends: .test
script:
- rake test1
job2:
extends: .test
script:
- rake test2
如您所见,您可以轻松地使用 includes 来进行更精细的设置。此外,您可以定义 job1
和 job2
变量,例如用于测试目标,并将脚本块移动到 includes.yml
此外,您还可以将 anchors 用于脚本部分。看起来像这样
includes.yml
.build-scirpt: &build
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
.build:
stage: build
script:
- *build
您还可以在配置中使用脚本锚点
如需更深入的解释,您还可以查看 https://docs.gitlab.com/ee/ci/yaml/includes.html
我有很多 Gitlab 项目都遵循相同的 CI 模板。每当 CI 脚本有一个小改动,我就不得不在每个项目中手动修改 CI 脚本。有没有一种方法可以将 CI 脚本存储在一个中央位置,并让您的项目调用该 CI 脚本并替换一些环境变量?例如,
gitlab-ci.yml每个项目
/bin/bash -c "$(curl -fsSL <link_to_the_central_location>.sh)"
gitlab-ci.yml在中心位置
stages:
- build
- test
build-code-job:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
test-code-job1:
stage: test
script:
- echo "If the files are built successfully, test some files with one command:"
- rake test1
test-code-job2:
stage: test
script:
- echo "If the files are built successfully, test other files with a different command:"
- rake test2
你不需要 curl,实际上 gitlab 通过 include 指令支持这个。
您需要一个存储库,用于存储您的通用 yml 文件。 (你可以选择它是一个完整的 ci 文件,还是只是部分文件。对于这个例子,让我们调用这个存储库 CI 并假设你的 gitlab 在 example.com 运行 - 所以项目 url 将是示例。com/ci。我们在其中创建两个文件只是为了展示可能性。
是一个完整的 CI 定义,随时可用 - 让我们调用文件
ci.yml
。这种方法不是很灵活stages: - build - test build-code-job: stage: build script: - echo "Check the ruby version, then build some Ruby project files:" - ruby -v - rake test-code-job1: stage: test script: - echo "If the files are built successfully, test some files with one command:" - rake test1 test-code-job2: stage: test script: - echo "If the files are built successfully, test other files with a different command:" - rake test2
是部分CI定义,更具可扩展性。让我们调用文件
includes.yml
.build: stage: build script: - echo "Check the ruby version, then build some Ruby project files:" - ruby -v - rake .test: stage: test script: - echo "this script tag will be overwritten"
甚至可以选择使用来自 yaml 的模板字符串。请参考 gitlab 文档,但它类似于 2.
我们确实有我们的项目想要使用这样的定义。所以要么
对于整个 CI 文件
include: - project: 'ci' ref: master # think about tagging if you need it file: 'ci.yml'
如您所见,我们正在引用cing 一个包含所有内容的 yml 文件。
部分扩展
include: - project: 'ci' ref: master # think about tagging if you need it file: 'includes.yml' stages: - build - test build-code-job: extends: .build job1: extends: .test script: - rake test1 job2: extends: .test script: - rake test2
如您所见,您可以轻松地使用 includes 来进行更精细的设置。此外,您可以定义
job1
和job2
变量,例如用于测试目标,并将脚本块移动到includes.yml
此外,您还可以将 anchors 用于脚本部分。看起来像这样
includes.yml
.build-scirpt: &build
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
.build:
stage: build
script:
- *build
您还可以在配置中使用脚本锚点
如需更深入的解释,您还可以查看 https://docs.gitlab.com/ee/ci/yaml/includes.html