Skip to content

Commit 8da1b4c

Browse files
authored
Edburns/ghcp sp 122 java release improvements (#1514)
* On branch edburns/ghcp-sp-122-java-release-improvements modified: .github/aw/actions-lock.json modified: .github/workflows/java.notes.template modified: .github/workflows/release-changelog.lock.yml modified: .github/workflows/release-changelog.md Try to fix the 📦 [View on Maven Central]((central.sonatype.com/redacted) problem. * fix: recompile release-changelog workflow with gh-aw v0.74.4 to match CI * fix: add preflight job to validate token permissions before publishing * fix: use workflow dispatch probe instead of unsafe PUT for actions:write check
1 parent a8e3db5 commit 8da1b4c

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

.github/workflows/java-publish-maven.yml

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,52 @@ jobs:
3636
name: Preflight checks
3737
runs-on: ubuntu-latest
3838
steps:
39-
- name: Verify GITHUB_TOKEN can create releases
39+
- name: Verify JAVA_RELEASE_TOKEN can push to repository
4040
run: |
41-
# Test that the token has contents:write by checking repo permissions
42-
PERMS=$(gh api repos/${{ github.repository }} --jq '.permissions.push // false')
43-
if [ "$PERMS" != "true" ]; then
44-
echo "::error::GITHUB_TOKEN lacks push/write permission on this repository. GitHub Release creation will fail."
41+
# JAVA_RELEASE_TOKEN is used by actions/checkout and for:
42+
# - git push origin main (doc updates)
43+
# - mvn release:prepare -DpushChanges=true (release commits + tags)
44+
# - git revert + push (rollback on failure)
45+
# It must have push (contents:write) permission on this repo.
46+
PUSH=$(gh api repos/${{ github.repository }} --jq '.permissions.push // false')
47+
if [ "$PUSH" != "true" ]; then
48+
echo "::error::JAVA_RELEASE_TOKEN lacks push permission on ${{ github.repository }}. It is required for pushing release commits and tags to main."
4549
exit 1
4650
fi
47-
echo "GITHUB_TOKEN permissions OK"
51+
echo "JAVA_RELEASE_TOKEN push access OK"
4852
env:
49-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
GITHUB_TOKEN: ${{ secrets.JAVA_RELEASE_TOKEN }}
5054

51-
- name: Verify JAVA_RELEASE_GITHUB_TOKEN is valid
55+
- name: Verify JAVA_RELEASE_GITHUB_TOKEN can trigger workflows
5256
run: |
53-
# Test that the token can authenticate and trigger workflows
54-
USER=$(gh api user --jq '.login' 2>&1) || {
55-
echo "::error::JAVA_RELEASE_GITHUB_TOKEN is invalid or expired. Changelog trigger will fail."
57+
# JAVA_RELEASE_GITHUB_TOKEN is used for:
58+
# - gh workflow run release-changelog.lock.yml (requires actions:write)
59+
# Check the token's OAuth scopes for 'workflow' (classic PAT) or
60+
# attempt a workflow dispatch with a non-existent ref to verify write access
61+
# (fine-grained PAT — these don't expose scopes via X-OAuth-Scopes).
62+
SCOPES=$(gh api -i user 2>&1 | grep -i '^x-oauth-scopes:' | tr '[:upper:]' '[:lower:]' || true)
63+
if echo "$SCOPES" | grep -q 'workflow'; then
64+
echo "JAVA_RELEASE_GITHUB_TOKEN has 'workflow' scope (classic PAT)"
65+
elif [ -z "$SCOPES" ]; then
66+
# Fine-grained PAT: no X-OAuth-Scopes header returned.
67+
# Attempt a workflow dispatch against a non-existent ref. If the token
68+
# has actions:write, the API returns 422 (validation failed on ref).
69+
# If it lacks the permission, the API returns 403.
70+
HTTP_CODE=$(gh api -X POST \
71+
"repos/${{ github.repository }}/actions/workflows/release-changelog.lock.yml/dispatches" \
72+
-f ref="preflight-check-nonexistent-ref" \
73+
-f 'inputs[tag]=preflight-check' \
74+
--silent -i 2>&1 | head -1 | grep -oE '[0-9]{3}' || echo "000")
75+
if [ "$HTTP_CODE" = "403" ] || [ "$HTTP_CODE" = "000" ]; then
76+
echo "::error::JAVA_RELEASE_GITHUB_TOKEN lacks actions:write permission on ${{ github.repository }}. It cannot trigger the changelog generation workflow."
77+
exit 1
78+
fi
79+
# 422 = has write access but ref doesn't exist (expected), 204 would mean it dispatched (shouldn't happen with fake ref)
80+
echo "JAVA_RELEASE_GITHUB_TOKEN actions:write access OK (fine-grained PAT, dispatch returned HTTP ${HTTP_CODE})"
81+
else
82+
echo "::error::JAVA_RELEASE_GITHUB_TOKEN lacks 'workflow' scope. Found scopes: ${SCOPES}. It needs this scope to trigger changelog generation via gh workflow run."
5683
exit 1
57-
}
58-
echo "JAVA_RELEASE_GITHUB_TOKEN is valid (authenticated as: ${USER})"
84+
fi
5985
env:
6086
GITHUB_TOKEN: ${{ secrets.JAVA_RELEASE_GITHUB_TOKEN }}
6187

0 commit comments

Comments
 (0)