如何 运行 下一个 github 操作步骤,即使上一步失败了,但作业仍然失败?
How do to run the next github action step even if the previous step failed, while still failing the job?
这个问题与 类似,但接受的答案对我没有帮助,因为它创造了一个额外的工作。
下面我要完成的是
- 当测试应用程序(第 2 步)通过时;测试清理步骤应该 运行 和 github 操作工作流程 returns 成功。
- 当测试应用程序(step2)失败时; test-clean、action-slack 和 fail-action 步骤应该 运行。 github 操作工作流 returns 失败。
如何修复以下代码以实现它?
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test-app
run: ./gradlew test
- name: test-clean
run: some cleanup that should run always
- name: action-slack
if: ${{ step2.result != 'success' }}
uses: 8398a7/action-slack@v3
with:
status: ${{ step2.result }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: fail-action
run: |
if ${{ step2.result != 'success' }}; then
exit 1
fi
您可以使用 status check functions 了解前面步骤的状态。如果您不包含此类函数,则隐含 if: success() && ...
。这意味着除非您在 if
子句中使用 always()
或 failure()
,否则当以前的作业失败时作业不会 运行。
要解决前面步骤的结果,您可以使用 steps context
,例如 steps.<id>.outcome
(在 continue-on-error
之前 已应用)或 steps.<id>.conclusion
(在应用 continue-on-error
之后)。
这是一个结合所有内容的工作示例:
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Mock to test workflow
- name: Test app
id: test-app # will be referenced later
run: |
echo "Testing app (randomly fails)"
if [[ $(($RANDOM % 2)) == 0 ]]; then exit 0; else exit 1; fi
# runs always
- name: test-clean
if: always()
run: echo "Cleanup after tests"
# runs if previous jobs failed and test-app was not successful (failure/cancelled)
- name: action-slack
if: failure() && steps.test-app.outcome != 'success'
run: |
echo "Run action-slack"
echo "Result of test-app was '${{ steps.test-app.outcome }}'"
PS:另一个问题的答案并没有增加额外的工作,而是包括一个关于如何跨工作应用它的例子。但是,该答案并未解决您的确切用例,但它可以通过提供一些指示对您有所帮助。
这个问题与
下面我要完成的是
- 当测试应用程序(第 2 步)通过时;测试清理步骤应该 运行 和 github 操作工作流程 returns 成功。
- 当测试应用程序(step2)失败时; test-clean、action-slack 和 fail-action 步骤应该 运行。 github 操作工作流 returns 失败。
如何修复以下代码以实现它?
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test-app
run: ./gradlew test
- name: test-clean
run: some cleanup that should run always
- name: action-slack
if: ${{ step2.result != 'success' }}
uses: 8398a7/action-slack@v3
with:
status: ${{ step2.result }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: fail-action
run: |
if ${{ step2.result != 'success' }}; then
exit 1
fi
您可以使用 status check functions 了解前面步骤的状态。如果您不包含此类函数,则隐含 if: success() && ...
。这意味着除非您在 if
子句中使用 always()
或 failure()
,否则当以前的作业失败时作业不会 运行。
要解决前面步骤的结果,您可以使用 steps context
,例如 steps.<id>.outcome
(在 continue-on-error
之前 已应用)或 steps.<id>.conclusion
(在应用 continue-on-error
之后)。
这是一个结合所有内容的工作示例:
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Mock to test workflow
- name: Test app
id: test-app # will be referenced later
run: |
echo "Testing app (randomly fails)"
if [[ $(($RANDOM % 2)) == 0 ]]; then exit 0; else exit 1; fi
# runs always
- name: test-clean
if: always()
run: echo "Cleanup after tests"
# runs if previous jobs failed and test-app was not successful (failure/cancelled)
- name: action-slack
if: failure() && steps.test-app.outcome != 'success'
run: |
echo "Run action-slack"
echo "Result of test-app was '${{ steps.test-app.outcome }}'"
PS:另一个问题的答案并没有增加额外的工作,而是包括一个关于如何跨工作应用它的例子。但是,该答案并未解决您的确切用例,但它可以通过提供一些指示对您有所帮助。