2
votes

I have a work flow which in one of its steps, if the commands finish with exit code 1 (failure), i want to run next command/job (fix the problem which caused previous command failure), but i don't want that exit code 1 affect on a 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 if the second command fixed the problem, the result status be succeed.
is it possible?

here is my workflow.yml:

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

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"
1
Did you try using black --check || true ? The || true suffix will ignore the command output error (always consider it as true) and allow to continue the workflow job or steps without returning failure. That way, you can eventually use black --check &> output.txt || true to create an output file and check in another step if this output file contains exit code 1 or not (for example, with grep -q) and save it as failure in a job output variable, to use in the second job if condition. - GuiFalourd

1 Answers

0
votes

As an example, you can use an if condition to set the output parameter and refer to it in subsequent steps

    steps:
      - uses: actions/checkout@master
      - name: Install Black
        run: pip3 install git+git://github.com/psf/black
      - name: Check black status
        id: black_status
        run: |
          if black --check .; then
            echo "::set-output name=result::true"
          else
            echo "::set-output name=result::false"
          fi
      - if: steps.black_status.outputs.result == 'true'
        name: Check1
        run: echo "hi"
      - if: steps.black_status.outputs.result == 'false'
        name: Check2
        run: echo "yo"