仅在上一步已运行的情况下运行GitHub Actions步骤

时间:2020-02-28 14:39:15

标签: github continuous-integration continuous-deployment 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

问题是测试失败时无法创建工件。

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

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

1 个答案:

答案 0 :(得分:6)

尝试检查success()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