Skip to content

Commit b0d794d

Browse files
committed
ci(gh-actions/update-flake-lock): Enable GPG commit signing
1 parent 080d1d8 commit b0d794d

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

.github/workflows/reusable-update-flake-lock.yml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ on:
1414
default: ''
1515
required: false
1616
type: string
17+
sign-commits:
18+
description: 'Enable GPG commit signing'
19+
default: false
20+
required: false
21+
type: boolean
1722

1823
secrets:
1924
NIX_GITHUB_TOKEN:
@@ -31,6 +36,12 @@ on:
3136
CREATE_PR_APP_PRIVATE_KEY:
3237
description: Private key of the GitHub App used for opening pull requests.
3338
required: true
39+
GIT_GPG_SIGNING_SECRET_KEY:
40+
description: GPG secret key used to sign commits
41+
required: false
42+
GIT_GPG_SIGNING_PASSPHRASE:
43+
description: GPG passphrase for secret key
44+
required: false
3445

3546
outputs:
3647
pr-url:
@@ -61,13 +72,30 @@ jobs:
6172
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
6273
substituters: ${{ vars.SUBSTITUTERS }}
6374

75+
- name: Configure Git credentials
76+
run: |
77+
git config --local user.name "${{ vars.GIT_USER_NAME }}"
78+
git config --local user.email "${{ vars.GIT_USER_EMAIL }}"
79+
git config --local commit.gpgsign ${{ inputs.sign-commits }}
80+
81+
- name: Import GPG key with passphrase
82+
if: ${{ inputs.sign-commits }}
83+
env:
84+
GIT_GPG_SIGNING_SECRET_KEY: ${{ secrets.GIT_GPG_SIGNING_SECRET_KEY }}
85+
GIT_GPG_SIGNING_PASSPHRASE: ${{ secrets.GIT_GPG_SIGNING_PASSPHRASE }}
86+
run: |
87+
echo "$GIT_GPG_SIGNING_SECRET_KEY" \
88+
| gpg --batch --yes \
89+
--pinentry-mode loopback \
90+
--passphrase "$GIT_GPG_SIGNING_PASSPHRASE" \
91+
--import
92+
git config --local user.signingkey "${{ vars.GIT_GPG_SIGNING_KEY_ID }}"
93+
6494
- name: Run `nix flake update`
6595
id: update-lockfile
66-
run: |
67-
curl -fsSL --proto '=https' --tlsv1.2 \
68-
https://raw.githubusercontent.com/metacraft-labs/nixos-modules/main/scripts/commit_flake_update.bash \
69-
-o commit_flake_update.bash
70-
FLAKE_INPUT=${{ inputs.flake-input }} bash commit_flake_update.bash
96+
run: ${GITHUB_ACTION_PATH}/scripts/commit_flake_update.bash
97+
env:
98+
FLAKE_INPUT: ${{ inputs.flake-input }}
7199

72100
- uses: tibdex/[email protected]
73101
id: generate-token

.github/workflows/update-flake-lock.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ jobs:
1414
secrets: inherit
1515
with:
1616
runner: '["self-hosted", "Linux", "x86-64-v2"]'
17+
sign-commits: true

scripts/commit_flake_update.bash

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ set -euo pipefail
44

55
FLAKE_INPUT=${FLAKE_INPUT:-""}
66

7-
if ! git config --get user.name >/dev/null 2>&1 || \
8-
[ "$(git config --get user.name)" = "" ] ||
9-
! git config --get user.email >/dev/null 2>&1 || \
10-
[ "$(git config --get user.email)" = "" ]; then
11-
echo "git config user.{name,email} is not set - configuring"
12-
set -x
13-
git config --local user.email "[email protected]"
14-
git config --local user.name "beep boop"
7+
running_in_github_actions() {
8+
[ -n "$CI" ] && \
9+
[ -n "$GITHUB_REPOSITORY" ] && \
10+
[ -n "$GITHUB_RUN_ID" ] && \
11+
[ -n "$GITHUB_TOKEN" ] && \
12+
curl --silent --fail \
13+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
14+
-H "Accept: application/vnd.github.v3+json" \
15+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" > /dev/null 2>&1
16+
}
17+
18+
if running_in_github_actions; then
19+
echo "Running in GitHub Actions."
20+
git config --list --show-origin
1521
fi
1622

1723
current_commit="$(git rev-parse HEAD)"
1824
export PRE_COMMIT_ALLOW_NO_CONFIG=1
1925

20-
git config --list --show-origin
21-
2226
nix flake update $FLAKE_INPUT --accept-flake-config --commit-lock-file
2327
commit_after_update="$(git rev-parse HEAD)"
2428

0 commit comments

Comments
 (0)