GitHub Action的作业能否从前一个作业离开的状态继续(无需使用构件或繁琐的过程)?

9
我之前使用了一个仅包含一个任务的Github Action,它完成了三个任务:
  • 构建.NET应用程序
  • 运行单元测试
  • 运行集成测试

现在,我将这个任务分成了三个独立的任务:

  • 我喜欢尝试新事物
  • 我想要在GitHub PR中单独更新步骤
  • 我可以/想同时运行单元测试和集成测试,以便整个过程可以快速完成

这是当前的GitHub Action:

name: Pull Request Checks

on: 
  pull_request:
    types: [opened, synchronize, reopened, labeled]
  
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Buid VS solution
      id: build
      run: dotnet build "FSharp project/MyProject.sln" -c RELEASE

  unit-tests:
    name: Unit Tests
    needs: [build]
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Unit Tests
      id: unit-tests
      run: dotnet test "FSharp project/UnitTests/UnitTests.fsproj" -c Release --no-build --filter "TestCategory!=SKIP_ON_DEPLOY"

  integration-tests:
    name: Integration Tests
    needs: [build]
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Integration Tests
      id: integration-tests
      if: github.event.action == 'labeled' && github.event.label.name == 'pr:ready'
      run: dotnet test "FSharp project/IntegrationTests/IntegrationTests.fsproj" -c Release --no-build --filter "TestCategory!=SKIP_ON_DEPLOY"

理想情况下,只有在 PR 被标记为“pr:ready”时才运行集成测试作业(这一点可能仍需调整/解决)。
整个流程都有效。我不得不在每个作业中复制“Checkout”步骤,这意味着它们是完全不同的“机器”。如果是这样,为什么使用 "--no-build" 的 "dotnet test" 仍然能够工作?微软更改了该标志的行为,所以老实说,我不记得这里运行的 dotnet cli 版本是否能够重用可能已执行的构建或者它是否会自己运行构建(如果需要的话)。因此,我不能完全确定“Checkout”结果在“后续”作业中是否具有完全新鲜的环境,如果是这种情况……有没有一种简单的方法来重用先前的“状态”(例如一个简单的参数,而不是使用工件和类似的东西)?
1个回答

2
每个 GitHub Actions 作业都在全新的虚拟环境中运行。在作业之间获取数据/文件等唯一的方法是使用 artifacts。在您的情况下,解决方案是在构建作业中生成一个 artifact,然后可以在两个测试作业中并行使用它。请参见检查操作存储库上的此 issue
因此,您可以将测试所需的所有内容打包到 artifact 中,并将测试作业中的 checkout 步骤替换为下载该 artifact。
至于为什么您的 --no-build 测试有效,这很难回答,而不知道正在检出什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接