Skip to content

Commit 853e529

Browse files
committed
fix(ci): pass the approval status SHA as a step output
Writing to $GITHUB_ENV under pull_request_target trips zizmor's github-env rule, which fails the code-scanning check. Step outputs carry the same values without granting later steps an attacker-shaped environment, and the resolved head SHA now also covers a workflow_dispatch run that fails after the pull request lookup. Signed-off-by: Jim Meyer <jimeyer@nvidia.com>
1 parent dad00ec commit 853e529

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

‎.github/workflows/core-approval.yml‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,8 @@ jobs:
3131
name: Publish core approval status
3232
if: github.repository_owner == 'NVIDIA'
3333
runs-on: ubuntu-latest
34-
# Seeded so the guard step below can still publish a red status when the
35-
# job fails before the publishing step resolves the head SHA itself — a
36-
# checkout failure, or a transient API error. Step-level writes to
37-
# $GITHUB_ENV override this for later steps, so post_status clearing it
38-
# still works. Empty only on workflow_dispatch, where no SHA is known until
39-
# the publishing step looks the pull request up.
4034
env:
4135
STATUS_CONTEXT: OpenShell / Core Approval
42-
STATUS_SHA: ${{ github.event.pull_request.head.sha || github.event.merge_group.head_sha }}
4336
steps:
4437
# Check out the default branch, never the pull request head. This job runs
4538
# with a write-capable token, so it must not fetch or execute contributor
@@ -55,6 +48,7 @@ jobs:
5548
persist-credentials: false
5649

5750
- name: Publish core approval status
51+
id: publish
5852
timeout-minutes: 10
5953
env:
6054
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -78,18 +72,16 @@ jobs:
7872
-f "description=$description" \
7973
-f "target_url=$target_url" >/dev/null
8074
# A status was published, so the guard step has nothing to add.
81-
# $GITHUB_ENV is read once after the step finishes and the last
82-
# write wins, so this correctly reflects whether any status was
83-
# posted.
84-
echo "STATUS_SHA=" >> "$GITHUB_ENV"
75+
# Step outputs are collected after the step finishes, including
76+
# when it fails, so this reaches the guard either way.
77+
echo "posted=true" >> "$GITHUB_OUTPUT"
8578
}
8679
8780
# A merge group only forms after the pull request satisfied this gate,
8881
# and approvals cannot change while an entry sits in the queue. Publish
8982
# success so the queue's required-check evaluation resolves instead of
9083
# waiting out check_response_timeout_minutes.
9184
if [ "$EVENT_NAME" = "merge_group" ]; then
92-
echo "STATUS_SHA=$MERGE_GROUP_SHA" >> "$GITHUB_ENV"
9385
post_status "$MERGE_GROUP_SHA" success \
9486
"Approval enforced at pull request" "$RUN_URL"
9587
exit 0
@@ -104,7 +96,7 @@ jobs:
10496
fi
10597
10698
HEAD_SHA=$(jq -r '.head.sha' <<< "$PR")
107-
echo "STATUS_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
99+
echo "head_sha=$HEAD_SHA" >> "$GITHUB_OUTPUT"
108100
AUTHOR=$(jq -r '.user.login' <<< "$PR")
109101
TARGET_URL="https://github.com/$GH_REPO/pull/$PR_NUMBER"
110102
@@ -132,13 +124,25 @@ jobs:
132124
# would otherwise sit at "Expected" forever. Publish red so the state is
133125
# visible and the job can be re-run.
134126
- name: Publish a failure status if none was published
135-
if: failure() && env.STATUS_SHA != ''
127+
if: failure() && steps.publish.outputs.posted != 'true'
136128
env:
137129
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
138130
GH_REPO: ${{ github.repository }}
131+
# The step output covers a failure after the pull request was looked
132+
# up, including on workflow_dispatch. The event payload covers a
133+
# failure before that — a failed checkout, or a transient API error.
134+
STATUS_SHA: ${{ steps.publish.outputs.head_sha || github.event.pull_request.head.sha || github.event.merge_group.head_sha }}
139135
shell: bash
140136
run: |
141137
set -euo pipefail
138+
139+
# workflow_dispatch aborting before the lookup leaves no SHA to
140+
# address. Nothing can be published; say so rather than failing here.
141+
if [ -z "$STATUS_SHA" ]; then
142+
echo "::warning::No head SHA resolved; cannot publish a failure status."
143+
exit 0
144+
fi
145+
142146
gh api --method POST "repos/$GH_REPO/statuses/$STATUS_SHA" \
143147
-f "state=failure" \
144148
-f "context=$STATUS_CONTEXT" \

0 commit comments

Comments
 (0)