在GitHub动作中,我可以提取重复的变量或条件吗?
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在工作流级别上使用环境变量,类似:
然后,可以在任何工作步骤中使用此变量。如果您想看一下,我在这里对其进行了测试:
请注意,如果:$ {env.Branch}}} 在工作级别上,您不能使用条件
。有一个参考 GitHub合作伙伴 Brightran 关于这个特定点:
You could you use an environment variable at the workflow level, something like:
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: