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

廉价感情. 提交于 2020-04-16 05:14:33

问题


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?


回答1:


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.




回答2:


Yes, you can. I built a GitHub Action that will do it for you: Add Env vars.

Use it as the first step in a job in your workflow, and pass in JSON-stringified env vars as the map parameter. They need to be set for each job - they will only be set for all subsequent steps in a job.

Here is your test case, using the Add Env vars:

  test:  
    runs-on: ubuntu-latest
    steps:
      - name: Setup env
        uses: jwulf/add-env-vars-action@master
        with:
          map: '{"resourceGroup1": "${{ github.run_id }}-${{ github.run_number }}", "resourceGroup2": "${{ github.run_id }}-${{ github.run_number }}"}'   
      - name: test1
        run: echo ${{ env.resourceGroup1 }}
      - name: test2
        run: echo ${{ env.resourceGroup2 }}



回答3:


It looks like set-env works as well: https://help.github.com/en/actions/reference/development-tools-for-github-actions#set-an-environment-variable-set-env



来源:https://stackoverflow.com/questions/60347162/github-actions-env-use-pre-defined-environment-variables-on-rhs-within-env-sec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!