我已经在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
步骤失败时,这也会导致此步骤运行。我不希望发生这种情况,因为在这种情况下没有可存档的文件。
如果上一步已运行(成功或失败),我如何才能运行此步骤?
答案 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