How to use GitHub Actions environment variables

In this blog, we will explore how to use environment variables in GitHub Actions, including how to create and use them in your workflows.

Environment variables are an essential part of any software development process, and GitHub Actions makes it easy to manage them. Environment variables are values that are stored outside of your source code and used by your code at runtime. They can be used to store API keys, connection strings, or any other values that your code needs to run.

Creating environment variables

There are several ways to create environment variables in GitHub Actions. In this blog post I am going to show you three main ways to achieve this:

  • Environment variables that can be used throughout the GitHub Action workflow
  • Environment variables specific for a job
  • Environment variables specific for a step within a job

Environment variables that can be used throughout the GitHub Action workflow

Environment variables that can be used throughout the GitHub Action workflow

One way is to define them directly in your workflow file using the env keyword.

Lets look at defining this initially so any step/job within the workflow can use the variable

name: Example variables

on:
  push:
    branches:
    - main

env:
  example_value: Test-value1

jobs:
  display_values:
    runs-on: ubuntu-latest
    steps:
      - name: "Test to display example_value environment variable" 
        run: echo "example_value value is $example_value"

In this example, we are creating an environment variable called example_value and assigning it the value Test-value1. You can access them using the syntax $example_value within a workflow step.

Environment variables specific for a job

Sometimes you may require more granular variables that are only specific for a job within your workflow. Lets look at how to do this below:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      example_value: Test-value1

In this example, we are creating an environment variable called example_value and assigning it the value Test-value1. You can access them using the syntax $example_value1 within the specific job build, such as:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      example_value: Test-value1
    steps:
      - name: "Test to display example_value environment variable" 
        run: echo "example_value value is $example_value"

Environment variables specific for a step within a job

Maybe an even more fine grained requirement than the above, lets finish with looking at creating environment variables specific for a step within a job

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: "Echo" 
        run: echo "Example of an echo"
      - name: "Test to display example_value environment variable" 
        run: echo "example_value value is $example_value"
        env:
          example_value: Test-value1

In the above, running two steps in the build job, we can see that the variable can only be accessed in the second step.

Environment variables are an essential part of any software development process, and GitHub Actions makes it easy to manage them with so many options available, hopefully this blog post has assisted you and given you an indication into what is possible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s