Skip to content

Commit bdbf11e

Browse files
committed
scripts/setup-release-secrets.zsh
1 parent 810fc14 commit bdbf11e

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

scripts/setup-release-secrets.zsh

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,34 @@ if [[ -n "${GPG_PRIVATE_KEY:-}" ]]; then
3535
echo "Using provided GPG_PRIVATE_KEY..."
3636
print -r -- "$GPG_PRIVATE_KEY" | gh secret set GPG_PRIVATE_KEY --app actions ${REPO_FLAG:+${REPO_FLAG[@]}}
3737
else
38-
[[ -n "${GPG_KEY_ID:-}" ]] || { echo "Provide GPG_PRIVATE_KEY or GPG_KEY_ID" >&2; exit 1; }
38+
if [[ -z "${GPG_KEY_ID:-}" ]]; then
39+
echo "No GPG_PRIVATE_KEY or GPG_KEY_ID provided. Attempting auto-detect..."
40+
# Find first signing-capable secret key (sec) via machine-readable output
41+
# Field 1=type (sec), 5=keyid, 12=capabilities (contains 's' when signing)
42+
CANDIDATE=$(gpg --list-secret-keys --with-colons 2>/dev/null | awk -F: '$1=="sec" && $12 ~ /s/ {print $5; exit}')
43+
if [[ -n "$CANDIDATE" ]]; then
44+
echo "Auto-detected signing key: $CANDIDATE"
45+
GPG_KEY_ID="$CANDIDATE"
46+
else
47+
echo "Could not auto-detect a signing key. Available secret keys:" >&2
48+
gpg --list-secret-keys --keyid-format=long || true
49+
echo "Set GPG_KEY_ID or GPG_PRIVATE_KEY and re-run." >&2
50+
exit 1
51+
fi
52+
fi
3953
echo "Exporting secret key for $GPG_KEY_ID ..."
40-
gpg --armor --export-secret-keys "$GPG_KEY_ID" | gh secret set GPG_PRIVATE_KEY --app actions ${REPO_FLAG:+${REPO_FLAG[@]}}
54+
# Try non-interactive export using loopback pinentry and provided passphrase.
55+
# If the agent disallows loopback, this may still prompt; in that case, instruct manual export.
56+
if gpg --batch --yes --pinentry-mode loopback --passphrase "${GPG_PASSPHRASE:-}" --armor --export-secret-keys "$GPG_KEY_ID" 2>/dev/null | gh secret set GPG_PRIVATE_KEY --app actions ${REPO_FLAG:+${REPO_FLAG[@]}}; then
57+
:
58+
else
59+
echo "Non-interactive export failed. Listing keys and instructions:" >&2
60+
gpg --list-secret-keys --keyid-format=long || true
61+
echo "Workaround: export your key and re-run with GPG_PRIVATE_KEY env var:" >&2
62+
echo " gpg --armor --export-secret-keys $GPG_KEY_ID > /tmp/secret.asc" >&2
63+
echo " GPG_PRIVATE_KEY=\"\$(cat /tmp/secret.asc)\" $0 ${REPO_FLAG:+--repo ${2:-}}" >&2
64+
exit 1
65+
fi
4166
fi
4267

4368
print -r -- "$GPG_PASSPHRASE" | gh secret set GPG_PASSPHRASE --app actions ${REPO_FLAG:+${REPO_FLAG[@]}}
@@ -57,4 +82,3 @@ if [[ $MISSING -ne 0 ]]; then
5782
fi
5883

5984
echo "All secrets set. Done."
60-

0 commit comments

Comments
 (0)