Share on facebook
Share on twitter
Share on linkedin
Share on email

GitHub Actions share environment variables across workflows

Recently I have been working with GitHub Actions and needed to be able to share environment variables across different GitHub workflows, to which I discovered that this feature doesn’t currently exist. As an alternative you can set variables as secrets in your GitHub repository, although that is quite manual and doesn’t help if you would like to manage variables in code.

We can solve this by storing our environment variables in a .env file and utilise a GitHub Action to configure variables for our GitHub workflows.

A GitHub Action has been published on the GitHub Actions Marketplace (available here) to help you achieve this or alternatively if you would like to utilise your own custom GitHub Action, follow the steps below.

1. Create a custom GitHub Action to set environment variables based off a file


name: 'Set environment variables'
description: 'Configures environment variables for a workflow'
    description: 'File path to variable file or directory. Defaults to ./.github/variables/* if none specified and runs against each file in that directory.'
    required: false
    default: ./.github/variables/*
  using: "composite"
    - run: |
        sed "" ${{ inputs.varFilePath }} >> $GITHUB_ENV
      shell: bash

2. Create .env file containing your variables and values



3. Add step in your GitHub Workflow to set environment variables


    if: ${{ github.ref == 'refs/heads/master' }}
    needs: build
    runs-on: ubuntu-latest
      - name: Set Environment Variables
        uses: ./.github/actions/setvars
          varFilePath: ./.github/variables/myvars.env

That’s it! Our GitHub Workflow will now use variables from our .env file which we can manage in code and reuse in other GitHub Workflows.

For reference my repo structure is as follows.

├── .github/
|  ├── actions/
|  |  ├── setvars/
|  |  |  ├── action.yml
|  ├── variables/
|  |  ├── myvars.env
|  ├── workflows/
|  |  ├── myworkflow.yml
[mailpoet_form id="1"]

Other Recent Blogs

Arinco trades as Arinco (VIC) Pty Ltd
and Arinco (NSW) Pty Ltd

All Rights Reserved


Level 9, 360 Collins Street, 
Melbourne VIC 3000

Level 3, 19 Bridge Street
Sydney NSW 2000

Get started on the right path to cloud success today. Our Crew are standing by to answer your questions and get you up and running.