将Gitlab项目调用存储在中央位置的相同gitlab-ci.yml [英] Having Gitlab Projects calling the same gitlab-ci.yml stored in a central location

查看:83
本文介绍了将Gitlab项目调用存储在中央位置的相同gitlab-ci.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多Gitlab项目都遵循相同的CI模板.只要CI脚本有很小的变化,我就必须在每个项目中手动修改CI脚本.有没有一种方法可以将CI脚本存储在中央位置,并用一些环境变量替换项来使您的项目称为CI脚本?例如,

I have many Gitlab project followed the same CI template. Whenever there is a small change in the CI script, I have to manually modify the CI script in each project. Is there a way you can store your CI script in a central location and have your project called that CI script with some environment variable substitution? For instance,

gitlab-ci.yml

gitlab-ci.yml in each project

/bin/bash -c "$(curl -fsSL <link_to_the_central_location>.sh)"

gitlab-ci.yml位于中心位置

gitlab-ci.yml in the central location

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指令支持此功能.

You do not need curl, actually gitlab supports this via the include directive.

  1. 您需要一个存储库,用于存储常规的yml文件.(您可以选择是整个ci文件还是仅一部分.对于本示例,请调用此存储库CI,并假设您的gitlab在example.com上运行-因此项目网址为example.com/ci.我们创建了两个文件在那里只是为了展示可能性.

  1. you need a repository, where you store your general yml files. (you can choose if it is a whole ci file, or just parts. For this example lets call this repository CI and assume your gitlab runs at example.com - so the project url would be example.com/ci. we create two files in there just to show the possibilities.

  1. 是完整的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类似.

  • There is even the option to use template string from yaml. please reference the gitlab documentation but it is similar to 2.

  • 我们确实有想要使用此类定义的项目.所以

  • we do have our project which wants to use such definitions. so either

    1. 用于整个CI文件

    1. For the whole CI file

     include:
       - project: 'ci'
         ref: master # think about tagging if you need it
         file: 'ci.yml'
    

    现在您可以看到,我们正在引用一个yml文件,其中包含所有内容.

    as you can see now we are referencing one yml file, with all the cahnges.

    具有部分延伸

     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

    As you see, you can easily use the includes, to have a way more granular setup. Additionally you could define at job1 and job2 variables, eg for the test target, and move the script block into the includes.yml

  • 还可以使用锚定用于脚本部分.看起来像这样

    Futhermore you can also use anchors for the script parts. Which looks like this

    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
    

    ,您还可以在配置中使用脚本锚点

    and you can use also the script anchor within your configuration

    有关更深入的说明,您还可以查看 https://docs.gitlab.com/ee/ci/yaml/includes.html

    For a deeper explanation you can also take a look at https://docs.gitlab.com/ee/ci/yaml/includes.html

    这篇关于将Gitlab项目调用存储在中央位置的相同gitlab-ci.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆