Skip to main content
Learn how to refactor hard-coded values into environment variables within your GitHub Actions workflows. This approach adheres to the DRY principle, simplifies maintenance, and enhances readability.

1. Baseline Workflow (Hard-coded Values)

Hard-coding credentials like dockerUsername and s3curePaSsW0rd is insecure and violates DRY. Always use variables and secrets instead.

2. Step-Level Environment Variables

Define env per step to remove inline repetition:

Referencing Variables

You can reference environment variables in two ways inside run: blocks:
  1. Shell syntax
  2. GitHub Actions expression
Both methods are supported; choose based on readability.

3. Job-Level Environment Variables

Move shared variables to the job level so all steps inherit them:
  • Job-level env applies to all steps in that job.
  • Step-specific env values (like DOCKER_PASSWORD) override or extend the job-level settings.

Variable Scope Overview


4. Workflow-Level Environment Variables

Set env at the workflow root so all jobs inherit the same defaults:
All jobs now share CONTAINER_REGISTRY, DOCKER_USERNAME, and IMAGE_NAME from the workflow-level env.

5. Running the Workflow and Inspecting Logs

Once the workflow runs, GitHub Actions expands and masks sensitive data:
Sensitive values like DOCKER_PASSWORD are automatically masked in the logs.

Next Steps

Learn how to use secrets for secure storage of credentials:

Watch Video