Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fail a job in Github Actions?

I'm developing a Github actions workflow. This workflow runs on Linux, Mac, and Windows.

As part of the workflow, I have to check whether 2 environment variables are equal. If they don't - fail the job.

As described here, Github Actions support if: condition:

steps: - run: # How can I make a cross-platform failure here?   if: ${{ envA }} != ${{ envB }} 

How can I make the job fail if the above condition is true? In the beginning, I thought of a script, but there must be a more elegant way to fail a job.

like image 281
yahavi Avatar asked Sep 12 '19 09:09

yahavi


People also ask

How do you fail an action in GitHub?

GitHub uses the exit code to set the action's check run status, which can be success or failure . The action completed successfully and other tasks that depends on it can begin. Any other exit code indicates the action failed. When an action fails, all concurrent actions are canceled and future actions are skipped.

How do I re-run a test on GitHub?

Re-running all the jobs in a workflow 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 GitHub Actions jobs run in parallel?

You can configure a GitHub Actions workflow to be triggered when an event occurs in your repository, such as a pull request being opened or an issue being created. Your workflow contains one or more jobs which can run in sequential order or in parallel.

Do GitHub Actions cost money?

GitHub Actions usage is free for both public repositories and self-hosted runners. For private repositories, each GitHub account receives a certain amount of free minutes and storage, depending on the product used with the account.


2 Answers

I'd do run: exit 1. That will simply exit with an exit code of 1, on all three platforms.

Proof that it's cross-platform: https://github.com/rmunn/Testing/runs/220188838 which runs the following workflow:

name: Test exiting on failure  on: [push]  jobs:   build:      strategy:       matrix:         os: [ubuntu-latest, windows-latest, macOS-latest]     runs-on: ${{ matrix.os }}          steps:     - uses: actions/checkout@v1     - name: Try to fail       run: exit 1     - name: Print message if we don't fail       run: echo Should not get here 

(An earlier version of this answer recommended "/bin/false", but that would only work on Linux and macOS).

like image 123
rmunn Avatar answered Oct 12 '22 22:10

rmunn


In 2021, there is perhaps a more graceful way to do this:

- name: A/B Check   if: ${{ envA }} != ${{ envB }}   uses: actions/github-script@v3   with:     script: |         core.setFailed('envA and envB are not equivalent!') 

Here, we use the github-script action to provide a one liner script that will fail the job. The "A/B Check" step will only run if the condition in the if line is true, so the script will only run in that case, which is what we want.

The nice thing about this approach is that you will get nicely formatted output in the Actions UI in your repo, showing that the "A/B Check" step caused the failure, and why (i.e. "envA and envB are not equivalent").

Note that if you have additional steps in the job after this, and you do NOT want them to run if the A/B check fails, you'll want to use if: success() on them to prevent them from running in that case.

like image 20
Bradleycorn Avatar answered Oct 12 '22 23:10

Bradleycorn