我有许多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指令支持curl。
-
您需要一个存储库,用于存储一般的yml文件。(您可以选择它是一个完整的ci文件,还是只是部分。对于本例,我们将此存储库称为CI,并假设您的gitlab在example.com上运行-因此项目url将是example.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'
正如你现在看到的,我们引用了一个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
job1
和job2
变量,例如用于测试目标,并将脚本块移动到includes.yml
-
此外,您还可以为脚本部分使用锚。它看起来像这样
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