Search code examples
gitlabcontinuous-integrationgitlab-cigitlab-ci-runner

Run specific job in Gitlab CI base on a condition


I have a repo QA/tests which I want to run all the jobs when there is a push to this repo.

I used a script to generate the jobs dynamically:

job-generator:
  stage: generate
  tags:
    - kuber
  script:
    - scripts/generate-job.sh > generated-job.yml
  artifacts:
    paths:
      - generated-job.yml

main:
  trigger:
    include:
      - artifact: generated-job.yml
        job: job-generator
    strategy: depend

At the next step, I have another repo products/first which I want to run a specific job in QA/tests at every push in the products/first so I tried:

stages:
  - test

tests:
  stage: test
  variables:
    TARGET: first
  trigger:
    project: QA/tests
    branch: master
    strategy: depend

Then I tried to define a global TARGET: all variable in my main gitlab-ci.yml and override it with the TARGET: first in the above YAML.

generate-job.sh:

#!/bin/bash

PRODUCTS=("first" "second" "third")

for P in "${PRODUCTS[@]}"; do
cat << EOF
$P:
  stage: test
  tags:
    - kuber
  script:
    - echo -e "Hello from $P"
  rules:
    - if: '"$TARGET" == "all"'
      when: always
    - if: '"$TARGET" == $P'
      when: always

EOF
done

But no results. the downstream pipeline doesn't have any job at all!

Any idea?


Solution

  • I solved it with:

    gitlab-ci.yml:

    variables:
      TARGET: all
    
    job-generator:
      stage: generate
      tags:
        - kuber
      script:
        - scripts/generate-job.sh > generated-job.yml
      artifacts:
        paths:
          - generated-job.yml
    
    main:
      variables:
        CHILD_TARGET: $TARGET
      trigger:
        include:
          - artifact: generated-job.yml
            job: job-generator
        strategy: depend
    

    and use CHILD_TARGET in my generate-job.sh:

    #!/bin/bash
    
    PRODUCTS=("first" "second" "third")
    
    for P in "${PRODUCTS[@]}"; do
    cat << EOF
    $P:
      stage: test
      tags:
        - kuber
      script:
        - echo -e "Hello from $P"
      rules:
        - if: '\$CHILD_TARGET == "all"'
          when: always
        - if: '\$CHILD_TARGET == "$P"'
          when: always
    
    EOF
    done
    

    So I could call it from other projects like this:

    stages:
      - test
    
    e2e-tests:
      stage: test
      variables:
        TARGET: first
      trigger:
        project: QA/tests
        branch: master
        strategy: depend