gitlabgitlab-ci

Trigger a child pipeline based on reading flags from a config file


I have a config File: config.yml for each project (repository structure provided at the bottom)

AWS_Deploy_Code: True
Azure_Deploy_Code: False

I want to read the config file and trigger the stages based on the flags within the YAML.

If AWS_Deploy_Code =True then trigger Stage_A

If Azure_Deploy_Code =True then trigger Stage_B

Repository is designed as

|->PROJECT-A
| |-> config.yml
|
|->PROJECT-B
| |-> config.yml

I am able to read the YAML file and save the flags in a .env file. However, I am unable to trigger the specific stage based on the flags from YAML/.env file


Solution

  • You cannot choose between stages, but you can build the rest of the pipeline (Downstream Pipelines) based on the values of config.yml. Therefore you should have one step before to execute the script.

    gitlab-ci.yml

    generate-config:
      stage: generate
      script: generate-ci-config > generated-config.yml
      artifacts:
        paths:
          - generated-config.yml
    
    child-pipeline:
      stage: execute
      trigger:
        include:
          - artifact: generated-config.yml
            job: generate-config
    

    generate-ci-config

    #!/bin/bash
    # Some stuff for reading the YAML file
    if [[ "$AWS_Deploy_Code" == "True" ]]; then
       cat << EOF
       stages:
         - stage_A
       stage-A:
         stage: stage_A
         script: "echo stage_A"
       EOF
    elif [[ "$Azure_Deploy_Code" == "False" ]]; then
       cat << EOF
       stages:
         - stage_B
       stage-B:
         stage: stage_B
         script: "echo stage_B"
       EOF
    else
       echo "Error"
    fi