Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude an exit code in github actions workflow result status

I have a workflow which in one of its steps, if the command finishes with exit code 1 (failure), I want to run the next command/job (fix the problem which caused previous command failure), but I don't want that exit code 1 to affect the workflow result status.
In this situation, if I have exit code 1, even if I fix the problem, the result status will be failure, but I want the result status to be success if the second command fixed the problem.
Is this possible?

Here is my workflow.yml:
\

name: autoblack
on: [pull_request, push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Python 3.8
        uses: actions/[email protected]
        with:
          python-version: 3.8
      - name: Install Black
        run: pip3 install git+git://github.com/psf/black
        
      - name: Run black --check .
        run: black --check .
  
  reformat:
    runs-on: ubuntu-latest
    needs: [build]
    if: always() && (needs.build.result == 'failure')
    steps:
      - uses: actions/[email protected]
      - name: Set up Python 3.8
        uses: actions/[email protected]
        with:
          python-version: 3.8
      - name: Install Black
        run: pip3 install git+git://github.com/psf/black
      - name: If needed, commit black changes to the pull request
        env:
          NEEDS_CONTEXT: ${{ toJSON(needs) }}
        run: |
          black --fast .
          git config --global user.name 'autoblack'
          git config --global user.email '[email protected]'
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
          git checkout $GITHUB_HEAD_REF
          echo "$NEEDS_CONTEXT"
          git commit -am "fixup: Format Python code with Black"
          git push
          echo "$NEEDS_CONTEXT"

If the job build with command run: black --check . has exit code 1, I want to run the job reformat to fix the problem and push that to the repository. The second job works fine, but the final result label is failure!!

like image 739
Signor Avatar asked Nov 30 '25 04:11

Signor


1 Answers

The continue-on-error attribute of the step (also available on the job) can be used to prevent a job from failing when a step fails.

Then the steps context contains a steps.<step_id>.outcome that can be used in expressions to determine if the prior step failed. For example, in the workflow below I have a step that will fail when the commitlint command fails, but it uses continue-on-error to allow the workflow to continue:

      - name: commitlint
        id: commitlint
        continue-on-error: true
        run: |
          npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose

Then the subsequent two steps will apply or remove a label based on the commitlint step's success/failure outcome:

      - name: label when commitlint fails
        if: ${{ steps.commitlint.outcome == 'failure' }}
        uses: andymckay/[email protected]
        with:
          add-labels: "commit-message-rule-violation"

      - name: label removal when commitlint succeeds
        if: ${{ steps.commitlint.outcome == 'success' }}
        uses: andymckay/[email protected]
        with:
          remove-labels: "commit-message-rule-violation"

The workflow ultimately succeeds regardless of whether the commitlint step fails or not due to continue-on-error: true.

The complete working workflow for this is here

like image 132
Scott Willeke Avatar answered Dec 01 '25 16:12

Scott Willeke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!