在GitHub动作中,我可以提取重复的变量或条件吗?

发布于 2025-02-07 01:28:41 字数 523 浏览 1 评论 0原文

我有一个github动作。在其中,我有几个步骤只能在某些条件下执行 - 在这种情况下,如果分支名称匹配某个模式,则应执行这些步骤。像这样:

- name: Deploy infrastructure
  if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/features/lrd')

此如果块在我的动作中递归了几次。是否可以将其提取到共享变量,条件或功能中,以便我可以摆脱丑陋的代码重复?最好的方法是什么?

我想一个选项是在较早的步骤中计算变量并在后期的步骤中引用它,如本文中所述: https ://stackoverflow.com/a/58676568/4290962

有一个单独的一步来计算此值似乎有些丑陋。这是我们能做的最好的吗?还是有更好的选择?

提前致谢!

I have a GitHub Action. In it I have several steps that should only be executed under certain conditions - in this case, these steps should be executed if the branch name matches a certain pattern. Like this:

- name: Deploy infrastructure
  if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/features/lrd')

This if block recurs several times in my Action. Is it possible to extract it to a shared variable, condition or function so I can get rid of the ugly code duplication? What would be the nicest way to do this?

I suppose that one option would be to compute the variable in an earlier step and reference it in the later steps, as explained in this post: https://stackoverflow.com/a/58676568/4290962

It just seems a bit ugly to have a separate step to compute this value. Is it the best we can do? Or is there a nicer option?

Thanks in advance!

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

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

发布评论

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

评论(1

╰沐子 2025-02-14 01:28:41

您可以在工作流级别上使用环境变量,类似:

on:
  push:

env:
  BRANCH: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/features/lrd') }}

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Check branch and print it
        if: ${{ env.BRANCH == 'true' }} # using directly {{ env.BRANCH }} might work as well
        run: echo ${{ env.BRANCH }}

然后,可以在任何工作步骤中使用此变量。如果您想看一下,我在这里对其进行了测试:

请注意,如果:$ {env.Branch}}} 在工作级别上,您不能使用条件。有一个参考 GitHub合作伙伴 Brightran 关于这个特定点:

关于您只能在以下情况下使用它:

  • 用键的值;
  • 步骤的名称键的值;
  • 步骤的env键的价值;
  • 在条件;
  • 的步骤中

  • 在运行步骤的命令行中。

You could you use an environment variable at the workflow level, something like:

on:
  push:

env:
  BRANCH: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/features/lrd') }}

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Check branch and print it
        if: ${{ env.BRANCH == 'true' }} # using directly {{ env.BRANCH }} might work as well
        run: echo ${{ env.BRANCH }}

Then, this variable could be used in any job step. I tested it here if you want to have a look:

Note that you can't use the condition if: ${{ env.BRANCH }} at the job level, it's not supported yet. There is a reference in this post on the Github community from Github Partner brightran about this specific point:

About the env context, you can only use it in the following scenarios:

  • the value of the with key;
  • the value of the step’s name key;
  • the value of the step’s env key;
  • in a step’s if conditional;
  • in the command lines of a run step.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文