python-3.x 在github actions工作流结果状态中排除退出代码

d8tt03nd  于 2023-06-07  发布在  Python
关注(0)|答案(2)|浏览(177)

我有一个工作流,在其中一个步骤中,如果命令以退出代码1(失败)结束,我想运行下一个命令/作业(修复导致上一个命令失败的问题),但我不希望退出代码1影响工作流结果状态。
在这种情况下,如果我有退出代码1,即使我修复了问题,结果状态也将是failure,但如果第二个命令修复了问题,我希望结果状态是success
这可能吗?
下面是我的workflow.yml
\

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"

如果带有run: black --check .命令的作业build的退出代码为1,我希望运行作业reformat来修复问题并将其推送到存储库。第二项工作工作正常,但最终的结果标签是失败!!

n3schb8v

n3schb8v1#

stepcontinue-on-error属性(在作业上也可用)可用于防止在步骤失败时作业失败。
然后,steps上下文包含一个steps.<step_id>.outcome,它可以在表达式中用于确定前一步是否失败。例如,在下面的工作流中,我有一个步骤,当commitlint命令失败时会失败,但它使用continue-on-error来允许工作流继续:

- 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

接下来的两个步骤将根据commitlint步骤的成功/失败结果应用或删除标签:

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

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

无论提交步骤是否由于continue-on-error: true而失败,工作流最终都会成功。
完整的工作流程是here

vsdwdz23

vsdwdz232#

例如,可以使用if条件设置输出参数,并在后续步骤中引用它

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"

相关问题