Skip to main content
  1. 🔰Posts/
  2. 🗂️My Trainings/
  3. VCS Trainings and Certifications/
  4. GitHub Actions - The Complete Guide/

GitHub Actions: Environment Variables & Secrets

📚 Part 3 of 5: "GitHub Actions The Complete Guide" series.

GitHub Actions environment variables are user-defined values that can influence the behaviour of workflows, allowing for customization based on different contexts.
External Resources »
GitHub Actions official DocumentationGitHub Actions MarketplaceGitHub.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.

flowchart TB classDef redclass fill:#EB4925 classDef redclasss stroke:#EB4925 classDef yellowclass stroke:#EBAC25 classDef greenclass stroke:#C7EB25 A[Environment 1
'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:


IMPORTANT: When calling variables, ensure you respect the levels where the variable was declared (i.e. you can only call a workflow-level variable in all steps but you can’t call variables defined at a step-level in another step).

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 #

In GitHub Actions, variables are used to store non-sensitive information that can be displayed and edited, while secrets are specifically designed to securely store sensitive data, such as API keys, and are encrypted to prevent exposure in logs.

Secrets should be used for any sensitive information, whereas variables can be used for general configuration data.

flowchart TD classDef redclass fill:#EB4925 classDef yellowclass stroke:#EBAC25 classDef greenclass stroke:#C7EB25 C@{ shape: braces, label: "Together with environment variables " } A[Some environment variable values should never be exposed]:::greenclass -->|Example: Database access password| B[Use Secrets]:::redclass B -.- C

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.

  1. On GitHub, navigate to the main page of the repository.
  2. Under your repository name, click Settings. If you cannot see the “Settings” tab, select the- dropdown menu, then click Settings.

  1. In the “Security” section of the sidebar, select

  2. Secrets and variables, then click Actions.

  3. Click the Secrets tab.

  1. Click New repository secret.
  2. In the Name field, type a name for your secret.
  3. In the Secret field, enter the value for your secret.
  4. 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.

1
2
3
4
5
    env:
      MONGODB_CLUSTER_ADDRESS: cluster0.15pwqcc.mongodb.net
      MONGODB_USERNAME: ${{ secrets.MONGODB_USERNAME }}
      MONGODB_PASSWORD: ${{ secrets.MONGODB_PASSWORD }}
      PORT: 8080
It is worth noting that once secrets are saved in GitHub, they can no longer be exposed.

GitHub Deployment Environments #

You can create and deploy to different 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.

You can use environments to require approval for a job to proceed, restrict which branches can trigger a workflow, gate deployments with custom deployment protection rules, or limit access to secrets.

Referencing environments #

1
2
3
4
5
6
7
8
jobs:
  test:
    environment: testing
    env:
      MONGODB_CLUSTER_ADDRESS: cluster0.15pwqcc.mongodb.net
      MONGODB_USERNAME: ${{ secrets.MONGODB_USERNAME }}
      MONGODB_PASSWORD: ${{ secrets.MONGODB_PASSWORD }}
      PORT: 8080

More info:


» Sources « #

» Disclaimer « #

This series draws heavily from Maximilian Schwarzmüller’s GitHub Actions - The Complete Guide course on Udemy.

About the instructor:
🌐 Website📺 YouTube
💼 LinkedIn🗃️ GitHub
My Repos for this section:
cicd-gh-actions-courseLearnings from "GitHub Actions - The Complete Guide" on Udemy.

ℹ️Shared for educational purposes only, no rights reserved.