简体   繁体   English

在 GitHub 操作工作流中重用作业

[英]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 种主要的代码重用方法:

Reusing workflows重用工作流程

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.显而易见的选择是使用“可重用工作流程”功能,该功能允许您将一些步骤提取到单独的“可重用”工作流程中,并将此工作流程称为其他工作流程中的作业。

Takeaways:要点:

  • Reusable workflows can't call other reusable workflows.可重用工作流不能调用其他可重用工作流。
  • The strategy property is not supported in any job that calls a reusable workflow.调用可重用工作流的任何作业都不支持strategy属性。
  • Env variables and secrets are not inherited.环境变量和秘密不会被继承。
  • It's not convenient if you need to extract and reuse several steps inside one job.如果您需要在一项工作中提取和重用多个步骤,这并不方便。
  • Since it runs as a separate job, you have to use build artifacts to share files between a reusable workflow and your main workflow.由于它作为单独的作业运行,因此您必须使用构建工件在可重用工作流和主工作流之间共享文件。
  • You can call a reusable workflow in synchronous or asynchronous manner (managing it by jobs ordering using needs keys).您可以以同步或异步方式调用可重用工作流(通过使用needs键的作业排序来管理它)。
  • A reusable workflow can define outputs that extract outputs/outcomes from executed steps.可重用的工作流可以定义从执行步骤中提取输出/结果的输出。 They can be easily used to pass data to the "main" workflow.它们可以很容易地用于将数据传递给“主”工作流。

Dispatched workflows分派的工作流

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 使用限制的限制,因此在查找已调度工作流的状态时会有延迟。

Takeaways外卖

  • You can have multiple nested calls, triggering a workflow from another triggered workflow.您可以有多个嵌套调用,从另一个触发的工作流触发一个工作流。 If done careless, can lead to an infinite loop.如果做的不小心,可能会导致死循环。
  • You need a special token with "workflows" permission;您需要具有“工作流程”权限的特殊令牌; your usual secrets.GITHUB_TOKEN doesn't allow you to dispatch a workflow.你通常的secrets.GITHUB_TOKEN不允许你调度工作流。
  • You can trigger multiple dispatched workflows inside one job.您可以在一个作业中触发多个分派的工作流。
  • There is no easy way to get some data back from dispatched workflows to the main one.没有简单的方法可以将一些数据从分派的工作流中恢复到主要的工作流中。
  • Works better in "fire and forget" scenario.在“一劳永逸”的情况下效果更好。 Waiting for a finish of dispatched workflow has some limitations.等待分派的工作流完成有一些限制。
  • You can observe dispatched workflows runs and cancel them manually.您可以观察已调度的工作流运行并手动取消它们。

Composite Actions复合动作

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.从您的“主要”工作流程来看,它看起来像一个通常的操作(一个步骤),但在内部它由多个步骤组成,每个步骤都可以调用自己的操作。

Takeaways:要点:

  • Supports nesting: each step of a composite action can use another composite action.支持嵌套:复合动作的每一步都可以使用另一个复合动作。
  • Bad visualisation of internal steps run: in the "main" workflow it's displayed as a usual step run.内部步骤运行的不良可视化:在“主要”工作流程中,它显示为通常的步骤运行。 In raw logs you can find details of internal steps execution, but it doesn't look very friendly.在原始日志中,您可以找到内部步骤执行的详细信息,但看起来不太友好。
  • Shares environment variables with a parent job, but doesn't share secrets, which should be passed explicitly via inputs.与父作业共享环境变量,但不共享机密,应通过输入显式传递。
  • Supports inputs and outputs.支持输入和输出。 Outputs are prepared from outputs/outcomes of internal steps and can be easily used to pass data from composite action to the "main" workflow.输出是从内部步骤的输出/结果准备的,并且可以很容易地用于将数据从复合操作传递到“主”工作流。
  • A composite action runs inside the job of the "main" workflow.复合动作在“主”工作流的作业中运行。 Since they share a common file system, there is no need to use build artifacts to transfer files from the composite action to the "main" workflow.由于它们共享一个公共文件系统,因此无需使用构建工件将文件从复合操作传输到“主”工作流。
  • You can't use 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM