在GitHub中创建作业之间的依赖关系 [英] Create dependencies between jobs in GitHub Actions

查看:48
本文介绍了在GitHub中创建作业之间的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GitHub Actions的新手,他在尝试各种选项来制定出CI/CD管道的良好方法.

I'm new to GitHub Actions, playing with various options to work out good approaches to CI/CD pipelines.

最初,我将所有CI步骤都放在一项工作中,并执行以下操作:

Initially I had all my CI steps under one job, doing the following:

  • 回购中的结帐代码
  • 皮棉
  • 扫描漏洞源
  • 构建
  • 测试
  • 创建图片
  • 扫描图像中的漏洞
  • 推送到AWS ECR

其中某些步骤无需按顺序进行;例如我们可以在构建的同时运行linting和源代码漏洞扫描;节省时间(如果我们假设这些步骤将会通过).

Some of those steps don't need to be done in sequence though; e.g. we could run linting and source code vulnerability scanning in parallel with the build; saving time (if we assume that those steps are going to pass).

即本质上,我希望我的管道执行以下操作:

i.e. essentially I'd like my pipeline to do something like this:

job1 = {
 - checkout code from repo #required per job, since each job runs on a different runner
 - lint
}
job2 = {
 - checkout code from repo
 - scan source for vulnerabilities
}
job3 = {
 - checkout code from repo
 - lint
 - scan source for vulnerabilities
 - build
 - test
 - create image
 - scan image for vulnerabilities
 - await job1 & job2
 - push to AWS ECR
}

我有几个问题:

  1. 是否可以在作业中设置一些 await jobN 规则;即查看另一项工作的状态?
  2. (仅当1的答案为 Yes 时才相关):是否有任何方法可以使一项工作的失败立即影响同一工作流程中的其他工作?即,如果我的棉绒作业检测到问题,那么我可以立即将其称为失败,因此希望作业1中的故障立即停止作业2和3消耗额外的时间,因为它们不再增加价值.
  1. Is it possible to setup some await jobN rule within a job; i.e. to view the status of one job from another?
  2. (only relevant if the answer to 1 is Yes): Is there any way to have the failure of one job immediately impact other jobs in the same workflow? i.e. If my linting job detects issues then I can immediately call this a fail, so would want the failure in job1 to immediately stop jobs 2 and 3 from consuming additional time, since they're no longer adding value.

推荐答案

理想情况下,您的某些作业应封装在自己的工作流程中,例如:

Ideally, some of your jobs should be encapsulated in their own workflows, for example:

  • 通过任何方式测试源的工作流程.
  • (构建和-)部署的工作流程.

然后让这些工作流相互依赖,或者使用不同的触发器来触发.

and then, have these workflows depend on each other, or be triggered using different triggers.

不幸的是,至少暂时而言,工作流依赖项不是现有功能(

Unfortunately, at least for the time being, workflow dependency is not an existing feature (reference).

尽管我认为将您提到的所有工作包含在一个工作流中会创建一个冗长且难以维护的文件,但我相信您仍然可以通过使用GitHub action语法提供的一些条件来实现您的目标.

Although I feel that including all of your mentioned jobs in a single workflow would create a long and hard to maintain file, I believe you can still achieve your goal by using some of the conditionals provided by the GitHub actions syntax.

可能的选项:

使用后者,示例语法可能如下所示:

Using the latter, a sample syntax may look like this:

jobs:
  job1:
  job2:
    needs: job1
  job3:
    needs: [job1, job2]

这是准备用于上述方法测试的工作流程.在此示例中,作业2仅在作业1完成后才运行,而作业3将不运行,因为它取决于失败的作业.

And here is a workflow ready to be used for testing of the above approach. In this example, job 2 will run only after job 1 completes, and job 3 will not run, since it depends on a job that failed.

name: Experiment
on: [push]

jobs:
  job1:
    name: Job 1
    runs-on: ubuntu-latest

    steps:
    - name: Sleep and Run
      run: |
        echo "Sleeping for 10"
        sleep 10

  job2:
    name: Job 2
    needs: job1
    runs-on: ubuntu-latest

    steps:
    - name: Dependant is Running
      run: |
        echo "Completed job 2, but triggering failure"
        exit 1

  job3:
    name: Job 3
    needs: job2
    runs-on: ubuntu-latest

    steps:
    - name: Will never run
      run: |
        echo "If you can read this, the experiment failed"

相关文档:

这篇关于在GitHub中创建作业之间的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆