将环境变量输入传递给可重用工作流程

tak*_*are 16 github-actions

我正在尝试从另一个工作流程调用可重用工作流程,并向其传递一些输入变量。在调用者工作流程中,我有一些环境变量,我想将它们作为输入传递给可重用的工作流程,如下所示:

env:
  SOME_VAR: bla_bla_bla
  ANOTHER_VAR: stuff_stuff

jobs:
  print:
    runs-on: ubuntu-latest
    steps:
      - name: Print inputs passed to the reusable workflow
        run: |
          echo "some var: $SOME_VAR"
          echo "another var: $ANOTHER_VAR"
  call_reusable:
    uses: ...
    with:
      input_var: $SOME_VAR
      another_input_var: $ANOTHER_VAR
Run Code Online (Sandbox Code Playgroud)

可重用的工作流程:

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      input_var:
        required: true
        type: string
      another_input_var:
        required: true
        type: string

jobs:
  the_job:
    runs-on: ubuntu-latest
    steps:
      - name: Print inputs
        run: |
          echo "input_var: ${{ inputs.input_var }}"
          echo "another_input_var: ${{ inputs.another_input_var }}"
Run Code Online (Sandbox Code Playgroud)

Print inputs passed to the reusable workflow步骤工作正常 - 所有变量都正确打印。但是,Print inputs可重用工作流(被调用者)中的步骤未按预期工作 - 所有变量均为空。

我在文档中找不到任何表明我的方法有问题的内容,因此,在我看来,这应该有效。尽管如此,查看日志还是有问题,正如在可重用工作流程(被调用者)中我可以看到:

Run echo "input_var: $SOME_VAR"
  echo "another_input_var: $ANOTHER_VAR"
  shell: /usr/bin/bash -e {0}
input_var: 
another_input_var: 
Run Code Online (Sandbox Code Playgroud)

with:我尝试将块中的值包装起来,$(echo)但这不起作用。

有任何想法吗?

Gui*_*urd 25

经过一些研究,我发现这个线程解释了:

\n
\n

您可以\xe2\x80\x99t 将 ENV 变量传递给可重用工作流程,因此它们在此模式中几乎没有用处。

\n
\n

此外,官方文档中还指出:

\n
\n

env在调用者工作流中的工作流级别定义的上下文中设置的任何环境变量都不会传播到被调用的工作流

\n
\n

因此,在您的情况下,您将无法直接使用env变量来实现您想要的效果,但有一些解决方法

\n

注意:如果 GitHub 能提出一种更好的方法来内联分配值或将它们传递到可重用工作流程中,那就太好了,因为多次处理每个参数只是为了将其传递到可重用工作流程中是很麻烦的。

\n
\n

解决方法可能是使用第一个作业的输出,并将这些输出用作可重用的工作流输入。

\n

以下是如何完成此操作的示例:

\n
env:\n  SOME_VAR: bla_bla_bla\n  ANOTHER_VAR: stuff_stuff\n\njobs:\n  print:\n    runs-on: ubuntu-latest\n    outputs:\n      some_var: ${{ steps.step1.outputs.some_var }}\n      another_var: ${{ steps.step1.outputs.another_var }}   \n    steps:\n      - name: Print inputs passed to the reusable workflow\n        id: step1\n        run: |\n          echo "some var: $SOME_VAR"\n          echo "some_var=$SOME_VAR" >> $GITHUB_OUTPUT\n          echo "another var: $ANOTHER_VAR"\n          echo "another_var=$ANOTHER_VAR" >> $GITHUB_OUTPUT\n  \n  call_reusable:\n    needs:\n      - print\n    uses: ...\n    with:\n      input_var: ${{ needs.print.outputs.some_var }}\n      another_input_var: ${{ needs.print.outputs.another_var }}\n
Run Code Online (Sandbox Code Playgroud)\n

编辑:删除了::set-output现已弃用的语法。

\n

这样,在不更新可重用工作流实现的情况下,输入将填充预期值。

\n

以下是我用来测试的工作流程:主要+可重用

\n

您可以在此处检查工作流程运行的预期结果。

\n

  • 对于实现如此简单的操作,此解决方法极其复杂。此外,它无法用于传递机密,因为它们会自动从输出中删除。 (4认同)
  • 在**可重用工作流程**中,您可以[继承秘密](https://docs.github.com/pt/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idsecretsinherit),或者您必须[无论如何将它们作为秘密发送到工作流程](https://docs.github.com/pt/actions/using-workflows/reusing-workflows#using-inputs-and-secrets-in-a-reusable-workflow)。因此,此解决方法仅在这种情况下对于输入和环境有意义,而对于秘密则不有意义。 (3认同)

Sid*_*Sid 5

最近 GitHub 推出了一项支持环境变量的功能(我必须说,这是非常基本且期待已久的功能)。
因此,其他解决方案可能是在 GitHub 中创建一个环境,在其下使用所需值创建一些变量,并根据需要使用该环境。

呼叫者工作流程:

jobs:
  Dev-Deployment:
    uses: ./.github/workflows/reusable-workflow.yml
    with:
        environment: Development

  QA-Deployment:
    uses: ./.github/workflows/reusable-workflow.yml
    with:
        environment: QA
Run Code Online (Sandbox Code Playgroud)

可重复使用的工作流程:

on:
 workflow_call:
  inputs:
    environment:
      type: string
      required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
    steps:
      - name: Print variables - ${{ vars.SOME_VAR }} - ${{ vars.ANOTHER_VAR }}
        run: |
            echo ${{ vars.SOME_VAR }}
            echo ${{ vars.ANOTHER_VAR }}
Run Code Online (Sandbox Code Playgroud)

GitHub环境:

在此输入图像描述