Set environment variable based on git branch

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}
< name: Build and deploy staging
> name: Build and deploy production
< - develop
> - master
< 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. :slight_smile:


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

1 Like

Did you try to use


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


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:

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

    - 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:

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
    - 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.


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.

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 :arrow_right: export ENVIRONMENT=“staging”
  • “master” branch :arrow_right: 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.

1 Like

Solution above works, but is being deprecated:

so it looks like this

    - name: Set env to staging
      if: endsWith(github.ref, '/develop')
      run: |
        echo "ENVIRONMENT=staging" >> $GITHUB_ENV
    - name: Set env to production
      if: endsWith(github.ref, '/master')
      run: |
       echo "ENVIRONMENT=production" >> $GITHUB_ENV
1 Like

If you use PowerShell then this should do the trick:

run: |
  echo "ENVIRONMENT=staging" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

In Windows cmd it should work like this:

run: |
  chcp 65001 > nul
  echo ENVIRONMENT=staging >> %GITHUB_ENV%