Skip to content

Commit b3e3942

Browse files
authored
Fixes #524 - allow adding gpg keys to dev containers (#525)
Signed-off-by: Jesse Sanford <[email protected]>
1 parent 1e7ee13 commit b3e3942

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
88
},
99
"postCreateCommand": ".devcontainer/postCreateCommand.sh",
10+
"postStartCommand": ".devcontainer/postStartCommand.sh",
1011
"workspaceFolder": "/home/vscode/idpbuilder",
1112
"workspaceMount": "source=${localWorkspaceFolder},target=/home/vscode/idpbuilder,type=bind",
1213
"hostRequirements": {

.devcontainer/postCreateCommand.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,22 @@ if [ -n "$GIT_COMMITER_EMAIL" ]; then
3131
echo "Configuring git user.email to: $GIT_COMMITER_EMAIL"
3232
git config --global user.email "$GIT_COMMITER_EMAIL"
3333
fi
34+
35+
# 1. Configure GPG agent
36+
mkdir -p ~/.gnupg
37+
echo "pinentry-program /usr/bin/pinentry" > ~/.gnupg/gpg-agent.conf
38+
echo "allow-loopback-pinentry" >> ~/.gnupg/gpg-agent.conf
39+
40+
# 2. Configure GPG client
41+
echo "use-agent" > ~/.gnupg/gpg.conf
42+
echo "pinentry-mode loopback" >> ~/.gnupg/gpg.conf
43+
44+
# 3. Restart GPG agent and set environment
45+
gpgconf --kill gpg-agent
46+
export GPG_TTY=$(tty)
47+
echo 'export GPG_TTY=$(tty)' >> ~/.bashrc
48+
49+
# 4. Configure Git for GPG signing
50+
git config --global commit.gpgsign true
51+
git config --global tag.gpgsign true
52+
git config --global gpg.program gpg

.devcontainer/postStartCommand.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Import GPG key if both parts are available
4+
if [ -n "$GPG_SECRET_KEY_PART1" ] && [ -n "$GPG_SECRET_KEY_PART2" ]; then
5+
echo "Importing GPG key..."
6+
echo "$GPG_SECRET_KEY_PART1$GPG_SECRET_KEY_PART2" | tr -d "'" | base64 -d | gunzip | gpg --batch --yes --no-tty --import
7+
if [ $? -eq 0 ]; then
8+
echo "GPG key imported successfully"
9+
10+
# Automatically configure Git to use the imported key for signing
11+
echo "Configuring Git to use the imported GPG key..."
12+
GPG_KEY_ID=$(gpg --list-secret-keys --keyid-format LONG | grep -E "^sec" | head -1 | awk '{print $2}' | cut -d'/' -f2)
13+
14+
if [ -n "$GPG_KEY_ID" ]; then
15+
git config --global user.signingkey "$GPG_KEY_ID"
16+
echo "Git configured to use GPG key: $GPG_KEY_ID"
17+
else
18+
echo "Warning: Could not detect GPG key ID for Git configuration"
19+
fi
20+
else
21+
echo "Failed to import GPG key"
22+
fi
23+
else
24+
echo "GPG key parts not found, skipping GPG import"
25+
fi

0 commit comments

Comments
 (0)