From ae17b9f8de2a893cba185eaacf25d8a67558aeb8 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 10 Aug 2021 09:28:13 +0200 Subject: [PATCH] Add `git-config-global` input (#103) Co-authored-by: CrazyMax --- .github/workflows/ci.yml | 4 ++++ README.md | 1 + action.yml | 4 ++++ dist/index.js | 22 ++++++++++++++-------- src/context.ts | 2 ++ src/git.ts | 9 +++++++-- src/main.ts | 12 ++++++------ 7 files changed, 38 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce3e29fd..a9a55dfb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,9 @@ jobs: strategy: fail-fast: false matrix: + global: + - false + - true os: - ubuntu-latest - macOS-latest @@ -44,6 +47,7 @@ jobs: with: gpg-private-key: ${{ steps.test.outputs.pgp }} passphrase: ${{ steps.test.outputs.passphrase }} + git-config-global: ${{ matrix.global }} git-user-signingkey: true git-commit-gpgsign: true git-tag-gpgsign: true diff --git a/README.md b/README.md index 9f80ac31..83e5e76b 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Following inputs can be used as `step.with` keys |---------------------------------------|---------|------------------------------------------------| | `gpg-private-key` | String | GPG private key exported as an ASCII armored version or its base64 encoding (**required**) | | `passphrase` | String | Passphrase of the GPG private key | +| `git-config-global` | Bool | Set Git config global (default `false`) | | `git-user-signingkey` | Bool | Set GPG signing keyID for this Git repository (default `false`) | | `git-commit-gpgsign`**ยน** | Bool | Sign all commits automatically. (default `false`) | | `git-tag-gpgsign`**ยน** | Bool | Sign all tags automatically. (default `false`) | diff --git a/action.yml b/action.yml index a699ff2b..c23dfcbd 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,10 @@ inputs: passphrase: description: 'Passphrase of the GPG private key' required: false + git-config-global: + description: 'Set Git config global' + default: 'false' + required: false git-user-signingkey: description: 'Set GPG signing keyID for this Git repository' default: 'false' diff --git a/dist/index.js b/dist/index.js index bf978ed1..415ac3e5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -44,6 +44,7 @@ function getInputs() { return { gpgPrivateKey: core.getInput('gpg-private-key', { required: true }), passphrase: core.getInput('passphrase'), + gitConfigGlobal: core.getBooleanInput('git-config-global'), gitUserSigningkey: core.getBooleanInput('git-user-signingkey'), gitCommitGpgsign: core.getBooleanInput('git-commit-gpgsign'), gitTagGpgsign: core.getBooleanInput('git-tag-gpgsign'), @@ -113,9 +114,14 @@ const git = (args = []) => __awaiter(void 0, void 0, void 0, function* () { return res.stdout.trim(); }); }); -function setConfig(key, value) { +function setConfig(key, value, global) { return __awaiter(this, void 0, void 0, function* () { - yield git(['config', key, value]); + let args = ['config']; + if (global) { + args.push('--global'); + } + args.push(key, value); + yield git(args); }); } exports.setConfig = setConfig; @@ -430,7 +436,7 @@ function run() { context.setOutput('email', privateKey.email); if (inputs.gitUserSigningkey) { core.info('๐Ÿ” Setting GPG signing keyID for this Git repository'); - yield git.setConfig('user.signingkey', privateKey.keyID); + yield git.setConfig('user.signingkey', privateKey.keyID, inputs.gitConfigGlobal); const userEmail = inputs.gitCommitterEmail || privateKey.email; const userName = inputs.gitCommitterName || privateKey.name; if (userEmail != privateKey.email) { @@ -438,19 +444,19 @@ function run() { return; } core.info(`๐Ÿ”จ Configuring Git committer (${userName} <${userEmail}>)`); - yield git.setConfig('user.name', userName); - yield git.setConfig('user.email', userEmail); + yield git.setConfig('user.name', userName, inputs.gitConfigGlobal); + yield git.setConfig('user.email', userEmail, inputs.gitConfigGlobal); if (inputs.gitCommitGpgsign) { core.info('๐Ÿ’Ž Sign all commits automatically'); - yield git.setConfig('commit.gpgsign', 'true'); + yield git.setConfig('commit.gpgsign', 'true', inputs.gitConfigGlobal); } if (inputs.gitTagGpgsign) { core.info('๐Ÿ’Ž Sign all tags automatically'); - yield git.setConfig('tag.gpgsign', 'true'); + yield git.setConfig('tag.gpgsign', 'true', inputs.gitConfigGlobal); } if (inputs.gitPushGpgsign) { core.info('๐Ÿ’Ž Sign all pushes automatically'); - yield git.setConfig('push.gpgsign', inputs.gitPushGpgsign); + yield git.setConfig('push.gpgsign', inputs.gitPushGpgsign, inputs.gitConfigGlobal); } } } diff --git a/src/context.ts b/src/context.ts index 2e1282cf..15776d57 100644 --- a/src/context.ts +++ b/src/context.ts @@ -4,6 +4,7 @@ import {issueCommand} from '@actions/core/lib/command'; export interface Inputs { gpgPrivateKey: string; passphrase: string; + gitConfigGlobal: boolean; gitUserSigningkey: boolean; gitCommitGpgsign: boolean; gitTagGpgsign: boolean; @@ -17,6 +18,7 @@ export async function getInputs(): Promise { return { gpgPrivateKey: core.getInput('gpg-private-key', {required: true}), passphrase: core.getInput('passphrase'), + gitConfigGlobal: core.getBooleanInput('git-config-global'), gitUserSigningkey: core.getBooleanInput('git-user-signingkey'), gitCommitGpgsign: core.getBooleanInput('git-commit-gpgsign'), gitTagGpgsign: core.getBooleanInput('git-tag-gpgsign'), diff --git a/src/git.ts b/src/git.ts index a9c62774..49f536bc 100644 --- a/src/git.ts +++ b/src/git.ts @@ -14,6 +14,11 @@ const git = async (args: string[] = []): Promise => { }); }; -export async function setConfig(key: string, value: string): Promise { - await git(['config', key, value]); +export async function setConfig(key: string, value: string, global: boolean): Promise { + let args: Array = ['config']; + if (global) { + args.push('--global'); + } + args.push(key, value); + await git(args); } diff --git a/src/main.ts b/src/main.ts index 5088abba..f04a3deb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -63,7 +63,7 @@ async function run(): Promise { if (inputs.gitUserSigningkey) { core.info('๐Ÿ” Setting GPG signing keyID for this Git repository'); - await git.setConfig('user.signingkey', privateKey.keyID); + await git.setConfig('user.signingkey', privateKey.keyID, inputs.gitConfigGlobal); const userEmail = inputs.gitCommitterEmail || privateKey.email; const userName = inputs.gitCommitterName || privateKey.name; @@ -74,20 +74,20 @@ async function run(): Promise { } core.info(`๐Ÿ”จ Configuring Git committer (${userName} <${userEmail}>)`); - await git.setConfig('user.name', userName); - await git.setConfig('user.email', userEmail); + await git.setConfig('user.name', userName, inputs.gitConfigGlobal); + await git.setConfig('user.email', userEmail, inputs.gitConfigGlobal); if (inputs.gitCommitGpgsign) { core.info('๐Ÿ’Ž Sign all commits automatically'); - await git.setConfig('commit.gpgsign', 'true'); + await git.setConfig('commit.gpgsign', 'true', inputs.gitConfigGlobal); } if (inputs.gitTagGpgsign) { core.info('๐Ÿ’Ž Sign all tags automatically'); - await git.setConfig('tag.gpgsign', 'true'); + await git.setConfig('tag.gpgsign', 'true', inputs.gitConfigGlobal); } if (inputs.gitPushGpgsign) { core.info('๐Ÿ’Ž Sign all pushes automatically'); - await git.setConfig('push.gpgsign', inputs.gitPushGpgsign); + await git.setConfig('push.gpgsign', inputs.gitPushGpgsign, inputs.gitConfigGlobal); } } } catch (error) {