Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions: env: Use pre-defined environment variables on RHS within env section

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?

like image 624
edburns Avatar asked Feb 21 '20 22:02

edburns


People also ask

Can I use variables in .env file?

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.

How do I set environment variables in GitHub Actions?

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.

How do I pass variables from one job to another in GitHub Actions?

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.

Should you use .env in Production?

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.


1 Answers

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.

enter image description here

However, in the case of env as a result, the former has not yet been evaluated. Maybe you can use the latter.

enter image description here

like image 181
banyan Avatar answered Oct 05 '22 15:10

banyan