In GitHub Workflow I'm looking for a build-in variable which would contain the name of the current GH deployment environment (e.g. "staging") or be blank/unset if no such thing exist.
Since there doesn't seem to be a build-in variable for it (perhaps I'm overlooking it?), is there another way that this value can be obtained from within a pipeline step?
Here's an example workflow:
jobs:
job1:
runs-on: ubuntu-latest
environment: uat
steps:
- name: 'Print name'
run: echo "GH deployment env : ..."
job2:
runs-on: ubuntu-latest
environment: staging
steps:
- name: 'Print name'
run: echo "GH deployment env : ..."
Here is a simple idea:
You can use Configuration Variables at the level of the Environment. These can then be used with the ${{ vars.XXXX }} in your workflow file.
The idea is that for each of your GitHub environments, you define variable with the name of that environment. Obviously this is redundant information. Yet, it is not that much. It is just a simple var that you need to make sure exists for your environment.
Here is an example of such a definition for a GitHub environment named uat.

With that example, you can then reference such variable anywhere in GitHub workflow with this syntax
${{ vars.DEPLOYMENT_ENVIRONMENT_NAME }}.
The variable's value will take flavor of the current deployment environment. Since these variables are hierarchical, you can define the same variable at the level of the Repository for example with a value of
UNDEFINED - a 'DEPLOYMENT_ENVIRONMENT_NAME' variable must exist for the current GitHub environment, it doesn't and you need to fix that!!.
.. so that you can easily see that you've missed to setup something.
And then a note to the GitHub folks: Obviously the word "environment" and certainly the word "environment variable" is extremely overloaded. I wish your documentation would be more precise in this respect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With