[英]Reuse Jobs in GitHub Actions Workflow
I'm migrating a pipeline from Circle CI to Github Actions and am finding it a bit weird that I can only run jobs once instead of creating a job, then calling it from the workflow section, making it possible to call a job multiple times without duplicating the commands/scripts in that job.我正在将管道从 Circle CI 迁移到 Github Actions 并且发现我只能运行一次作业而不是创建作业,然后从工作流部分调用它,这使得可以多次调用作业而无需复制该作业中的命令/脚本。
My pipeline pushes out code to three environments, then runs a lighthouse scan for each of them.我的管道将代码推送到三个环境,然后对每个环境运行一次灯塔扫描。 In circle ci I have 1 job to push the code to my envs and 1 job to run lighthouse.在循环 ci 中,我有 1 份工作将代码推送到我的环境,还有 1 份工作来运行灯塔。 Then from my workflow section, I just call the jobs 3 times, passing the env as a parameter.然后从我的工作流部分,我只调用了 3 次作业,将 env 作为参数传递。 Am I missing something or is there no way to do this in github actions?我是否遗漏了什么,或者在 github 动作中没有办法做到这一点? Do I just have to write out my commands 3 times in each job?我是否只需要在每个工作中写出我的命令 3 次?
There are 3 main approaches for code reusing in GitHub Actions: GitHub Actions 中有 3 种主要的代码重用方法:
The obvious option is using the "Reusable workflows" feature that allows you to extract some steps into a separate "reusable" workflow and call this workflow as a job in other workflows.显而易见的选择是使用“可重用工作流程”功能,该功能允许您将一些步骤提取到单独的“可重用”工作流程中,并将此工作流程称为其他工作流程中的作业。
strategy
property is not supported in any job that calls a reusable workflow.调用可重用工作流的任何作业都不支持strategy
属性。needs
keys).您可以以同步或异步方式调用可重用工作流(通过使用needs
键的作业排序来管理它)。Another possibility that GitHub gives us is workflow_dispatch event that can trigger a workflow run. GitHub 给我们的另一种可能性是可以触发工作流运行的workflow_dispatch事件。 Simply put, you can trigger a workflow manually or through GitHub API and provide its inputs.简而言之,您可以手动或通过 GitHub API 触发工作流并提供其输入。
There are actions available on the Marketplace which allow you to trigger a "dispatched" workflow as a step of "main" workflow. Marketplace 上有一些可用的操作,可让您触发“已调度”工作流程作为“主要”工作流程的一个步骤。
Some of them also allow doing it in a synchronous manner (wait until dispatched workflow is finished). 其中一些还允许以同步方式执行此操作(等到分派的工作流完成)。 It is worth to say that this feature is implemented by polling statuses of repo workflows which is not very reliable, especially in a concurrent environment.值得一提的是,这个特性是通过轮询 repo 工作流的状态来实现的,这不是很可靠,尤其是在并发环境中。 Also, it is bounded by GitHub API usage limits and therefore has a delay in finding out a status of dispatched workflow.此外,它受 GitHub API 使用限制的限制,因此在查找已调度工作流的状态时会有延迟。
secrets.GITHUB_TOKEN
doesn't allow you to dispatch a workflow.你通常的secrets.GITHUB_TOKEN
不允许你调度工作流。In this approach we extract steps to a distinct composite action , that can be located in the same or separate repository.在这种方法中,我们将步骤提取到不同的复合操作中,该操作可以位于相同或单独的存储库中。
From your "main" workflow it looks like a usual action (a single step), but internally it consists of multiple steps each of which can call own actions.从您的“主要”工作流程来看,它看起来像一个通常的操作(一个步骤),但在内部它由多个步骤组成,每个步骤都可以调用自己的操作。
continue-on-error
option inside a composite action.您不能在复合操作中使用continue-on-error
选项。Source: my "DRY: reusing code in GitHub Actions" article来源:我的“DRY:在 GitHub Actions 中重用代码”文章
I'm currently in the exact same boat and just found an answer.我目前在同一条船上,刚刚找到了答案。 You're looking for a Composite Action , as suggested in this answer .您正在寻找一个Composite Action ,正如this answer中所建议的那样。
Reusable workflows can't call other reusable workflows.可重用工作流不能调用其他可重用工作流。
Actually, they can, since Aug. 2022:实际上,自 2022 年 8 月以来,他们可以:
GitHub Actions: Improvements to reusable workflows GitHub 操作:改进可重用工作流
Reusable workflows can now be called from a matrix and other reusable workflows.现在可以从矩阵和其他可重用工作流中调用可重用工作流。
You can now nest up to 4 levels of reusable workflows giving you greater flexibility and better code reuse.您现在可以嵌套多达 4 个级别的可重用工作流,从而为您提供更大的灵活性和更好的代码重用。
Calling a reusable workflow from a matrix allows you to create richer parameterized builds and deployments.从矩阵调用可重用的工作流允许您创建更丰富的参数化构建和部署。
Learn more about nesting reusable workflows .了解有关嵌套可重用工作流的更多信息。
Learn more about using reusable workflows with the matrix strategy .了解有关使用矩阵策略的可重用工作流的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.