- Welcome my DevOps blog./
- 🔰Posts/
- 🗂️My Trainings/
- VCS Trainings and Certifications/
- GitHub Actions - The Complete Guide/
- GitHub Actions: Environment Variables & Secrets/
GitHub Actions: Environment Variables & Secrets

Table of Contents
| External Resources » | ||
|---|---|---|
| GitHub Actions official Documentation | GitHub Actions Marketplace | GitHub.com |
Environment Variables #
Declaring a variable #
In GitHub Actions, you can declare variables at three levels:
- workflow
- job
- step
Workflow-level variables apply to the entire workflow, job-level variables are specific to a job, and step-level variables are only available within a specific step.
1. At Workflow level #
env:
GLOBAL_VAR: 'Global Value'
Variable is always the same across all jobs and environments in this Workflow.
2. At a Job level #
jobs:
build:
runs-on: ubuntu-latest
env:
JOB_VAR: 'Job-specific Value'
Variable is different for each environment.
3. At a Step level #
steps:
- name: Set Step Variable
run: echo "STEP_VAR=Step Value" >> $GITHUB_ENV
Accessing a variable #
To access these variables in your scripts, use the following syntax:
- For shell commands:
$VARIABLE_NAME - In YAML expressions:
${{ env.VARIABLE_NAME }}
This structure allows you to adapt your workflows based on the context of the run, making your CI/CD processes more flexible and efficient.
'Testing']:::greenclass --> subB D[Environment 2
'Production']:::redclasss --> subB subgraph DB_password direction TB subB[const password = process.env.DB_PASSWORD]:::redclass end subB --> C[const password = 'abc']:::greenclass subB --> F[const password = '123']:::redclasss
To access environment variables in a Windows runner, use the syntax $env:VARIABLE_NAME in PowerShell. This allows you to retrieve the value of the specified environment variable during your script execution.
More info:
Default Environment Variables #
GitHub Actions also provides a couple of default environment variables that are set automatically: https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
These environment variable can, for example, give you quick access to the repository to which the workflow belongs, the name of the event that triggered the workflow and many other things.
Environment Variables vs Secrets #
Creating secrets for a repository #
To create secrets or variables on GitHub for an organization repository, you must have write access. For a personal account repository, you must be the repository owner to create secrets or variable in the web UI or a repository collaborator to create secrets or variables through the REST API.
- On GitHub, navigate to the main page of the repository.
- Under your repository name, click Settings. If you cannot see the “Settings” tab, select the- dropdown menu, then click Settings.

In the “Security” section of the sidebar, select
Secrets and variables, then click Actions.
Click the Secrets tab.

- Click New repository secret.
- In the Name field, type a name for your secret.
- In the Secret field, enter the value for your secret.
- Click Add secret.
If your repository has environment secrets or can access secrets from the parent organization, then those secrets are also listed on this page.
Secrets can be stored for a specific repository or for an environment.
They can be stored at a repository-level or at an organization-level.
More info:
Accessing secrets #
Secrets context object is being used to access / reference secrets stored in GitHub Actions repository / organization.
| |
GitHub Deployment Environments #
Environments are used to describe a general deployment target like production, staging, or development.
When a GitHub Actions workflow deploys to an environment, the environment is displayed on the main page of the repository.
Referencing environments #
| |
More info:
- Deployment Environments: https://docs.github.com/en/actions/concepts/workflows-and-actions/deployment-environments
- Creating environments: Managing environments for deployment
» Sources « #
- GitHub Actions Variables: https://docs.github.com/en/actions/concepts/workflows-and-actions/variables
- Variable References: https://docs.github.com/en/actions/reference/workflows-and-actions/variables
- Using Secrets in GitHub Actions: https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets
- Deployment Environments: https://docs.github.com/en/actions/concepts/workflows-and-actions/deployment-environments
- Creating environments: Managing environments for deployment
» Disclaimer « #
This series draws heavily from Maximilian Schwarzmüller’s GitHub Actions - The Complete Guide course on Udemy.
| About the instructor: | |
|---|---|
| 🌐 Website | 📺 YouTube |
| 🗃️ GitHub |
| My Repos for this section: | |
|---|---|
| cicd-gh-actions-course | Learnings from "GitHub Actions - The Complete Guide" on Udemy. |
