【问题标题】:Running a GitHub Actions step only if previous step has run仅当上一步已运行时才运行 GitHub Actions 步骤
【发布时间】:2022-01-01 22:50:46
【问题描述】:

我在 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 步骤失败时,这也会导致此步骤运行。我不希望这种情况发生,因为在这种情况下没有什么可归档的。

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      一个可能更好的选择是&lt;step&gt;.outcome&lt;step&gt;.conclusion

      https://docs.github.com/en/actions/learn-github-actions/contexts#steps-context

      steps.&lt;step id&gt;.conclusion。应用 continue-on-error 后已完成步骤的结果。可能的值为 successfailurecancelledskipped。当 continue-on-error 步骤失败时,结果是失败,但最终结论是成功。

      steps.&lt;step id&gt;.outcome 应用 continue-on-error 之前已完成步骤的结果。可能的值为 successfailurecancelledskipped。当 continue-on-error 步骤失败时,结果是失败,但最终结论是成功。

        - name: Build app
          id: build
          run: |
            <build command>
      
        - name: Run tests
      
        - name: Create artifact of test coverage
          if: steps.build.outcome == 'success'
      

      【讨论】:

        猜你喜欢
        • 2020-11-28
        • 1970-01-01
        • 2020-01-16
        • 1970-01-01
        • 2018-12-20
        • 2021-11-16
        • 2023-01-26
        • 2020-01-01
        • 2022-01-13
        相关资源
        最近更新 更多