Set environment variable based on git branch #25301
-
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 My problem is, that the workflow files for develop and master are very duplicated, see:
The only things that differ are:
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: |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
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 |
Beta Was this translation helpful? Give feedback.
-
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”. |
Beta Was this translation helpful? Give feedback.
-
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:
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:
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. |
Beta Was this translation helpful? Give feedback.
-
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:
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. |
Beta Was this translation helpful? Give feedback.
-
Solution above works, but is being deprecated: GitHub Actions: Deprecating set-env and add-path commands - GitHub ChangelogGitHub Actions: Deprecating set-env and add-path commands so it looks like this
|
Beta Was this translation helpful? Give feedback.
-
If you use PowerShell then this should do the trick:
In Windows
|
Beta Was this translation helpful? Give feedback.
-
This is how I did it to get around updating the environment being deprecated.
|
Beta Was this translation helpful? Give feedback.
-
FWIW, ref_name appears to be a recent addition. It was certainly not available at the time when endsWith was suggested. |
Beta Was this translation helpful? Give feedback.
Solution above works, but is being deprecated:
The GitHub BlogGitHub Actions: Deprecating set-env and add-path commands - GitHub Changelog
GitHub Actions: Deprecating set-env and add-path commands
so it looks like this