Need to Dynamically Display and Select Variables Based on Branch in GitLab CI/CD
Hello GitLab Team,
I hope you're doing well. I'm reaching out for assistance with an issue I'm encountering regarding dynamically displaying and selecting variables based on the branch in GitLab CI/CD. Below is the description of the problem and the steps I’ve taken so far.
I am trying to configure a GitLab CI/CD pipeline where I can display pre-defined variables with specific options for the user to select when running the pipeline. The variables should be dynamic, meaning the available options change based on the chosen branch for the pipeline.
What I have tried:
I have defined variables in three separate YAML files (main.yml, gta.yml, version.yml), each containing variables with multiple options. These variables should change depending on the branch selected for the pipeline (e.g., main, gta, version-based branches).
main.yml
variables:
VAR_1:
value: "MAIN_1"
options:
- "MAIN_1"
- "MAIN_2"
- "MAIN_3"
description: "First variable description"
VAR_2:
value: "MAIN_A"
options:
- "MAIN_A"
- "MAIN_B"
description: "Second variable description"
Base YAML file: I used the include directive to include the variables files conditionally based on the branch.
base.yml
include:
- local: 'variables/main.yml'
rules:
- if: '$CI_COMMIT_REF_NAME == "main"'
- local: 'variables/gta.yml'
rules:
- if: '$CI_COMMIT_REF_NAME == "gta"'
- local: 'variables/version.yml'
rules:
- if: '$CI_COMMIT_REF_NAME =~ "/^([0-9]+\.){3}[0-9]+(-RC[0-9]+)$/"'
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "web"
stages:
- build
- test
one:
stage: build
script:
- echo "build...."
- echo "CI_COMMIT_REF_NAME = $CI_COMMIT_REF_NAME"
two:
stage: test
script:
- echo "test..."
- echo "CI_COMMIT_REF_NAME = $CI_COMMIT_REF_NAME"
Additionally, the GitLab CI/CD configuration for this project is referenced from another project’s .gitlab-ci.yml file. This project includes branches such as gta, main, and 1.0.0.0-RC1.
The goal is to show these options to the user during the pipeline execution, allowing them to select from the available choices.
Current Issue: When only two variable templates are defined (e.g., main.yml and gta.yml), the UI behaves as expected and displays the corresponding variables when selecting the branch. However, when a third template is added (e.g., version.yml), the UI no longer loads the variable values correctly. The issue occurs when more than two templates are defined, and the expected behavior for the variable display is not achieved.
I appreciate your time and assistance in resolving this issue. I would greatly appreciate any suggestions or solutions you may have.
Thank you in advance!