Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 6

Set environment variable based on git branch

Solved! Go to Solution.

Hi community,

 

I'm sorry if this has been asked before, but I did not find anything useful.

 

My situation is like this: I have a `develop` branch that corresponds to the staging env and a `master` branch that corresponds to the production env. Each time I push to one of the branches, GH actions runs to build/test/deploy. This works fine.

 

My problem is, that the workflow files for develop and master are very duplicated, see:

➜ diff .github/workflows/{develop.yaml,master.yaml}
1c1
< name: Build and deploy staging
---
> name: Build and deploy production
5c5
< - develop
---
> - master
14c14
< ENVIRONMENT: staging
---
> ENVIRONMENT: production

 

The only things that differ are:

  • the workflow name
  • the branch name (develop/master)
  • the value of the ENVIRONMENT variable (staging/production)

 

I'd really like to consolidate this and have 1 file only for both staging & production. So is there a way to run the workflow on pushes to both develop & master branches? And to set an env var based on the branch that is pushed to?

 

Are there other ways to consolidate this? Many thanks for any suggestions. :)

 

5 Replies
Highlighted
Copilot Lvl 3
Message 2 of 6

Re: Set environment variable based on git branch

Have you considered just rendering the yaml files using a template and a small script? then whenever you make changes to the workflow you do it to the template and rerender

Ground Controller Lvl 1
Message 3 of 6

Re: Set environment variable based on git branch

Did you try to use

${GITHUB_REF##*/}

This should extract the branch part from "GITHUB_REF" env variable which is usually in form of "refs/heads/branch-name".

Solution
Copilot Lvl 2
Message 4 of 6

Re: Set environment variable based on git branch

Thank you @bomb-on and @ssboisen, I just took another look at the docs and found a syntax to set ENV vars that persist between steps:

 

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-fo...

 

This is what I did to set the ENVIRONMENT variable automatically:

 

  steps:
    - name: Set env to staging
      if: endsWith(github.ref, '/develop')
      run: |
        echo "::set-env name=ENVIRONMENT::staging"
    - name: Set env to production
      if: endsWith(github.ref, '/master')
      run: |
        echo "::set-env name=ENVIRONMENT::production"

It's kind of a simple mapping between branches and environment names and works in this case as we have only 2. When you run it, it looks like this:

 

Bildschirmfoto 2019-12-03 um 20.16.19.png

See that the production step is skipped, as I was pushing to the "develop" branch. With this var set, everything else falls into place.

 

At the top of my workflow file I have the following now:

 

name: Build and deploy
on:
  push:
    branches:
    - develop
    - master

I am happy now as I have to maintain 1 workflow file only and don't have to remember to run a script when I change something. Thank you again for supporting me with your ideas.

Copilot Lvl 2
Message 5 of 6

Re: Set environment variable based on git branch

Try to combine @bomb-on suggestion with yours and you won’t even have to think about the branch name because it will be resolved from GITHUB_REF variable. Keep in mind just that my suggestion might not work with Git Flow’s “feature” or “release” and similar branches.
Copilot Lvl 2
Message 6 of 6

Re: Set environment variable based on git branch

Hi @mklappir, I'm doing the same thing pretty much already, as I'm reading from github.ref, which is pretty much the same as GITHUB_REF. Extracting the part after the last slash is a neat trick, but the endsWith macro serves me just as well.

 

Extracting the current branch name was just part of the problem, but my main concern was how to map the branch name (develop, master) to the environment name (staging, production) and save that part in an environment variable that persists between steps. So I need:

 

  • "develop" branch ➡️ export ENVIRONMENT="staging"
  • "master" branch ➡️ export ENVIRONMENT="production"

 

I agree that in an ideal world, I would just rename the environments according to the branch name, but that would require a lot of work on other ends which I want to avoid, especially now that I have a solution that works for me.