Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to exclude an exit code in github actions workflow result status (failure)? #908

Closed
signorrayan opened this issue Sep 14, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@signorrayan
Copy link

I have a workflow 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/setup-python@v2.2.2
        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/checkout@v2.3.4
      - name: Set up Python 3.8
        uses: actions/setup-python@v2.2.2
        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 'signorrayan@users.noreply.github.com'
          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"

@signorrayan signorrayan added the bug Something isn't working label Sep 14, 2021
@im-mortal
Copy link

im-mortal commented Sep 15, 2021

GitHub uses the exit code of the last executed command to set the action's check run status.

To start with, there’s no need to run this workflow with two distinct runner jobs. One job (or even step) is quite enough for this.
Secondly, you may want to disable the default fail-fast behavior for your shell commands.
Then, you can use black --check . as a condition:

black --check . || black --fast .
…
git push

Having that said, this is an issue tracker for filing bugs. You may want to learn more about shell scripts, GitHub Actions and visit GitHub Community if you have any questions left by then.

Good luck!

@signorrayan
Copy link
Author

@im-mortal Thanks for the help. here is my new workflow. everything seems good, but the new problem is if a person create a Pull Request, this workflow will be failed because of the 'branch name'. for example patch-1 (the branch that created pull request from that) >> main (main branch in the main repository), will be fail, because the patch-1 branch does not exists in the main repository.

name: autoblack
on: [pull_request, push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        if: github.event_name == 'pull_request'
        with:
          fetch-depth: 0
          ref: ${{ github.event.pull_request.head.ref }}

      - uses: actions/checkout@v2
        if: github.event_name == 'push'
        with:
          fetch-depth: 0
      - uses: actions/checkout@v2
      - name: Set up Python 3.9
        uses: actions/setup-python@v1
        with:
          python-version: 3.9
      - name: Install Black
        run: pip3 install git+git://github.com/psf/black
        
      - name: Run black --check .
        run: |
          black --check . || black --fast .
          git config --global user.name 'autoblack'
          git config --global user.email 'signorrayan@users.noreply.github.com'
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
          git checkout $GITHUB_HEAD_REF
          git commit -am "fixup: Format Python code with Black"
          git push

@im-mortal
Copy link

@signorrayan For actions/checkout@v2, you should use ${{ github.event.pull_request.head.sha }} as a git reference for a pull request event object.
Since github.event_name == 'pull_request' condition fails, this workflow starts from step Run black --check ., which, having no if condition itself, effectively checks out your repo using the non-existent branch.

Perhaps, you would be interested in ditching the first step altogether and enabling auto-merge instead.

However, would like to highlight the fact that you're asking for help instead of actually reporting a bug or suggesting a meaningful change to the code or the docs of this repository. Please close this issue and refer to my previous message to learn about the appropriate place to ask ad hoc questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants