How to reference context values in GitHub Actions expression syntax?

倾然丶 夕夏残阳落幕 提交于 2021-02-04 18:18:05

问题


I want to set an environment variable in the env: section of a GitHub Action and make use of the Contexts and expression syntax for GitHub Actions. I tried this:

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      MYVAR: ${{ format('{0}:{1}', ${{ env.PATH }}, ${{ env.HOME }} ) }}

    steps:
    - name: Check environment
      run: echo $MYVAR

This results in the error message:

### ERRORED 10:45:52Z

- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 10, Col: 14): Unexpected symbol: '${{'. Located at position 19 within expression: format('{0}:{1}', ${{ env.PATH

This syntax:

    env:
      MYVAR: ${{ format('{0}:{1}', {{ env.PATH }}, {{ env.HOME }} ) }}

results in error:

### ERRORED 13:14:18Z

- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 10, Col: 14): Unexpected symbol: '{{'. Located at position 19 within expression: format('{0}:{1}', {{ env.PATH

and:

    env:
      MYVAR: ${{ format('{0}:{1}', env.PATH, env.HOME ) }}

results in error:

### ERRORED 13:16:12Z

- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 10, Col: 14): Unrecognized named-value: 'env'. Located at position 19 within expression: format('{0}:{1}', env.PATH, env.HOME )

I'm aware of the solutions in How do i set an env var with a bash expression in GitHub Actions? and Github Actions, how to share a calculated value between job steps? for setting environment variables, but I would like to understand the expression syntax.


回答1:


As @jonrsharpe pointed out, it is not possible to use the env context in the value of a workflow environment variable. This was discussed here:

https://github.community/t5/GitHub-Actions/How-to-use-env-context/td-p/38951




回答2:


What you are trying to do is quite possible, just don't use the ${{ inside an existing context. Do this instead:

According to the documentation on the env context:

You can only use the env context in the value of the with and name keys, or in a step's if conditional. For more information on the step syntax, see "Workflow syntax for GitHub Actions."



来源:https://stackoverflow.com/questions/59809764/how-to-reference-context-values-in-github-actions-expression-syntax

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