Skip to content

Commit 2d53338

Browse files
committed
Fix deletion of GPG keys after release
It previously didn't work because we need to provide a fingerprint (not the key ID) when deleting a GPG key in batch mode. This deletion is probably unnecessary anyway, because we delete the GPG homedir afterwards, but better safe than sorry.
1 parent 3c33911 commit 2d53338

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

release.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,31 @@ if [ -z "$RELEASE_GPG_PRIVATE_KEY_PATH" ]; then
110110
exit 1
111111
fi
112112

113+
#--------------------------------------------
114+
# GPG
115+
116+
function gpg_import() {
117+
local privateKeyPath="$1"
118+
shift
119+
local keyId
120+
keyId=$(gpg "${@}" --batch --import "$privateKeyPath" 2>&1 | tee /dev/stderr | grep 'key.*: secret key imported' | sed -E 's/.*key ([^:]+):.*/\1/')
121+
# output the fingerprint of the imported key
122+
gpg "${@}" --list-secret-keys --with-colon "$keyId" | sed -E '2!d;s/.*:([^:]+):$/\1/'
123+
}
124+
125+
function gpg_delete() {
126+
local fingerprint="$1"
127+
shift
128+
gpg "${@}" --batch --yes --delete-secret-keys "$fingerprint"
129+
}
130+
113131
#--------------------------------------------
114132
# Cleanup on exit
115133

116134
function cleanup() {
117135
if [ -n "$IMPORTED_KEY" ]; then
118136
echo "Deleting imported GPG private key..."
119-
gpg --homedir="$RELEASE_GPG_HOMEDIR" --batch --yes --delete-secret-keys "$IMPORTED_KEY" || true
137+
gpg_delete "$IMPORTED_KEY" || true
120138
fi
121139
if [ -d "$RELEASE_GPG_HOMEDIR" ]; then
122140
echo "Cleaning up GPG homedir..."
@@ -136,7 +154,8 @@ if [ -e "$RELEASE_GPG_HOMEDIR" ]; then
136154
exit 1
137155
fi
138156
mkdir -p -m 700 "$RELEASE_GPG_HOMEDIR"
139-
IMPORTED_KEY="$(gpg --homedir="$RELEASE_GPG_HOMEDIR" --batch --import "$RELEASE_GPG_PRIVATE_KEY_PATH" 2>&1 | tee /dev/stderr | grep 'key.*: secret key imported' | sed -E 's/.*key ([^:]+):.*/\1/')"
157+
export GNUPGHOME="$RELEASE_GPG_HOMEDIR"
158+
IMPORTED_KEY="$(gpg_import "$RELEASE_GPG_PRIVATE_KEY_PATH")"
140159
if [ -z "$IMPORTED_KEY" ]; then
141160
echo "Failed to import GPG key"
142161
exit 1

0 commit comments

Comments
 (0)