|
5 | 5 | # PR branch ran each workflow twice (once per event). PR checks still report |
6 | 6 | # from the push run of the head commit. |
7 | 7 | push: |
| 8 | + # A push made with the workflow token starts no workflow, so the version PRs |
| 9 | + # that automation opens (Version Packages, Prepare Python Release) get no |
| 10 | + # push run. Their workflows dispatch this one on the PR's branch instead |
| 11 | + # (#5110): `workflow_dispatch` is the one event that token can start, and its |
| 12 | + # run's checks attach to the branch's head commit. The rollup a PR reads |
| 13 | + # leaves dispatched checks out, so the `dispatch-status` jobs below report |
| 14 | + # the run as a commit status instead. |
| 15 | + workflow_dispatch: |
8 | 16 |
|
9 | 17 | # Superseded runs on the same ref are cancelled (e.g. rapid amend-pushes), |
10 | 18 | # except on main, where every merge keeps its own CI run |
@@ -174,3 +182,78 @@ jobs: |
174 | 182 | with: |
175 | 183 | name: dist-${{ matrix.package }} |
176 | 184 | path: src/${{ matrix.package }}/dist/ |
| 185 | + |
| 186 | + # A dispatched run's checks attach to the branch's head commit but are left |
| 187 | + # out of its status rollup, which is what a pull request reads (verified on |
| 188 | + # #5110: 29 successful dispatched check runs, a rollup of null). So on a |
| 189 | + # dispatch, which is how a version PR opened by automation gets CI, these two |
| 190 | + # jobs mirror the run onto the commit as a status, which the rollup does |
| 191 | + # include: pending at the start, then the result of every job above. A push |
| 192 | + # run reports through its own checks and skips both. |
| 193 | + dispatch-status-pending: |
| 194 | + name: Report the dispatched run as pending |
| 195 | + if: github.event_name == 'workflow_dispatch' |
| 196 | + timeout-minutes: 5 |
| 197 | + runs-on: ubuntu-latest |
| 198 | + permissions: |
| 199 | + statuses: write |
| 200 | + steps: |
| 201 | + - name: Set the commit status to pending |
| 202 | + env: |
| 203 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 204 | + run: | |
| 205 | + gh api "repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA" \ |
| 206 | + -f state=pending -f context="Python (dispatched)" \ |
| 207 | + -f description="Dispatched run in progress" \ |
| 208 | + -f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" |
| 209 | +
|
| 210 | + # `always()`, so a cancelled run cannot leave the status pending. A run that |
| 211 | + # was cancelled because a newer dispatch on the same commit superseded it |
| 212 | + # posts nothing, and the newer run reports instead; any other cancellation |
| 213 | + # is reported as an error. A job skipped by its own `if:` counts as passed. |
| 214 | + dispatch-status: |
| 215 | + name: Report the dispatched run's result |
| 216 | + if: github.event_name == 'workflow_dispatch' && always() |
| 217 | + needs: |
| 218 | + [ |
| 219 | + dispatch-status-pending, |
| 220 | + detect-packages, |
| 221 | + script-tests, |
| 222 | + test, |
| 223 | + coverage, |
| 224 | + test-windows, |
| 225 | + build, |
| 226 | + ] |
| 227 | + timeout-minutes: 5 |
| 228 | + runs-on: ubuntu-latest |
| 229 | + permissions: |
| 230 | + statuses: write |
| 231 | + # To look for a newer dispatched run of this workflow on the commit. |
| 232 | + actions: read |
| 233 | + steps: |
| 234 | + - name: Set the commit status to the run's result |
| 235 | + env: |
| 236 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 237 | + GH_REPO: ${{ github.repository }} |
| 238 | + NEEDS: ${{ toJSON(needs) }} |
| 239 | + run: | |
| 240 | + if jq -e 'all(.[]; .result == "success" or .result == "skipped")' <<<"$NEEDS" >/dev/null; then |
| 241 | + STATE=success DESCRIPTION="Every job passed" |
| 242 | + elif jq -e 'any(.[]; .result == "failure")' <<<"$NEEDS" >/dev/null; then |
| 243 | + STATE=failure DESCRIPTION="A job failed; open the run for details" |
| 244 | + else |
| 245 | + NEWER=$(gh run list --workflow python.yml --event workflow_dispatch \ |
| 246 | + --commit "$GITHUB_SHA" --json databaseId \ |
| 247 | + --jq "[.[] | select(.databaseId > $GITHUB_RUN_ID)] | length") || NEWER=0 |
| 248 | + # A failed lookup counts as no newer run: an error status is |
| 249 | + # better than one left pending. |
| 250 | + if [ "${NEWER:-0}" -gt 0 ]; then |
| 251 | + echo "Superseded by a newer dispatched run, which reports instead" |
| 252 | + exit 0 |
| 253 | + fi |
| 254 | + STATE=error DESCRIPTION="The run was cancelled; dispatch it again" |
| 255 | + fi |
| 256 | + gh api "repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA" \ |
| 257 | + -f state="$STATE" -f context="Python (dispatched)" \ |
| 258 | + -f description="$DESCRIPTION" \ |
| 259 | + -f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" |
0 commit comments