Skip to main content
Streamline your GitHub Actions workflows by reducing duplication and centralizing configuration with environment variables. In this guide, you’ll learn to declare variables at the step, job, and workflow levels, so you can:
  • Maintain clean, DRY workflows
  • Easily update container names, registry endpoints, and other parameters
  • Secure sensitive data using GitHub Secrets
Below is a sample workflow file (variable-secrets.yaml) that builds, pushes, and deploys a Docker image:
Whenever you push:
  1. GitHub Actions builds the Docker image.
  2. Logs in to Docker Hub.
  3. Pushes the image.
  4. Runs the container in the deploy job, which depends on docker.
Notice how the registry, username, and image name are repeated. Let’s eliminate this duplication using environment variables.

Overview of Environment Variable Scopes


1. Step-Level Environment Variables

Define variables in an individual step. This is ideal for values that aren’t reused elsewhere.
You can reference an environment variable using:
  • $VAR_NAME
  • ${{ env.VAR_NAME }} (recommended when mixing with expressions)

2. Job-Level Environment Variables

Apply the same variables to all steps in a job by declaring them under jobs.<job_id>.env:
Any env under a job cascades to every step. You can still override or augment variables at the step level.

3. Workflow-Level Environment Variables

Declare variables at the top of your workflow to make them available across all jobs and steps:
Never hard-code sensitive values like passwords or API keys. Store them in GitHub Secrets and reference them with ${{ secrets.YOUR_SECRET_NAME }}.

Verifying Your Workflow

  1. Commit and push your changes.
  2. Open the Actions tab in your repository.
  3. Watch the docker and deploy jobs run sequentially.
  4. Expand each step to confirm that variables are correctly substituted and that secrets remain masked.
Use ${{ env.VAR_NAME }} when combining expressions with literal strings to ensure consistent parsing.

References

Watch Video