Home > front end >  How to associate status of a Github action workflow with a commit or PR
How to associate status of a Github action workflow with a commit or PR

Time:06-16

I have a Github action workflow which is set to trigger on issue_comment, for example the following

name: Testing actions
on:
  issue_comment:
    types: [created, edited]

jobs:
  testing-actions:
    if: github.event.issue.pull_request && contains(github.event.comment.body, '@test')
    name: Just testing
    runs-on: ubuntu-latest
    steps:
      - uses: xt0rted/pull-request-comment-branch@v1
        id: comment-branch
      - uses: actions/checkout@v3
        with:
          ref: ${{ steps.comment-branch.outputs.head_ref }}
      - name: Print stuff
        run: echo "Hello dudes!"

The workflow is working, it is getting triggered when I comment "@test" on a PR. But I have to check it from the actions tab, the workflow doesn't get associated with any commit or PR, so it doesn't get shown in the checks tab in the PR, or the commits don't show any commit status.

How do I get the workflow status and details in the PR page, so that I don't have to go to actions tab manually to see the results?

CodePudding user response:

I solved this by manually setting the commit status using @myrotvorets/set-commit-status-action. The workflow file ended up being like the following

name: Testing actions
on:
  issue_comment:
    types: [created, edited]

jobs:
  testing-actions:
    if: github.event.issue.pull_request && contains(github.event.comment.body, '@test')
    name: Just testing
    runs-on: ubuntu-latest
    steps:
      - name: Get PR details
        uses: xt0rted/pull-request-comment-branch@v1
        id: comment-branch
      - name: Set commit status as pending
        uses: myrotvorets/set-commit-status-action@master
        with:
          sha: ${{ steps.comment-branch.outputs.head_sha }}
          token: ${{ secrets.GITHUB_TOKEN }}
          status: pending

      - uses: actions/checkout@v3
        with:
          ref: ${{ steps.comment-branch.outputs.head_ref }}
      - name: Print stuff
        run: echo "Hello dudes!"

      - name: Set final commit status
        uses: myrotvorets/set-commit-status-action@master
        if: always()
        with:
          sha: ${{ steps.comment-branch.outputs.head_sha }}
          token: ${{ secrets.GITHUB_TOKEN }}
          status: ${{ job.status }}

This works pretty well, the workflow runs get associated with the PR's head commit, and thus it shows up on the PR page as a check too. Thanks @rethab for the alpha.

  • Related