首页 > 解决方案 > 仅当上一步已运行时才运行 GitHub Actions 步骤

问题描述

我在 GitHub 操作中设置了一个工作流来运行我的测试并创建测试覆盖率的工件。我的 YAML 文件的精简版本如下所示:

name: Build

on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Other steps here

      - name: Build app
      - name: Run tests
      - name: Create artifact of test coverage

      # Other steps here

问题是当测试失败时没有创建工件。

我从文档中弄清楚了if: always()条件,但这也会导致当我的步骤失败时运行此步骤。我不希望这种情况发生,因为在这种情况下没有什么可以存档的。Build app

如果上一步已经运行(成功或失败),我如何才能运行这一步?

标签: githubcontinuous-integrationcontinuous-deploymentgithub-actions

解决方案


尝试检查success()OR failure()

name: Build

on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Other steps here

      - name: Build app
      - name: Run tests
      - name: Create artifact of test coverage
        if: success() || failure()

      # Other steps here

或者,创建退出代码的步骤输出,您可以在以后的步骤中检查它。例如:

      - name: Build app
        id: build
        run: |
          <build command>
          echo ::set-output name=exit_code::$?

      - name: Run tests

      - name: Create artifact of test coverage
        if: steps.build.outputs.exit_code == 0

推荐阅读