Setting Variables Dynamically Based on GitLab CI/CD Branch in Shell Script

  • 5 September 2023
  • 1 reply

Userlevel 4

In DataOps CI/CD pipelines, you may need to set environment variables dynamically based on the Git branch being used. This can be achieved by creating a shell script that evaluates the $CI_COMMIT_BRANCH variable and exports the desired variables accordingly. This article will guide you through the process of creating such a script and integrating it into your DataOps CI/CD pipeline.


  • Access to a DataOps project with CI/CD enabled.
  • A basic understanding of DataOps CI/CD configuration.

Step 1: Create a Shell Script

First, create a shell script that sets variables based on the value of $CI_COMMIT_BRANCH. Save this script as inside a directory named scripts in your project's root directory.

echo "Hey"

if [[ $CI_COMMIT_BRANCH == 'master' ]]; then
  export VAR_A='master'
elif [[ $CI_COMMIT_BRANCH == 'qa' ]]; then
  export VAR_A='qa'
  export VAR_A='whatever'

echo $VAR_A

In this script:

  • We start by echoing the branch name to verify that it's correctly captured.
  • We use conditional statements to set VAR_A based on the branch name.
  • You can customize the conditions and variable assignments as needed.

Step 2: Modify Your CI/CD Configuration

Now, let's modify your DataOps CI/CD configuration to integrate the shell script. In your -ci.yml file, add a job that loads the secrets and runs the shell script.

    - cp scripts/ /runner-scripts/
    - chmod +x /runner-scripts/
- /dataops

In this script:

  • cp scripts/ /runner-scripts/ copies the shell script to a location where it can be executed.
  • chmod +x /runner-scripts/ ensures that the script is executable.

With this configuration, the load_secrets job will set VAR_A based on the branch name, as defined in the shell script.


By creating a shell script that dynamically sets environment variables based on the Git branch name and integrating it into your DataOps CI/CD pipeline, you can streamline your CI/CD process and customize variable values according to different branches. This approach ensures a more flexible and efficient pipeline configuration.

1 reply

Userlevel 2
Badge +1

I completely agree with your article. Since dynamic environment variables based on branches or other criteria are indeed a standard practice in CI/CD pipelines. It’s great that you have highlighted topic with clear examples in the guide. This hands-on approach will benefit people who are looking to optimize their CI/CD processes.