github操作的参数化`ons-on“ on”值

发布于 2025-02-07 15:26:23 字数 950 浏览 2 评论 0 原文

我有这个GitHub工作流程,需要在哪些跑步者运行的情况下进行参数化。因此,在YAML文件中,我尝试了:

# ... 
jobs: 
  process:
    name: Process
    runs-on: ${{ secrets.GH_RUNNER_TAG }}
# ...

但是,我会收到此错误:

工作流程无效。 。位于表达式中的位置1:secrets.gh_runner_tag

该元素不可用的秘密注入吗?还有其他选择吗?该值不需要是秘密,但是我需要在一个地方将其放在一个地方,而不是每次跑步者标签都会更改数百个YAML文件...


edit1: 我尝试过,如所建议的 guifalourd ,在工作流级别上创建一个环境变量,该变量可以容纳秘密:

env:
  RUNNER_LABEL: ${{ secrets.GH_RUNNER_TAG }}

jobs:
  analyze:
    name: Analyze
    runs-on: $RUNNER_LABEL

并且它不起作用。动作被卡住了。我尝试使用:

$ runner_label->被卡住了 “ $ runner_label” - >也被卡住了 $ {env.runner_label}} - >操作没有启动,输出错误:

工作流程无效。 。位于表达式中的位置1:env.runner_label

此外,我已经检查了通过在运行设置的有效的,硬编码的值和设置第一步AS:

steps:
  - name: Test
    run: echo "$RUNNER_LABEL"

产生“ ***” - 证明GitHub已自动输出并自动编辑了一个秘密。

I have this GitHub workflow that I need to parameterize on which runners runs. So in the YAML file I tried:

# ... 
jobs: 
  process:
    name: Process
    runs-on: ${{ secrets.GH_RUNNER_TAG }}
# ...

However, I get this error:

The workflow is not valid. .github/workflows/action.yml (Line: 12, Col: 14): Unrecognized named-value: 'secrets'. Located at position 1 within expression: secrets.GH_RUNNER_TAG

Is the secrets injection not available for this element? Is there some other alternative? The value does not need to be a secret but I need to have it in one place and not edit hundreds of YAML files everytime the runner tag would change...


EDIT1:
I've tried, as GuiFalourd suggested, to create an environment variable at the workflow level which would hold the secret:

env:
  RUNNER_LABEL: ${{ secrets.GH_RUNNER_TAG }}

jobs:
  analyze:
    name: Analyze
    runs-on: $RUNNER_LABEL

And it doesn't work. The action gets stuck. I tried using:

$RUNNER_LABEL -> gets stuck
"$RUNNER_LABEL" -> gets stuck, too
${{ env.RUNNER_LABEL }} -> action does not start, outputs error:

The workflow is not valid. .github/workflows/action.yml (Line: 14, Col: 14): Unrecognized named-value: 'env'. Located at position 1 within expression: env.RUNNER_LABEL

Furthermore, I've checked that the env var is properly assigned, by placing a valid, hard-coded value for runs-on and setting first step as:

steps:
  - name: Test
    run: echo "$RUNNER_LABEL"

which produces "***" - proof that a secret has been output and redacted automatically by GitHub.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2025-02-14 15:26:23

这是可以使用可重复使用的工作流来实现的接受呼叫者的输入。

我们可以将其命名为“过程”的主要管道将使用共享代码库/管道,让我们称其为“通用”,可以接受输入,其中一个输入可以是 runs-on-On-On-On 值。

例如

# common.yml
name: parameterized job
on:
  workflow_call:
    inputs:
      runner:
        required: true
        type: string
jobs:
  common:
    name: Common
    runs-on: ${{ inputs.runner }}
    steps:
      - run: echo "Hello World"
# process.yml
name: process
on:
  push:

jobs:
  process:
    uses: username/repo/.github/workflows/common.yml@branch
    with:
      runner: machine_with_specific_label # using ${{ env.MY_RUNNER_LABEL }} is possible as well


This is achievable using Reusable Workflow by configuring the "called" workflow to accept inputs from the caller.

The main pipeline which we can name it as "process" will use a shared codebase/pipeline lets call it "common" which can accept inputs, one of these inputs can be the runs-on value.

For example

# common.yml
name: parameterized job
on:
  workflow_call:
    inputs:
      runner:
        required: true
        type: string
jobs:
  common:
    name: Common
    runs-on: ${{ inputs.runner }}
    steps:
      - run: echo "Hello World"
# process.yml
name: process
on:
  push:

jobs:
  process:
    uses: username/repo/.github/workflows/common.yml@branch
    with:
      runner: machine_with_specific_label # using ${{ env.MY_RUNNER_LABEL }} is possible as well


ぃ双果 2025-02-14 15:26:23

在不使用参数化工作流的情况下找到了替代方案。这在使用 vars 的同时可以直接如示例 不。

  1. 在您的工作流程中,创建一个先决条件的作业,该作业在托管跑步者上获取工作流程输入,并将跑步者名称设置为其输出。
  2. 使用先决条件的输出在后续作业上设置跑步者名称。

最终的工作流将看起来像这样:

name: Example Workflow

on:
  workflow_dispatch:
    inputs:
      runner:
        required: false
        description: 'Runner name'
        default: 'dev-runner'

jobs:
  prereq:
    runs-on: ubuntu-latest
    steps:
      - name: Provision step # at least one step is required in a job
        run: |
          echo "Provisioning ${{ github.event.inputs.runner }}"
    outputs:
      runner: ${{ github.event.inputs.runner }}
  actualjob:
    needs: prereq
    runs-on: ${{ needs.prereq.outputs.runner }}
    steps:
      - name: Ta-da
        run: echo "success!"

Found an alternative without using parameterized workflows. This works while using vars directly as documented in the example does not.

  1. In your workflow, create a prerequisite job that takes a workflow input on a hosted runner and sets the runner name as its output.
  2. Use the output from the prerequisite job to set the runner name on subsequent jobs.

The final workflow will look something like this:

name: Example Workflow

on:
  workflow_dispatch:
    inputs:
      runner:
        required: false
        description: 'Runner name'
        default: 'dev-runner'

jobs:
  prereq:
    runs-on: ubuntu-latest
    steps:
      - name: Provision step # at least one step is required in a job
        run: |
          echo "Provisioning ${{ github.event.inputs.runner }}"
    outputs:
      runner: ${{ github.event.inputs.runner }}
  actualjob:
    needs: prereq
    runs-on: ${{ needs.prereq.outputs.runner }}
    steps:
      - name: Ta-da
        run: echo "success!"
墨落成白 2025-02-14 15:26:23

尝试以下操作:

env:
  RUNNER_LABEL: ${{ secrets.GH_RUNNER_TAG }}

jobs:
  analyze:
    name: Analyze
    runs-on: ${{ env.RUNNER_LABEL }}

Try this :

env:
  RUNNER_LABEL: ${{ secrets.GH_RUNNER_TAG }}

jobs:
  analyze:
    name: Analyze
    runs-on: ${{ env.RUNNER_LABEL }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文