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

标签 github continuous-integration continuous-deployment github-actions

我已经在GitHub Action 中设置了工作流程来运行测试并创建测试覆盖范围的工件。我的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步骤失败时,这也将导致此步骤运行。我不希望发生这种情况,因为在这种情况下没有可存档的内容。

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

最佳答案

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

关于github - 仅在上一步已运行的情况下运行GitHub Actions步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60453924/

相关文章:

msbuild - 是否可以使用 dotnet CLI 提取 csproj 文件中元素的值?

linux - 如何在拉取后恢复到之前的提交而不丢失之前的更改?

amazon-web-services - 将现有 CodePipeline/CodeBuild 项目导出到 Cloudformation

maven - 将Eclipse格式化程序文件转换为Checkstyle文件

数据库部署 - 生成部署计划期间发生错误。部署无法继续

javascript - 使用 GitLab 持续部署 NodeJS

azure - 基础设施版本控制 持续部署

git - 无法推送或 pull 到 Github

github - 如何在 GitHub 问题评论中嵌入 Gist?

php - 如何使用 API 创建 GitHub Gist?