How to Create Dynamic Environment Variables in GitHub Actions

Creating dynamic environment variables in GitHub Actions are super useful. I have been doing this recently on a few workflows and thought I’d share a quick guide on how to set up dynamic environment variables with some practical examples.

Using $GITHUB_ENV to Create Dynamic Environment Variables

$GITHUB_ENV is a file path used by GitHub Actions. It allows you to create or even update environment variables during a workflow run. When you write key-value pairs to this file, GitHub Actions automatically exports those as environment variables.

An example of how this works, the below step creates a variable $COLOUR:

steps:
  - name: Setting colour Variable
    run: echo "COLOUR=RED" >> $GITHUB_ENV

A Basic Example of Creating Dynamic Environment Variables

Lets look at the below example with two steps:

name: Dynamic Environment Variables

on:
  pull_request:
    branches:
      - main

jobs:
  dynamic-variables-1:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Set Colour
        run: |
          COLOUR="blue"
          echo "COLOUR=$COLOUR" >> $GITHUB_ENV

      - name: Echo Colour
        run: |
          echo $COLOUR

Breaking it down:

  1. Setting the variable in step Set Colour , we can see that its writing to $GITHUB_ENV with colour is blue
  2. Using the variable in step Echo Colour
Screenshot of GitHub Action workflow, highlighting the step echo colour is returning Blue

Github repo containing above workflow

Use Cases for Dynamic Environment Variables

Now that we have covered a basic example, there is lots of potential use cases for creating dynamic environment variables within your GitHub Actions:

1. Conditional Environment Variable Setups

A popular use case is the ability to configure environment environments depending on the environment you are using.

name: Dynamic Environment Variables

on:
  pull_request:
    branches:
      - main

jobs:
  dynamic-variables-2:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set environment variables
        run: |
          if [[ ${{ github.ref }} == 'refs/heads/main' ]]; then
            echo "ENVIRONMENT_LOCATION=uksouth" >> $GITHUB_ENV
            echo "ENVIRONMENT_URL=https://thomasthornton.cloud" >> $GITHUB_ENV
          elif [[ ${{ github.ref }} == 'refs/heads/staging' ]]; then
            echo "ENVIRONMENT_LOCATION=ukwest" >> $GITHUB_ENV
            echo "ENVIRONMENT_URL=https://staging.thomasthornton.cloud" >> $GITHUB_ENV
          else
            echo "ENVIRONMENT_LOCATION=westeurope" >> $GITHUB_ENV
            echo "ENVIRONMENT_URL=https://dev.thomasthornton.cloud" >> $GITHUB_ENV
          fi

      - name: Deploy environment variables
        run: |
          echo "Deploying to $ENVIRONMENT_URL in location: $ENVIRONMENT_LOCATION"

In this example, the environment variables $ENVIRONMENT_LOCATION & $ENVIRONMENT_URL are set depending on which branch the workflow is running.

In the below, its running on branch: pr-example

Screenshot of GitHub Action workflow, highlighting the step environment variables output when branch is called pr-example

Github repo containing above workflow

2. Setting the date

Setting variable for date could be useful to use throughout your stages, for tracking when a particular workflow was ran or a timestamp for additional logging

name: Dynamic Environment Variables

on:
  pull_request:
    branches:
      - main

jobs:
  dynamic-variables-3:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set date
        run: |
          DATE=$(date +'%Y%m%d')
          echo "DATE=$DATE" >> $GITHUB_ENV

      - name: Echo Date
        run: |
          echo "The date this run was created was $DATE"

Screenshot from GitHub workflow showing output of echo date

In this workflow, the $DATE variable captures the current date in YYYYMMDD format and makes it available for use throughout the workflow.

Github repo containing above workflow

3. Using Multi-line variables

In some cases, you might need to set multi-line variables, such as when dealing with configuration files. Here’s an example:

name: Dynamic Environment Variables

on:
  pull_request:
    branches:
      - main

jobs:
  dynamic-variables-4:
  config:
    runs-on: ubuntu-latest
    steps:
      - name: Set multi-line variable
        run: |
          EOF=$(uuidgen)
          echo "CONFIG_EXAMPLE<<$EOF" >> $GITHUB_ENV
          echo "blog_name: thomasthornton.cloud" >> $GITHUB_ENV
          echo "sql_connections: 10000" >> $GITHUB_ENV
          echo "timeout: 60s" >> $GITHUB_ENV
          echo "$EOF" >> $GITHUB_ENV

      - name: Echo multi-line variable
        run: |
          echo "$CONFIG_EXAMPLE"

From the output below, we can see the multi-line variable has been outputted correctly:

Screenshot from GitHub workflow showing output of multi-line variable

GitHub repo containing above workflow

Dynamic environment variables are incredibly useful in GitHub Actions. They can simplify workflows, make configurations flexible, and provide greater control over how your pipeline runs. Whether you’re setting conditional variables, adding timestamps, or managing multi-line configurations, dynamic environment variables give you the power to automate effectively.

I do recommend start experimenting with dynamic environment variables in your workflows and see how they can be useful as part of your CI/CD pipelines.

Leave a Reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading