仅在上一步已运行的情况下运行GitHub Actions步骤 [英] Running a GitHub Actions step only if previous step has run
问题描述
我已经在GitHub操作中设置了工作流程来运行测试并创建测试覆盖范围的工件.我的YAML文件的精简版看起来像这样:
I've set up a workflow in GitHub actions to run my tests and create an artifact of the test coverage. The stripped down version of my YAML file looks like this:
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
问题是测试失败时无法创建工件.
The problem is that the artifact does not get created when the tests fail.
I figured out about if: always()
condition the from the docs, but this will also cause this step to run when my Build app
step fails. I don't want that to happen because there is nothing to archive in that case.
如果上一步已运行(成功或失败),我如何才能运行此步骤?
How can I only run this step if the previous step has run (either succeeded or failed)?
推荐答案
尝试检查 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
或者,创建退出代码的步骤输出,您可以在以后的步骤中进行检查.例如:
Alternatively, create a step output of the exit code that you can check in later steps. For example:
- 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 Actions步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!