I'm trying to follow an example Github has for testing my build with github actions, and then compressing the test results and uploading them as an artifact. https://help.github.com/en/actions/automating-your-workflow-with-github-actions/persisting-workflow-data-using-artifacts#uploading-build-and-test-artifacts
I'm having trouble with what to do when my tests fail though. This is my action. When my tests pass everything works great, my results are zipped an exported as an artifact, but if my tests fail, it stops the rest of the steps in the job, so my results never get published.
I tried adding the continue-on-error: true https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepscontinue-on-error
This makes it continue after it fails and uploads my test results. but then the job is marked as passed, even though my test step failed. Is there some way to have it upload my artifact even if a step fails, while still marking the overall job as failed?
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: Archive Rest Results
uses: actions/upload-artifact@v1
with:
name: test-results
path: app/build/reports/tests
You can add
if: always()
to your step to have it run even if a previous step fails https://docs.github.com/en/actions/learn-github-actions/expressions#status-check-functions
so for a single step it would look like this:
steps:
- name: Build App
run: ./build.sh
- name: Archive Test Results
if: always()
uses: actions/upload-artifact@v1
with:
name: test-results
path: app/build
Or you can add it to a job:
jobs:
job1:
job2:
needs: job1
job3:
if: always()
needs: [job1, job2]
Additionally, as pointed out below, putting always()
will cause the function to run even if the build is canceled. If you don't want the function to run when you manually cancel a job, you can instead put:
if: success() || failure()
or
if: '!cancelled()'
(Quotes are needed so that !cancelled()
is not interpreted as a YAML tag.)
Likewise, if you want to run a function ONLY when something has failed, you can put:
if: failure()
Also, as mentioned in the comments, if a status check function is not used in if
, like
if: true
the result will (perhaps confusingly) behave like
if: success() && true
This is documented in Expressions - GitHub Docs:
Status check functions
You can use the following status check functions as expressions in if conditionals. A default status check of
success()
is applied unless you include one of these functions.