Environment Variables in GitHub Actions (with Docker Example)

Search for a command to run...

No comments yet. Be the first to comment.
Introduction User access management is one of the most fundamental responsibilities in any infrastructure environment, yet it is often handled in an ad hoc and manual way. In many organizations, espec

Managing sensitive credentials like API keys across multiple GitHub repositories is challenging. It leads to duplicate secrets, a lack of versioning, and potential inconsistencies between environments. The solution is to use HashiCorp Vault Secrets o...

Introduction DevOps engineers today spend countless hours context-switching between dashboards, logs, cloud consoles, and documentation. Traditional AI tools like ChatGPT are great at answering questions, but they lack direct access to your infrastru...

When working with GitHub Actions, you may face situations where multiple workflow runs overlap, consume unnecessary resources, or even cause conflicts in deployment. This is where concurrency comes into play. Concurrency in GitHub Actions allows you ...

When teams scale, managing workflows across multiple repositories becomes more complex. Each project often shares common configuration values, cloud credentials, or deployment secrets. Instead of duplicating these across repositories, GitHub provides...

CloudDecode
11 posts
CloudDecode simplifies cloud & DevOps—covering Azure, AWS, Kubernetes, Terraform, CI/CD & more—with clear guides to help you decode, learn, and build with confidence.
Automation is at the heart of modern software delivery, and GitHub Actions has become a go-to solution for CI/CD pipelines. One of the most powerful features in GitHub Actions is the ability to manage environment variables (envs). These variables help control behavior, store configuration, and keep sensitive data secure.
In this article, we’ll explore different ways to declare and use environment variables in GitHub Actions, and we’ll tie it all together with a Docker build and push example.
Environment variables are key-value pairs that provide configuration settings to jobs, steps, or even the whole workflow. They can be used to:
Pass configurations (for example, Node.js version, Docker image name)
Control workflow behavior
Store sensitive information (API keys, tokens)
Defined at the top of the workflow, available in all jobs and steps.
env:
WORKFLOW_ENV: "workflow-scope"
Defined under a job, only available within that job.
jobs:
build:
runs-on: ubuntu-latest
env:
JOB_ENV: "job-scope"
Defined under a step, only available within that step.
steps:
- name: Step with env
env:
STEP_ENV: "step-scope"
run: echo "Step var: $STEP_ENV"
Useful for testing across multiple environments (for example, OS or language versions).
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
version: [14, 16]
Use with ${{ matrix.os }} or ${{ matrix.version }}.
Created at runtime with GITHUB_ENV.
- run: echo "BUILD_ID=${{ github.run_id }}" >> $GITHUB_ENV
Used for sensitive values like tokens or API keys.
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
Let’s bring it together with a Docker pipeline.
name: Docker CI/CD
on:
push:
branches: [ main ]
env:
IMAGE_NAME: myapp
jobs:
docker:
runs-on: ubuntu-latest
strategy:
matrix:
version: ["1.0", "2.0"]
steps:
- uses: actions/checkout@v3
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker image
run: |
docker build . -t ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:${{ matrix.version }}
- name: Push Docker image
run: |
docker push ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:${{ matrix.version }}
Workflow-level: IMAGE_NAME is available everywhere.
Matrix-level: Builds multiple versions of the Docker image.
Secrets: Docker credentials are securely injected from GitHub Secrets.
Workflow → Job → Step
↓ ↓
Matrix Dynamic/Secrets
This flow shows how variables cascade down from workflow to job to step, while matrix, dynamic, and secrets act as overlays.
Use workflow-level environment variables for common configurations.
Use job-level environment variables to scope variables to a specific job.
Use step-level environment variables sparingly for unique cases.
Use matrix environment variables to test multiple versions or environments.
Use dynamic environment variables when values need to be generated at runtime.
Use secrets for sensitive information such as credentials and tokens.
Environment variables are a cornerstone of GitHub Actions workflows. Whether you’re building, testing, or deploying with Docker, managing environment variables correctly ensures your pipeline is clean, secure, and maintainable.
By combining these scopes effectively, you can design workflows that adapt easily to new environments and use cases.