Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a github-actions step, even if the previous step fails, while still failing the job

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.
github-ci-result
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 
like image 746
Tomer Shemesh Avatar asked Nov 14 '19 13:11

Tomer Shemesh


People also ask

How do I rerun actions on GitHub?

Re-running all the jobs in a workflowIn the left sidebar, click the workflow you want to see. From the list of workflow runs, click the name of the run to see the workflow run summary. In the upper-right corner of the workflow, use the Re-run jobs drop-down menu, and select Re-run all jobs.

Do steps run in parallel GitHub Actions?

Only jobs can run in parallel, but steps always run sequentially.

How do I manually run a workflow action in GitHub?

On GitHub.com, navigate to the main page of the repository. Under your repository name, click Actions. In the left sidebar, click the workflow you want to run. Above the list of workflow runs, select Run workflow.

Do GitHub Actions jobs run sequentially?

To run jobs sequentially, you can define dependencies on other jobs using the jobs. <job_id>. needs keyword. Each job runs in a runner environment specified by runs-on .

How to re-run a failed job in GitHub actions?

While Github Actions doesn’t provide a way to re-run just the failed jobs out of the box, let’s build something on our own. 1. Set up a step to get the current timestamp 2. Name a cache container using the above timestamp 3. Restore the previous result 4. Set up a condition for further steps 5. Save the result on success

Can I skip a step in a GitHub action without failing?

GitHub Actions: Skipping a step without failing | An independent mind… I wanted to have a GitHub Action step run that might fail, but if it failed the rest of the steps should still execute and the overall run should be treated as a success. Skip to primary navigation Skip to content Skip to footer An independent mind... Portfolio Posts Categories

How do I manually trigger a workflow run on GitHub?

Follow these steps to manually trigger a workflow run. Write access to the repository is required to perform these steps. On GitHub, navigate to the main page of the repository. Under your repository name, click Actions . In the left sidebar, click the workflow you want to run. Above the list of workflow runs, select Run workflow .

What triggers a GitHub event?

Most GitHub events are triggered by more than one type of activity. For example, the label is triggered when a label is created, edited, or deleted. The types keyword enables you to narrow down activity that causes the workflow to run. When only one activity type triggers a webhook event, the types keyword is unnecessary.


2 Answers

You can add

if: always() 

to your step to have it run even if a previous step fails https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#job-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] 
like image 146
Tomer Shemesh Avatar answered Oct 13 '22 04:10

Tomer Shemesh


Other way, you can add continue-on-error: true. Look like

- name: Job fail   continue-on-error: true   run |     exit 1 - name: Next job   run |     echo Hello 

Read more in here.

like image 32
Khai Vu Avatar answered Oct 13 '22 06:10

Khai Vu