在github动作工作流中,是否有办法让多个作业重复使用相同的设置? [英] In a github actions workflow, is there a way to have multiple jobs reuse the same setup?

查看:71
本文介绍了在github动作工作流中,是否有办法让多个作业重复使用相同的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的项目与github操作挂钩,以进行持续集成.我创建了两个单独的作业:第一个作业检查pull请求中的代码是否被我们的linter接受,第二个作业检查代码是否通过测试套件.我喜欢这样的两项工作,在Github网页中针对拉取请求显示为两个单独的复选标记:

I recently hooked up my project with github actions for continuous integration. I created two separate jobs: the first one checks if the code in the pull request is accepted by our linter, and the second one checks if the code passes the test suite. I like that having two jobs like this shows up as two separate checkmarks in the Github webpage for the pull request:

我现在遇到的问题是工作流YAML文件中有一些重复的代码:前三个步骤,安装了Lua和Luarocks.维护不仅烦人,而且两次执行相同的操作浪费CI分钟.有办法避免这种情况吗?这样,设置代码只能写在一个地方,并且在工作流程执行时只能运行一次?

The problem I'm having now is that there is some duplicated code in workflow YAML file: the first 3 steps, which install Lua and Luarocks. Not only is it annoying to maintain, but it also wastes CI minutes by running the same actions twice. Is there a way to avoid this? So that the setup code is only written in one place, and only runs once when the workflow executes?

但是我感到困惑的是继续进行的正确方式:

But I am confused what would be the proper way to proceed:

  1. 我应该使用共享的设置代码创建自己的Github Action吗?
  2. 我应该创建一个已经预先安装了Lua和Luarocks的Docker镜像吗?
  3. 我应该使用一份工作吗?如果Linter和测试套件是同一工作的步骤,我还能为它们做独立的复选标记吗?
  4. 还有别的吗?

这是我的工作流程的当前YAML文件:

Here is the current YAML file for my workflow:

name: Github Actions CI

on: [ pull_request ]

jobs:
    lint:
        name: Lint
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: leafo/gh-actions-lua@v8.0.0
            - uses: leafo/gh-actions-luarocks@v4.0.0

            - run: luarocks install luacheck
            - run: ./run-linter.sh

    test:
        name: Test
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: leafo/gh-actions-lua@v8.0.0
            - uses: leafo/gh-actions-luarocks@v4.0.0

            - run: luarocks install busted
            - run: ./build-project.sh
            - run: ./run-test-suite.sh

我尝试搜索类似的问题,但找不到能完全回答我问题的内容:

I tried searching for similar questions but couldn't find anything that exactly answered my question:

  1. 在GitHub Actions工作流程中缓存APT包:我无法使用此解决方案,因为我没有办法精确指定我正在使用的所有依赖项的所有版本,以便可以对其进行缓存.我也不介意是否不缓存工作流的单独运行.我更担心代码重复.
  2. Github操作在作业之间共享工作空间/工件吗?不必管理将上传工件上传到单独的服务,然后再将其删除.
  3. 在作业中重复使用github操作的一部分:在那个问题中作业之间的唯一区别是单个变量,因此可以接受的答案是使用构建矩阵.但是我认为仅设置代码相同的情况下,构建矩阵就无法很好地发挥作用吗?
  1. Caching APT packages in GitHub Actions workflow: I can't use this solution because I don't have a way to precisely specify all the versions of all the dependencies that I am using, so that they may be cached. I also don't mind if separate runs of the workflow are not cached. I'm more worried about the code duplication.
  2. Github actions share workspace/artifacts between jobs? I don't want to have to manage uploading uploading artifacts to a separate service and then deleting them afterwards.
  3. Reuse portion of github action across jobs: In that question the only difference between the jobs is a single variable, so accepted answer is to use a build matrix. But I don't think a build matrix would work as well in my case, where only the setup code is the same?

推荐答案

您正在寻找的是

What you are looking for is composite action which help you reuse once defined set of steps.

jobs:
    lint:
        name: Lint
        runs-on: ubuntu-latest
        steps:
            - uses: octocat/say-hello@v1

            - run: luarocks install luacheck
            - run: ./run-linter.sh

    test:
        name: Test
        runs-on: ubuntu-latest
        steps:
            - uses: octocat/say-hello@v1

            - run: luarocks install busted
            - run: ./build-project.sh
            - run: ./run-test-suite.sh

octocat/say-hello/action.yml:

octocat/say-hello/action.yml:

runs:
  using: "composite"
  steps: 
  - uses: actions/checkout@v2  # to my understanding you can't use "uses" in steps of an action because an action can't encapsulate another action, but only run-s
  - uses: leafo/gh-actions-lua@v8.0.0
  - uses: leafo/gh-actions-luarocks@v4.0.0

有关更多详细信息,您还可以检查ADR

For more details you can also check's ADR here.

为什么不能简单地对所有作业运行一次,因为每个作业可能在不同的计算机上运行.

And why you can't simply run it once for all jobs, because each job may run on a different machine.

作业是在同一跑步者上执行的一组步骤.默认情况下,具有多个作业的工作流程将并行运行这些作业.您还可以配置工作流程以按顺序运行作业.例如,工作流可以具有两个顺序的作业,用于构建和测试代码,其中测试作业取决于构建作业的状态.如果构建作业失败,则测试作业将不会运行.

A job is a set of steps that execute on the same runner. By default, a workflow with multiple jobs will run those jobs in parallel. You can also configure a workflow to run jobs sequentially. For example, a workflow can have two sequential jobs that build and test code, where the test job is dependent on the status of the build job. If the build job fails, the test job will not run.

这篇关于在github动作工作流中,是否有办法让多个作业重复使用相同的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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