Skip to content

Commit 5520207

Browse files
authored
Update github-build.yml
1 parent cce6d7e commit 5520207

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

.github/workflows/github-build.yml

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ on:
1212
jobs:
1313
build:
1414
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
1517

1618
steps:
17-
- uses: actions/checkout@v5
19+
- name: Checkout
20+
uses: actions/checkout@v5
1821

19-
- name: Set up Java (always)
22+
# Always set up Java + Maven cache
23+
- name: Set up Java (baseline)
2024
uses: actions/setup-java@v5
2125
with:
2226
distribution: temurin
2327
java-version: '17'
2428
cache: maven
2529

26-
# Only import the GPG key when secrets are available AND the PR is from this repo
30+
# Import GPG only when secrets are present AND this isn't a forked PR
2731
- name: Import GPG (trusted contexts only)
28-
if: ${{ secrets.OSSRH_GPG_SECRET_KEY != '' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
32+
if: ${{ secrets.OSSRH_GPG_SECRET_KEY != '' && secrets.OSSRH_GPG_SECRET_KEY_PASSWORD != '' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
2933
uses: actions/setup-java@v5
3034
with:
3135
distribution: temurin
@@ -34,27 +38,40 @@ jobs:
3438
gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }}
3539
gpg-passphrase: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }}
3640

37-
- name: Allow loopback pinentry + show key (trusted only)
38-
if: ${{ secrets.OSSRH_GPG_SECRET_KEY != '' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
41+
- name: Configure pinentry & show key (trusted only)
42+
if: ${{ secrets.OSSRH_GPG_SECRET_KEY != '' && secrets.OSSRH_GPG_SECRET_KEY_PASSWORD != '' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
3943
run: |
4044
mkdir -p ~/.gnupg && chmod 700 ~/.gnupg
4145
echo 'allow-loopback-pinentry' >> ~/.gnupg/gpg-agent.conf || true
4246
gpgconf --kill gpg-agent || true
43-
gpg --batch --list-secret-keys --keyid-format LONG
47+
echo "=== Secret keys in CI keyring (if any) ==="
48+
gpg --batch --list-secret-keys --keyid-format LONG || true
49+
50+
# Decide if we can sign (i.e., a signing-capable key is present)
51+
- name: Decide whether we can sign
52+
id: signable
53+
shell: bash
54+
run: |
55+
if gpg --batch --with-colons --list-secret-keys 2>/dev/null | grep -E '^(sec|ssb):' >/dev/null; then
56+
echo "sign=true" >> "$GITHUB_OUTPUT"
57+
else
58+
echo "sign=false" >> "$GITHUB_OUTPUT"
59+
fi
4460
4561
- name: Build & Verify (sign if possible, otherwise skip)
4662
env:
4763
MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }}
64+
shell: bash
4865
run: |
49-
KEYID=$(gpg --batch --with-colons --list-secret-keys \
50-
| awk -F: '($1=="ssb" && $12 ~ /s/) || ($1=="sec" && $12 ~ /s/){print $5; exit}')
51-
if [ -n "$KEYID" ]; then
52-
echo "Signing with key $KEYID"
66+
if [ "${{ steps.signable.outputs.sign }}" = "true" ]; then
67+
KEYID=$(gpg --batch --with-colons --list-secret-keys \
68+
| awk -F: '($1=="ssb" && $12 ~ /s/) || ($1=="sec" && $12 ~ /s/){print $5; exit}')
69+
echo "Signing with key: $KEYID"
5370
mvn --no-transfer-progress -B --update-snapshots \
5471
-Dgpg.keyname="$KEYID" \
5572
clean verify
5673
else
57-
echo "No signing key in this context; skipping GPG signing."
74+
echo "No signing key available in this context; skipping GPG signing."
5875
mvn --no-transfer-progress -B --update-snapshots \
5976
-Dgpg.skip=true \
6077
clean verify

0 commit comments

Comments
 (0)