Skip to content

Commit b9eace8

Browse files
committed
Improve GPG import - use gpg-preset-passphrase to cache passphrase
Try to get keygrip from dry-run import, then preset passphrase in gpg-agent. This allows import to proceed without needing passphrase during import. Falls back to passphrase-fd method if keygrip extraction fails.
1 parent 75547a4 commit b9eace8

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

.github/workflows/main.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,28 @@ jobs:
243243
# Start gpg-agent with loopback pinentry (ignore error if already running)
244244
gpg-agent --daemon --allow-loopback-pinentry 2>&1 || true
245245
246-
# Import the subkey using passphrase
247-
# Write key to temp file, then import with passphrase
246+
# Import the subkey
247+
# First, try to get the keygrip from the key (dry-run import)
248248
KEY_FILE=$(mktemp)
249249
echo "$GPG_PRIVATE_KEY" > "$KEY_FILE"
250250
251-
# Import with passphrase via --passphrase-fd 0 (stdin)
252-
echo "$GPG_PASSPHRASE" | gpg --batch --yes --pinentry-mode loopback --passphrase-fd 0 --import "$KEY_FILE"
251+
# Try to import and capture keygrip if available
252+
IMPORT_OUTPUT=$(gpg --batch --import-options import-show --dry-run --import "$KEY_FILE" 2>&1 || true)
253+
KEYGRIP=$(echo "$IMPORT_OUTPUT" | grep -oP 'keygrip: \K[0-9A-F]{40}' | head -1)
254+
255+
# If we found a keygrip, preset the passphrase in gpg-agent
256+
if [ -n "$KEYGRIP" ]; then
257+
echo "$GPG_PASSPHRASE" | gpg-preset-passphrase --preset "$KEYGRIP" 2>&1 || true
258+
fi
259+
260+
# Now import the key (passphrase should be cached if keygrip was found)
261+
# If keygrip wasn't found, try import with passphrase directly
262+
if [ -n "$KEYGRIP" ]; then
263+
gpg --batch --yes --import "$KEY_FILE"
264+
else
265+
# Fallback: try with passphrase-fd
266+
echo "$GPG_PASSPHRASE" | gpg --batch --yes --pinentry-mode loopback --passphrase-fd 0 --import "$KEY_FILE"
267+
fi
253268
254269
# Clean up temp file
255270
rm -f "$KEY_FILE"

0 commit comments

Comments
 (0)