Gitlab CI/CD: Passing Environment Variables Between Jobs: A Practical Guide

Software developer and CEO at RoyalZSoftware. I build web applications for startups with Ruby on Rails, Angular and React.
In continuous integration and continuous deployment (CI/CD) workflows, sharing data between different stages or jobs in a pipeline is often necessary. GitLab CI/CD provides several ways to pass environment variables from one job to another, ensuring that critical information like build artifacts, configurations, or authentication tokens can flow through the pipeline efficiently.
This post will cover different strategies for passing environment variables between jobs in GitLab CI/CD and how to use them effectively.
Why Pass Environment Variables Between Jobs?
Environment variables are an essential part of CI/CD pipelines, allowing you to store dynamic data such as:
API keys or authentication tokens.
Build information, such as version numbers or commit SHAs.
Paths to generated artifacts, like compiled binaries or Docker images.
Configuration details that are required across multiple jobs.
Sometimes, the output or data generated by one job needs to be available in subsequent jobs for successful pipeline execution. Passing environment variables between jobs helps streamline this process, allowing for seamless sharing of dynamic data across different stages in the pipeline.
Strategies for Passing Environment Variables Between Jobs
There are several ways to share environment variables between jobs in GitLab CI/CD, depending on your use case and pipeline design. Here are the most common approaches:
1. Using Artifacts to Store Environment Variables
One of the simplest and most robust ways to pass environment variables between jobs is by using artifacts. Artifacts are files generated by one job that can be downloaded and used by another job. This method works well for larger or more complex data that needs to be passed between jobs.
Example: Storing Environment Variables in a File
In the first job, we store environment variables in a file and use artifacts to pass them to the next job.
stages:
- build
- deploy
build_job:
stage: build
script:
- echo "API_TOKEN=$API_TOKEN" > env_variables.txt
- echo "Build number: $CI_PIPELINE_ID"
artifacts:
paths:
- env_variables.txt
deploy_job:
stage: deploy
script:
- source env_variables.txt
- echo "Deploying with API_TOKEN=$API_TOKEN"
dependencies:
- build_job
Explanation:
The
build_jobcreates a file (env_variables.txt) containing the environment variables.These variables are stored as artifacts and passed to the next job,
deploy_job.In the
deploy_job, the environment variables are sourced from the file and used in the deployment script.
2. Using dependencies to Share Artifacts
When passing artifacts between jobs, it’s essential to ensure that subsequent jobs have access to those artifacts. This is where the dependencies keyword comes in handy. It allows jobs to specify which upstream jobs they should inherit artifacts from.
In the example above, deploy_job uses dependencies to inherit the artifacts (the file containing environment variables) from build_job. This allows the deployment stage to access the stored environment variables.

