Skip to content

Commit 65172bd

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

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

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

Lines changed: 35 additions & 2 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,35 @@ 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
6696
run: |
6797
curl -fsSL --proto '=https' --tlsv1.2 \
6898
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
99+
-o commit_flake_update.bash && \
100+
bash commit_flake_update.bash
101+
env:
102+
GITHUB_TOKEN: ${{ github.token }}
103+
FLAKE_INPUT: ${{ inputs.flake-input }}
71104

72105
- uses: tibdex/[email protected]
73106
id: generate-token

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ on:
1111
jobs:
1212
update-flake-lock:
1313
uses: ./.github/workflows/reusable-update-flake-lock.yml
14-
secrets: inherit
14+
secrets:
15+
NIX_GITHUB_TOKEN: ${{ github.token }}
16+
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
17+
CREATE_PR_APP_ID: ${{ secrets.CREATE_PR_APP_ID }}
18+
CREATE_PR_APP_PRIVATE_KEY: ${{ secrets.CREATE_PR_APP_PRIVATE_KEY }}
19+
GIT_GPG_SIGNING_SECRET_KEY: ${{ secrets.GIT_GPG_SIGNING_SECRET_KEY }}
20+
GIT_GPG_SIGNING_PASSPHRASE: ${{ secrets.GIT_GPG_SIGNING_PASSPHRASE }}
1521
with:
1622
runner: '["self-hosted", "Linux", "x86-64-v2"]'
23+
sign-commits: true

scripts/commit_flake_update.bash

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ 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"
7+
running_in_github_actions() {
128
set -x
13-
git config --local user.email "[email protected]"
14-
git config --local user.name "beep boop"
9+
[ -n "${CI:-}" ] && \
10+
[ -n "${GITHUB_REPOSITORY:-}" ] && \
11+
[ -n "${GITHUB_RUN_ID:-}" ] && \
12+
[ -n "${GITHUB_TOKEN:-}" ] && \
13+
curl --silent --fail \
14+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
15+
-H "Accept: application/vnd.github.v3+json" \
16+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" > /dev/null 2>&1
17+
}
18+
19+
if running_in_github_actions; then
20+
echo "Running in GitHub Actions."
21+
git config --list --show-origin
1522
fi
1623

1724
current_commit="$(git rev-parse HEAD)"
1825
export PRE_COMMIT_ALLOW_NO_CONFIG=1
1926

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

0 commit comments

Comments
 (0)