|
| 1 | +name: 'Manual Status Setter (gh cli)' |
| 2 | + |
| 3 | +on: |
| 4 | + # This allows the workflow to be run manually from the Actions tab |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + pr_number: |
| 8 | + description: 'The number of the PR to post the status to' |
| 9 | + required: true |
| 10 | + type: string |
| 11 | + fail: |
| 12 | + required: false |
| 13 | + type: boolean |
| 14 | + default: false |
| 15 | + |
| 16 | +env: |
| 17 | + REPO_NAME: ${{ github.event.repository.name }} |
| 18 | + REPO_OWNER: ${{ github.repository_owner }} |
| 19 | + |
| 20 | +jobs: |
| 21 | + manual-check-job: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + # Step 1: Get the HEAD commit SHA of the Pull Request using gh api |
| 25 | + - name: 'Get PR SHA' |
| 26 | + id: get_sha |
| 27 | + env: |
| 28 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + run: | |
| 30 | + # Use gh api with the --jq flag to directly query the head commit SHA |
| 31 | + PR_SHA=$(gh api \ |
| 32 | + "/repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/pulls/${{ inputs.pr_number }}" \ |
| 33 | + --jq '.head.sha') |
| 34 | +
|
| 35 | + echo "Found SHA: $PR_SHA for PR #${{ github.event.inputs.pr_number }}" |
| 36 | + echo "sha=$PR_SHA" >> "$GITHUB_OUTPUT" |
| 37 | +
|
| 38 | + # Step 2: Set the initial "pending" status on that specific commit |
| 39 | + - name: 'Set initial pending status' |
| 40 | + env: |
| 41 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + run: | |
| 43 | + gh api \ |
| 44 | + --method POST \ |
| 45 | + "/repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/statuses/${{ steps.get_sha.outputs.sha }}" \ |
| 46 | + -f state='pending' \ |
| 47 | + -f context='continuous-integration/slow-ci' \ |
| 48 | + -f description='Performing slow CI checks...' |
| 49 | +
|
| 50 | + # Step 3: Run the actual task |
| 51 | + - name: 'Run complex task...' |
| 52 | + run: | |
| 53 | + echo "Doing some work for PR #${{ github.event.inputs.pr_number }}..." |
| 54 | + sleep 10 |
| 55 | + # This is where your logic would determine success or failure |
| 56 | + if [ "${{ inputs.fail }}" == "true"]; then |
| 57 | + echo "the 'fail' input was set to true. Simulating failure." |
| 58 | + exit 1 |
| 59 | + else |
| 60 | + echo "The 'fail' input was false. The step will succeed." |
| 61 | + fi |
| 62 | +
|
| 63 | + # Step 4: Set the final "success" status on the same commit |
| 64 | + - name: 'Set final success status' |
| 65 | + if: success() # Only run if previous steps succeeded |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + run: | |
| 69 | + gh api \ |
| 70 | + --method POST \ |
| 71 | + "/repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/statuses/${{ steps.get_sha.outputs.sha }}" \ |
| 72 | + -f state='success' \ |
| 73 | + -f context='continuous-integration/slow-ci' \ |
| 74 | + -f description='Custom checks passed!' |
| 75 | +
|
| 76 | + # Step 5 (Optional): Set a "failure" status if the job fails |
| 77 | + - name: 'Set final failure status' |
| 78 | + if: failure() # Only run if any previous step failed |
| 79 | + env: |
| 80 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + run: | |
| 82 | + gh api \ |
| 83 | + --method POST \ |
| 84 | + "/repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/statuses/${{ steps.get_sha.outputs.sha }}" \ |
| 85 | + -f state='failure' \ |
| 86 | + -f context='continuous-integration/slow-ci' \ |
| 87 | + -f description='Custom checks failed!' |
0 commit comments