Github Workflows: 在矩阵作业中更改或指定环境变量

huangapple go评论46阅读模式
英文:

Github Workflows: Changing or specifying environment variables in matrix jobs

问题

如何在作业矩阵的一部分指定或更改环境变量?

例如,这里我希望我的作业输出“Two”和“Three”,而不是“One”,但是 GitHub 完全忽略了在矩阵中定义或更改环境变量:

英文:

How do I specify or change an environment variable as part of a job matrix?

For example, here I want my job to echo "Two" and "Three" instead of "One", but github completely ignores the definition or change of the environment variable in the matrix:

name: test-test-test

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]

env:
  MY_VAR: One

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - env:
              MY_VAR: Two
          - env:
              MY_VAR: Three
    steps:
      - run: echo "$MY_VAR2"

yields echo "One" and echo "One" in both jobs.

答案1

得分: 2

使用此上下文访问已在工作流步骤作业中设置的环境变量信息。请注意,您不能在步骤iduses键的值中使用env上下文。示例是env.env_var

GitHub提供了关于上下文的详细文档,网址为
https://docs.github.com/en/actions/learn-github-actions/contexts

英文:

Use this context to access information about environment variables that have been set in a workflow, step, or job. Note that you cannot use the env context in the value of the id and uses keys within a step. An example is env.env_var.

GitHub provides extensive documentation about contexts at
https://docs.github.com/en/actions/learn-github-actions/contexts.

答案2

得分: 2

你使用的语法在矩阵中不起作用。正如官方文档中所述:

你定义的变量成为matrix上下文中的属性,你可以在工作流文件的其他区域引用这些属性。

在你的情况下,要访问在include字段列表中设置的矩阵env变量,你需要使用${{ matrix.env.MY_VAR }}

类似这样:

env:
  MY_VAR: One

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - env:
              MY_VAR: Two
          - env:
              MY_VAR: Three
    steps:
      - run: |
          echo "$MY_VAR" # 等同于 ${{ env.MY_VAR }}
          echo ${{ matrix.env.MY_VAR }}          

这将生成2个test任务:test (Two)test (Three)

我在这里做了一个测试,如果你想要查看。


结论:

基本上,你在工作流级别设置的env与矩阵策略中设置的env不是同一个,所以你无法在那里替换该值。

替代方案

可能你可以像@Adam在另一个答案中所说的那样,将env的值在工作流级别设置在environment上下文中以使其生效(文档中的参考链接 + 另一个工作流示例)。

例如:

env:
  WORKFLOW_VARIABLE: WORKFLOW

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      JOB_VARIABLE: JOB
    steps:
      - name: Example
        env:
          STEP_VARIABLE: STEP
        run: |
          echo "Hello World"
          echo "This is the $WORKFLOW_VARIABLE environment variable"
          echo "This is the $JOB_VARIABLE environment variable"
          echo "This is the $STEP_VARIABLE environment variable"          

但根据你计划实现的内容,矩阵策略可能更有效率。

英文:

The syntax you used won't work for matrix. As stated in the official documentation:

> The variables that you define become properties in the matrix context, and you can reference the property in other areas of your workflow file.

In your case, to access the matrix env variable you set in the include field list, you would need to use ${{ matrix.env.MY_VAR }}.

Something like this:

env:
  MY_VAR: One

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - env:
              MY_VAR: Two
          - env:
              MY_VAR: Three
    steps:
      - run: |
          echo "$MY_VAR" # is equal to ${{ env.MY_VAR }}
          echo ${{ matrix.env.MY_VAR }}          

This will generate 2 test jobs: test (Two) and test (Three).

I made a test here if you want to have a look.


Conclusion:

Basically, the env you set at the workflow level isn't the same env set in the matrix strategy, so you can't substitute the value there.

Alternative

What you could do eventually (as @Adam stated in the other answer) is to set the env value at the workflow level for it to work, in the environment context. (Reference in the documentation + another workflow example).

Example:

env:
  WORKFLOW_VARIABLE: WORKFLOW

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      JOB_VARIABLE: JOB
    steps:
      - name: Example
        env:
          STEP_VARIABLE: STEP
        run: |
          echo "Hello World"
          echo "This is the $WORKFLOW_VARIABLE environment variable"
          echo "This is the $JOB_VARIABLE environment variable"
          echo "This is the $STEP_VARIABLE environment variable"

But depending on what you plan to achieve, a matrix strategy you be more efficient.

huangapple
  • 本文由 发表于 2023年2月14日 19:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定