Blogs

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

action.yml

name: 'Set environment variables'
description: 'Configures environment variables for a workflow'
inputs:
  varFilePath:
    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/*
runs:
  using: "composite"
  steps:
    - run: |
        sed "" ${{ inputs.varFilePath }} >> $GITHUB_ENV
      shell: bash

2. Create .env file containing your variables and values

myvars.env

MYVAR1=value1
MYVAR2=value2

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

myworkflow.yml

  deploy:
    if: ${{ github.ref == 'refs/heads/master' }}
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Set Environment Variables
        uses: ./.github/actions/setvars
        with:
          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

Subscribe

Other Recent Blogs

Tech Consulting: Lessons learned and defining a career path

Being a Consultant is one of those titles now that embodies so much. Generally speaking, a Tech Consultant is an experienced technical specialist who implements solutions and provides technological advice to drive positive outcomes for clients and help transform their business. But what does it take to be one? We spoke to our very own, Michael Turnley, and got his insights into the world of Tech Consulting and how he got there.

Read More »

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

All Rights Reserved

 

Level 17, 303 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.