stefanzweifel / git-auto-commit-action

Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
MIT License
1.97k stars 226 forks source link

How to get status check from commit through? #231

Closed alexisdrakopoulos closed 1 year ago

alexisdrakopoulos commented 2 years ago

Any advice on how to get the status checks to pass through without rerunning tests. I am using this action to update some badges which are outputs of the test coverage report. The action (due to pushing a commit) sadly resets the status check.

stefanzweifel commented 2 years ago

I assume you're talking about the missing status-check icons on the commits that have been pushed by the Action? You're PRs look like this:

Screenshot 2022-07-11 at 19 50 17

The answer I usually give to these questions is that you can switch to use private access tokens, so that the commits made by the Action will trigger your usual workflows: https://github.com/stefanzweifel/git-auto-commit-action#commits-made-by-this-action-do-not-trigger-new-workflow-runs.

But this can waste precious runner minutes.

I personally follow the mantra: If the changes committed by "git-auto-commit" does not impact the logic of my code, I don't bother with using a personal access token. It just wastes runner-minutes. I can see the status checks on the previous commit in the GitHub UI.

But I can see that this won't work if you use branch protection rules – which Github started to promote more heavily the last few days. If you use protected branches the "personal access token"-route is the only way to go.

alexisdrakopoulos commented 2 years ago

Actually it's worse in mine, since my branch requires tests to pass it just shows a yellow dot and lint_and_test Expected — Waiting for status to be reported rather than the big green tick.

alexisdrakopoulos commented 2 years ago

I'll try something like this:

      - uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: "[skip ci] Writing coverage files & updating readme"
          file_pattern: .coverage_scripts/coverage.md README.md
        # Reference: https://github.com/bahmutov/eleventy-example/blob/main/.github/workflows/ci.yml#L27
        # https://docs.github.com/en/rest/reference/repos#create-a-commit-status
        run: |
          curl --request POST \
          --url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
          --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
          --header 'content-type: application/json' \
          --data '{
            "context": "lint_and_test",
            "state": "success",
            "description": "Staging tests passed",
            "target_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
          }'
stefanzweifel commented 2 years ago

I see. I don't have much experience with protected branches. Give it a go with inline CURL command.

Or try the personal access token (it's not that complicated).

Or you could maybe update your lint_and_test workflow to also be triggered by repository_dispatch. That way you can manually trigger the workflow (not sure if this actually works)

-   uses: stefanzweifel/git-auto-commit-action@v4
    with:
      commit_message: "[skip ci] Writing coverage files & updating readme"
      file_pattern: .coverage_scripts/coverage.md README.md

    # https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#repository_dispatchs
    run: |
      curl --request POST \
      --url https://api.github.com/repos/${{ github.repository }}/actions/workflows/lint_and_test.yml/dispatches \
      --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
      --header 'content-type: application/json' \
      --data '{
        "ref": ${{ github.sha }},
      }'
xalakox commented 2 years ago

the github.sha contains the sha for the commit before the update, this worked for me :

    - name: Send the OK back to gitub
      run: |
        curl --request POST \
        --url https://api.github.com/repos/${{ github.repository }}/statuses/`git rev-parse HEAD` \
        --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
        --header 'content-type: application/json' \
        --data '{
          "context": "test",
          "state": "success",
          "description": "Staging tests passed",
          "target_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        }'