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

Microsoft Teams IP Phones and Intune Enrollment

Microsoft Teams provides a growing portfolio of devices that can be used as desk and conference room phones. These IP phones run on Android 8.x or 9.x and are required to be enrolled in Intune. By default, these devices are enrolled as personal devices, which is not ideal as users should not be able to enrol their own personal Android devices.

Read More »

Level 9, 360 Collins Street, 
Melbourne VIC 3000

Level 2, 24 Campbell St,
Sydney NSW 2000

200 Adelaide St,
Brisbane QLD 4000

191 St Georges Terrace
Perth WA 6000

Level 10, 41 Shortland Street

Part of

Arinco trades as Arinco (VIC) Pty Ltd and Arinco (NSW) Pty Ltd. © 2023 All Rights Reserved Arinco™ | Privacy Policy | Sustainability and Our Community
Arinco acknowledges the Traditional Owners of the land on which our offices are situated, and pay our respects to their Elders past, present and emerging.

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.