Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create gitlab-ci.yml file?

Is there any tool to generate .gitlab-ci.yml file like Jenkins has job-dsl-plugin to create jobs?

Jenkins DSL plugin allows me to generate jobs using Groovy, which outputs an xml that describes a job for Jenkins.

I can use DSL and a json file to generate jobs in Jenkins. What I’m looking for is a tool to help me generate .gitlab-ci.yml based on a specification.

like image 744
Hong Miller Avatar asked Oct 31 '17 18:10

Hong Miller


1 Answers

The main question i have to ask what is your goal?

just reduce maintenance effort for repeating job snippets:

Sometimes .gitlab-ci.yml file are pretty similar in a lot of projects, and you want to manage them centrally. Then i recommend to take a look at Having Gitlab Projects calling the same gitlab-ci.yml stored in a central location - which shows multiple ways of centralizing your build,

generate pipeline configuration as the build is highly flexible

Actually this is more a templating task, and can be achieved in nearly every script language you like.

With simple bash, groovy, python, go, .. you name it. In the end the question is, what kind of flexibility you strive for, and what kind of logic you need for the generation. I will not go into the detail on how to generate a the .gitlab-ci.yml file, but how to use it for your next step. Because this is in my opinion the most crucial step. There is the way of simply generating and committing it, but you can also use GitLab CI to generate a file for you, which will be used in the next job of your pipeline.

setup:
  script:
    - echo ".." # generate your yaml file here, maybe use a custom image
  artifacts:
    paths:
      - generated.gitlab-ci.yml

trigger:
  needs:
    - setup
  trigger:
    include:
      - artifact: generated.gitlab-ci.yml
        job: setup
    strategy: depend

This allows you to generate a child pipeline and execute it - we use this for highly generic builds in monorepos.

see for further reading

  • GitLab JSONNET Example - documentation example for generated yml files within a pipeline
  • Dynamic Childpipelines - documentation for dynamically created pipelines
like image 72
Simon Schrottner Avatar answered Nov 07 '22 20:11

Simon Schrottner