[英]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 運行 - 所以項目 Z572D4E421E5E6B9BC11D815E8A02 我們將創建兩個文件。在那里只是為了展示可能性。
是一個完整的 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
此外,您還可以將錨點用於腳本部分。 看起來像這樣
包括.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.