14

I have to migrate from jenkins to gitlab and I would like to be able to use dynamic job names in order to have some information directly in the pipeline summary without having to click on each job etc.... in jenkins we can immediately see the parameters passed to our job and this is not the case in gitlab-ci.

My test runner being on a windows I tried to define the yml as follows:

job_%myParam%:
  stage: build
  script:
    - set>varList.txt
  artifacts:
    paths:
    - varList.txt

When I start my job with %myParam%=true, the variable is not interpreted in the job name and so it takes the name of job_%myParam% instead of expected "job_true".

Is that even possible?

thks :)

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
lepapareil
  • 491
  • 3
  • 6
  • 16

3 Answers3

16

As of gitlab 12.9, this can be done using trigger and child pipelines — although a little involved:

Quoting the example from the gitlab doc:

generate-config:
  stage: build
  script: generate-ci-config > generated-config.yml
  artifacts:
    paths:
      - generated-config.yml

child-pipeline:
  stage: test
  trigger:
    include:
      - artifact: generated-config.yml
        job: generate-config

In your case, you would put the definition of job_%myParam% in job.yml.in, then have the script generate-ci-config be e.g. sed "s/%myParam%/$PARAM/g" job.yml.in.

This is probably a bit much for just changing the names of course, and there will be a cost associated to the additional stage; but it does answer the question, and may be useful to do more, like start with different versions of the parameter in the same pipeline.

D-rk
  • 5,513
  • 1
  • 37
  • 55
AltGr
  • 331
  • 2
  • 4
  • Child pipelines limit you in how nested you can go – trallnag Feb 02 '21 at 23:44
  • in gitlab 14.3 there is also "variable inside variable" enabled https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1809 – Iron Bishop Oct 27 '21 at 14:13
  • AFAICT this approach should also allow defining Gitlab jobs completely dynamically during runtime, i.e. not just their names but also what they do. Very nice! – balu Apr 06 '23 at 14:26
8

No, it is not possible to have dynamic job names in GitLab CI.

King Chung Huang
  • 5,026
  • 28
  • 24
0

Here is a more specific example of how dynamic job can be generated:

generate-job:
  stage: .pre
  script:
    - |
      cat > generated-job.yml <<- EOM
      job_${myParam}:
        stage: build
        script:
          - echo \$CI_COMMIT_REF_NAME
        artifacts:
          paths:
          - varList.txt
      EOM
      cat generated-job.yml
  artifacts:
    paths:
      - generated-job.yml

run-job:
  stage: build
  trigger:
    include:
      - artifact: generated-job.yml
        job: generate-job
D-rk
  • 5,513
  • 1
  • 37
  • 55