From fab7456e45cbb91a88d6a1715603acfa468e2337 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 12 Nov 2026 13:55:21 +0100 Subject: [PATCH 1/5] Add `create-signed-commit` composite action Commit the staged changes through the GitHub REST Git Data API, so that GitHub signs the commit on behalf of the token owner, such as a GitHub App. `git commit -S` can't do this because a token has no private key. The action uses the REST API instead of `createCommitOnBranch` because it preserves file modes and symlinks and fits any number of changes in one commit. The `branch` input is required, so a workflow can't push to the default branch by accident. --- .../actions/create-signed-commit/action.yml | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 .github/actions/create-signed-commit/action.yml diff --git a/.github/actions/create-signed-commit/action.yml b/.github/actions/create-signed-commit/action.yml new file mode 100644 index 00000000..f841482c --- /dev/null +++ b/.github/actions/create-signed-commit/action.yml @@ -0,0 +1,172 @@ +name: Create signed commit +description: 'Commits the staged changes through the GitHub API, so that GitHub signs the commit on behalf of the token owner (e.g., a GitHub App), and pushes it to the given branch. Stage the files with `git add` first.' + +inputs: + github-token: + description: 'The token used to create the commit (e.g., a GitHub App installation token). It needs `contents: write`.' + required: false + default: ${{ github.token }} + branch: + description: 'The branch to push the signed commit to. It is created from the current HEAD if it does not exist on the remote.' + required: true + commit-message: + description: 'The commit message to use for the signed commit.' + required: true + +outputs: + commit-sha: + description: 'The SHA of the signed commit, or an empty string if there was nothing to commit.' + value: ${{ steps.commit.outputs.commit-sha }} + +runs: + using: composite + steps: + - name: Create signed commit + id: commit + uses: actions/github-script@v9 + env: + BRANCH: ${{ inputs.branch }} + COMMIT_MESSAGE: ${{ inputs.commit-message }} + with: + github-token: ${{ inputs.github-token }} + script: | + const fs = require('fs'); + + const { BRANCH: branch, COMMIT_MESSAGE: message } = process.env; + const { owner, repo } = context.repo; + + // Like `git commit`, only commit what the workflow has staged. Renames + // are disabled so that both the old and new path are listed. + const staged = await exec.getExecOutput( + 'git', + ['diff', '--cached', '--name-only', '-z', '--no-renames', 'HEAD'], + { silent: true }, + ); + const paths = new Set(staged.stdout.split('\0').filter(Boolean)); + + if (paths.size === 0) { + core.info('No changes to commit.'); + core.setOutput('commit-sha', ''); + return; + } + + const head = ( + await exec.getExecOutput('git', ['rev-parse', 'HEAD'], { + silent: true, + }) + ).stdout.trim(); + + const getBranchHead = async () => { + try { + const { data: ref } = await github.rest.git.getRef({ + owner, + repo, + ref: `heads/${branch}`, + }); + return ref.object.sha; + } catch (error) { + if (error.status === 404) { + return null; + } + + throw error; + } + }; + + const branchHead = await getBranchHead(); + const branchExists = branchHead !== null; + if (branchExists && branchHead !== head) { + throw new Error( + `Branch "${branch}" is at ${branchHead}, but the checkout is at ${head}. Check out the latest commit of the branch before creating a signed commit.`, + ); + } + + const tree = []; + for (const path of [...paths].sort()) { + let stat; + try { + stat = fs.lstatSync(path); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + + // The file was deleted. + tree.push({ path, mode: '100644', type: 'blob', sha: null }); + continue; + } + + if (stat.isDirectory()) { + throw new Error(`Cannot commit "${path}": Submodules are not supported.`); + } + + const isSymlink = stat.isSymbolicLink(); + const { data: blob } = await github.rest.git.createBlob({ + owner, + repo, + content: isSymlink + ? fs.readlinkSync(path) + : fs.readFileSync(path).toString('base64'), + encoding: isSymlink ? 'utf-8' : 'base64', + }); + + tree.push({ + path, + mode: isSymlink + ? '120000' + : stat.mode & 0o111 + ? '100755' + : '100644', + type: 'blob', + sha: blob.sha, + }); + } + + const { data: parent } = await github.rest.git.getCommit({ + owner, + repo, + commit_sha: head, + }); + + const { data: newTree } = await github.rest.git.createTree({ + owner, + repo, + base_tree: parent.tree.sha, + tree, + }); + + // The author and committer are deliberately omitted. GitHub only + // signs the commit when it sets them itself. + const { data: commit } = await github.rest.git.createCommit({ + owner, + repo, + message, + tree: newTree.sha, + parents: [head], + }); + + if (branchExists) { + await github.rest.git.updateRef({ + owner, + repo, + ref: `heads/${branch}`, + sha: commit.sha, + force: false, + }); + } else { + await github.rest.git.createRef({ + owner, + repo, + ref: `refs/heads/${branch}`, + sha: commit.sha, + }); + } + + core.info(`Created signed commit ${commit.sha} on "${branch}".`); + core.setOutput('commit-sha', commit.sha); + + // Move the local checkout onto the signed commit, so that later steps + // do not see the committed changes as pending. A mixed reset leaves + // any unstaged changes in the working tree. + await exec.exec('git', ['fetch', 'origin', commit.sha]); + await exec.exec('git', ['reset', commit.sha]); From 9efc0fd4e2cbef581b89d36f14b73a2f3bc8b008 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 12 Nov 2026 14:01:45 +0100 Subject: [PATCH 2/5] Sync the local checkout without persisted credentials Move the final `git fetch` and `git reset` out of the `github-script` step into their own step, and pass the token to the fetch in the URL. The action no longer depends on `actions/checkout` persisting credentials, so callers can use `persist-credentials: false`. The new step is skipped when there is nothing to commit. --- .github/actions/create-signed-commit/action.yml | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/actions/create-signed-commit/action.yml b/.github/actions/create-signed-commit/action.yml index f841482c..377b8686 100644 --- a/.github/actions/create-signed-commit/action.yml +++ b/.github/actions/create-signed-commit/action.yml @@ -165,8 +165,13 @@ runs: core.info(`Created signed commit ${commit.sha} on "${branch}".`); core.setOutput('commit-sha', commit.sha); - // Move the local checkout onto the signed commit, so that later steps - // do not see the committed changes as pending. A mixed reset leaves - // any unstaged changes in the working tree. - await exec.exec('git', ['fetch', 'origin', commit.sha]); - await exec.exec('git', ['reset', commit.sha]); + - name: Update local checkout + if: ${{ steps.commit.outputs.commit-sha != '' }} + shell: bash + env: + GITHUB_TOKEN: ${{ inputs.github-token }} + REPOSITORY: ${{ github.repository }} + COMMIT_SHA: ${{ steps.commit.outputs.commit-sha }} + run: | + git fetch "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}" "${COMMIT_SHA}" + git reset "${COMMIT_SHA}" From 9fef31dd55d708da7863d5e6d4eba8af79e76dac Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 12 Nov 2026 14:56:32 +0100 Subject: [PATCH 3/5] Commit the index instead of the working tree The action listed staged paths with `git diff --cached` but read contents, modes, and deletions from the working tree. As a result, it could commit unstaged edits, commit a `git rm --cached` removal as a modification, and ignore index modes and clean-filter or LFS blobs. Now the action reads the mode, blob SHA, and status of each change from `git diff --cached --raw` and uploads the staged blob with `git cat-file`. It parses the output with a named-group regex instead of pairing fields by position. --- .../actions/create-signed-commit/action.yml | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/.github/actions/create-signed-commit/action.yml b/.github/actions/create-signed-commit/action.yml index 377b8686..16ff200b 100644 --- a/.github/actions/create-signed-commit/action.yml +++ b/.github/actions/create-signed-commit/action.yml @@ -30,21 +30,35 @@ runs: with: github-token: ${{ inputs.github-token }} script: | - const fs = require('fs'); + const { execFileSync } = require('child_process'); const { BRANCH: branch, COMMIT_MESSAGE: message } = process.env; const { owner, repo } = context.repo; - // Like `git commit`, only commit what the workflow has staged. Renames - // are disabled so that both the old and new path are listed. + // Like `git commit`, only commit what the workflow has staged. + // Renames are disabled so that both the old and new path are + // listed. const staged = await exec.getExecOutput( 'git', - ['diff', '--cached', '--name-only', '-z', '--no-renames', 'HEAD'], + [ + 'diff', + '--cached', + '--raw', + '-z', + '--no-abbrev', + '--no-renames', + 'HEAD', + ], { silent: true }, ); - const paths = new Set(staged.stdout.split('\0').filter(Boolean)); - if (paths.size === 0) { + const changes = [ + ...staged.stdout.matchAll( + /:\d+ (?\d+) [0-9a-f]+ (?[0-9a-f]+) (?[A-Z])\0(?[^\0]+)\0/gu, + ), + ]; + + if (changes.length === 0) { core.info('No changes to commit.'); core.setOutput('commit-sha', ''); return; @@ -82,44 +96,32 @@ runs: } const tree = []; - for (const path of [...paths].sort()) { - let stat; - try { - stat = fs.lstatSync(path); - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } - - // The file was deleted. + for (const { + groups: { path, mode, sha, status }, + } of changes) { + if (status === 'D') { tree.push({ path, mode: '100644', type: 'blob', sha: null }); continue; } - if (stat.isDirectory()) { + if (mode === '160000') { throw new Error(`Cannot commit "${path}": Submodules are not supported.`); } - const isSymlink = stat.isSymbolicLink(); + // The blob in the index is the content git would commit. This also + // covers symlinks, whose blob is the link target. + const content = execFileSync('git', ['cat-file', 'blob', sha], { + maxBuffer: 100 * 1024 * 1024, + }); + const { data: blob } = await github.rest.git.createBlob({ owner, repo, - content: isSymlink - ? fs.readlinkSync(path) - : fs.readFileSync(path).toString('base64'), - encoding: isSymlink ? 'utf-8' : 'base64', + content: content.toString('base64'), + encoding: 'base64', }); - tree.push({ - path, - mode: isSymlink - ? '120000' - : stat.mode & 0o111 - ? '100755' - : '100644', - type: 'blob', - sha: blob.sha, - }); + tree.push({ path, mode, type: 'blob', sha: blob.sha }); } const { data: parent } = await github.rest.git.getCommit({ From 9f7f0a52e9ebf1fefa80fa61ac37eb682903716e Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 12 Nov 2026 15:17:18 +0100 Subject: [PATCH 4/5] Clear the persisted `extraheader` when syncing the checkout The sync step passes the token in the fetch URL, but the default `actions/checkout` persists its own `Authorization` header in `.git/config`. If the first request is challenged, git can send both headers, and GitHub rejects the duplicate. This happens after the signed commit has already landed. Pass `-c http.https://github.com/.extraheader=` to the fetch so only the token from the URL is sent, whichever token the checkout used. --- .github/actions/create-signed-commit/action.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/actions/create-signed-commit/action.yml b/.github/actions/create-signed-commit/action.yml index 16ff200b..7f376e5d 100644 --- a/.github/actions/create-signed-commit/action.yml +++ b/.github/actions/create-signed-commit/action.yml @@ -175,5 +175,7 @@ runs: REPOSITORY: ${{ github.repository }} COMMIT_SHA: ${{ steps.commit.outputs.commit-sha }} run: | - git fetch "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}" "${COMMIT_SHA}" + # Clear the `Authorization` header that `actions/checkout` persists, so + # that it is not sent in addition to the token in the URL. + git -c http.https://github.com/.extraheader= fetch "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}" "${COMMIT_SHA}" git reset "${COMMIT_SHA}" From 6b4d97b24a20a89758fce8a583a6426adf32dc58 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 13 Nov 2026 11:04:41 +0100 Subject: [PATCH 5/5] Only move the local branch when it is the one committed to The sync step ran `git reset` on whatever HEAD pointed at. When the signed commit was published to a different branch, the checked-out local branch ended up ahead of its remote, and a later `git push` could publish the commit there too. The fetch by SHA also left the remote-tracking branch stale. Fetch the published branch into `refs/remotes/origin/`, and reset only if HEAD is detached or on that branch. In any other case the checkout stays unchanged, with the changes still staged. --- .github/actions/create-signed-commit/action.yml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/actions/create-signed-commit/action.yml b/.github/actions/create-signed-commit/action.yml index 7f376e5d..fece9eb3 100644 --- a/.github/actions/create-signed-commit/action.yml +++ b/.github/actions/create-signed-commit/action.yml @@ -1,5 +1,5 @@ name: Create signed commit -description: 'Commits the staged changes through the GitHub API, so that GitHub signs the commit on behalf of the token owner (e.g., a GitHub App), and pushes it to the given branch. Stage the files with `git add` first.' +description: 'Commits the staged changes through the GitHub API, so that GitHub signs the commit on behalf of the token owner (e.g., a GitHub App), and pushes it to the given branch. Stage the files with `git add` first. Afterwards, the checkout is moved onto the signed commit if HEAD is detached or on the given branch. Otherwise, it is left unchanged.' inputs: github-token: @@ -173,9 +173,16 @@ runs: env: GITHUB_TOKEN: ${{ inputs.github-token }} REPOSITORY: ${{ github.repository }} + BRANCH: ${{ inputs.branch }} COMMIT_SHA: ${{ steps.commit.outputs.commit-sha }} run: | # Clear the `Authorization` header that `actions/checkout` persists, so # that it is not sent in addition to the token in the URL. - git -c http.https://github.com/.extraheader= fetch "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}" "${COMMIT_SHA}" - git reset "${COMMIT_SHA}" + git -c http.https://github.com/.extraheader= fetch "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}" "+refs/heads/${BRANCH}:refs/remotes/origin/${BRANCH}" + + # Only move HEAD if it is detached or on the branch that was committed + # to. + current="$(git symbolic-ref --short -q HEAD || true)" + if [[ -z "${current}" || "${current}" == "${BRANCH}" ]]; then + git reset "${COMMIT_SHA}" + fi