Skip to main content
How to Use ENV variables in GitHub Actions
4 min read

How to Use ENV variables in GitHub Actions

Introduction

Variables in GitHub Actions store reusable, nonsensitive information, such as usernames, paths, or configurations. A variable can be scoped to a single workflow or across multiple workflows to make it easy to maintain settings that might be different across various environments. Besides that, GitHub has set a few default environment variables that we can use in various of our actions, or we could define our own custom ones. We can also use contexts to fetch these variable values anywhere inside our workflows.

Why is it useful?

This approach simplifies configuration in our workflows. It would prevent us from hard-coding values and instead leverage the reusability of variables throughout steps or jobs in order to make the workflows easier to maintain and debug. We would then have more flexibility with regard to the maintenance of different environments, and it would also reduce the potential for errors created by manual entries.

Using ENV variables

Custom Variables

You may declare environment variables at a scope of workflow, job, or step. Here's how:

.github/workflows/env-variables.yml
name: Greeting on variable day
on:
workflow_dispatch

env:
DAY_OF_WEEK: Monday # Workflow level variable

jobs:
greeting_job:
runs-on: ubuntu-latest
env:
Greeting: Hello # Job level variable
steps:
- name: "Say Hello Mona it's Monday"
run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
env:
First_Name: Mona # Step level variable

Accessing Variables

The Variables can be accessed using Environment variables or using contexts in other parts of the workflow. It is possible for example to access as:

run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"

If you want to access them via contexts instead of directly as environment variables, use the following:

run: echo "${{ env.Greeting }} ${{ env.First_Name }}. Today is ${{ env.DAY_OF_WEEK }}!"

Security Concerns

Variables aren't masked in output so for passwords or API keys you should utilize GitHub Secrets. As an example, to access secrets you would normally do something like:

steps:
- name: Use secret
run: echo "Your secret is ${{ secrets.MY_SECRET }}"

Configuration Variables

These can be reused in multiple workflows using the organization, repository, or environment levels. They are referenced using the vars context.

on: workflow_dispatch
env:
env_var: ${{ vars.ENV_CONTEXT_VAR }} # Access configuration variables

jobs:
display-variables:
runs-on: ${{ vars.RUNNER }}
steps:
- name: Show variables
run: |
echo "Repo variable: $REPOSITORY_VAR"
echo "Org variable: $ORGANIZATION_VAR"
echo "Overridden variable: $OVERRIDE_VAR"
env:
REPOSITORY_VAR: ${{ vars.REPOSITORY_VAR }}
ORGANIZATION_VAR: ${{ vars.ORGANIZATION_VAR }}
OVERRIDE_VAR: ${{ vars.OVERRIDE_VAR }}

Passing Data Between Jobs

If there is a need to pass values across jobs or steps, then that's where you'll be using the job outputs. Here's how:

jobs:
job1:
runs-on: ubuntu-latest
steps:
- name: Generate value
id: step1
run: echo "::set-output name=my_var::Hello World"

job2:
needs: job1
runs-on: ubuntu-latest
steps:
- name: Use value from job1
run: echo "The value is ${{ steps.step1.outputs.my_var }}"

Setting ENV variables

Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable can't access the new value, but all subsequent actions in a job will have access. Environment variable names are case-sensitive and you can use punctuation.

- name: Set env
run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
- name: Test
run: echo $GITHUB_SHA_SHORT

Conclusion

Environment and configuration variables in GitHub Actions reduce the overhead of managing workflows and decrease complexity. Whether you have to use variables across multiple workflows or protect sensitive information by storing it with secrets, variables are a flexible, reusable solution that will minimize errors and improve maintainability.

Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring