I would like to declare some environment variables in a top level env section in my main.yml whose values use some pre-defined environment variables such as those documented in the GitHub Actions documentation. However, it appears I cannot use those pre-defined variables in the right hand side of my env section. For example:
env:
resourceGroup: ${GITHUB_RUN_ID}${GITHUB_RUN_NUMBER}
Is there a way to make it so any step that needs ${resourceGroup} can get it without having to manually define it within each step?
You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of your Compose file). Values set in the shell environment override those set in the .env file.
To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.
If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output. You can then reference this job output from a step in another job.
Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.
I tried the following two ways.
env:
resourceGroup1: ${GITHUB_RUN_ID}-${GITHUB_RUN_NUMBER}
resourceGroup2: ${{ github.run_id }}-${{ github.run_number }}
jobs:
foo:
runs-on: ubuntu-latest
steps:
- name: test1
run: echo ${{ env.resourceGroup1 }}
- name: test2
run: echo ${{ env.resourceGroup2 }}
In both cases, the results were obtained correctly.
However, in the case of env as a result, the former has not yet been evaluated. Maybe you can use the latter.
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