Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ The following inputs can be used to control the action's behavior:
* `ssh-agent-cmd`: Optional. Use this to specify a custom location for the `ssh-agent` binary.
* `ssh-add-cmd`: Optional. Use this to specify a custom location for the `ssh-add` binary.
* `git-cmd`: Optional. Use this to specify a custom location for the `git` binary.
* `git-global-config`: Optional. Alter the global git config? If `false` only the git config in the current working directory will be altered. Defaults to `false`.
> [!IMPORTANT]
>
> Using this option also sets the `GIT_CONFIG_NOSYSTEM` environment variable.
> This prevents the global git config from being read from the system-wide configuration file.

## Exported variables

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inputs:
git-cmd:
description: 'git command'
required: false
git-global-config:
description: 'Alter the global git configuration?'
default: false
runs:
using: 'node20'
main: 'dist/index.js'
Expand Down
11 changes: 8 additions & 3 deletions cleanup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const { execSync, execFileSync } = require("node:child_process");
const { keyFilePrefix } = require("./consts.js");
const { gitCmd, homePath, sshAgentCmd } = require("./paths.js");
const {
gitCmd,
gitGlobalConfig,
homePath,
sshAgentCmd,
} = require("./paths.js");
const { alterGitConfigWithRetry } = require("./utils.js");
const fs = require("node:fs");
const os = require("node:os");
Expand All @@ -20,7 +25,7 @@ function restoreGitConfig(maxTries = 3) {
console.log("Restoring git config");
const result = alterGitConfigWithRetry(() => {
return execSync(
`${gitCmd} config --global --get-regexp ".git@${keyFilePrefix}."`,
`${gitCmd} config${gitGlobalConfig} --get-regexp ".git@${keyFilePrefix}."`,
);
});
const sections = result
Expand All @@ -34,7 +39,7 @@ function restoreGitConfig(maxTries = 3) {
console.log(`Removing git config section ${section}`);
alterGitConfigWithRetry(() => {
return execSync(
`${gitCmd} config --global --remove-section ${section}`,
`${gitCmd} config${gitGlobalConfig} --remove-section ${section}`,
);
});
}
Expand Down
13 changes: 10 additions & 3 deletions dist/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3432,13 +3432,15 @@ const defaults =
const sshAgentCmdInput = core.getInput("ssh-agent-cmd");
const sshAddCmdInput = core.getInput("ssh-add-cmd");
const gitCmdInput = core.getInput("git-cmd");
const gitGlobalConfigInput = core.getBooleanInput("git-global-config");

module.exports = {
homePath: defaults.homePath,
sshAgentCmd:
sshAgentCmdInput !== "" ? sshAgentCmdInput : defaults.sshAgentCmdDefault,
sshAddCmd: sshAddCmdInput !== "" ? sshAddCmdInput : defaults.sshAddCmdDefault,
gitCmd: gitCmdInput !== "" ? gitCmdInput : defaults.gitCmdDefault,
gitGlobalConfig: gitGlobalConfigInput ? " --global" : "",
};


Expand Down Expand Up @@ -3660,7 +3662,12 @@ module.exports = require("util");
var __webpack_exports__ = {};
const { execSync, execFileSync } = __nccwpck_require__(421);
const { keyFilePrefix } = __nccwpck_require__(334);
const { gitCmd, homePath, sshAgentCmd } = __nccwpck_require__(644);
const {
gitCmd,
gitGlobalConfig,
homePath,
sshAgentCmd,
} = __nccwpck_require__(644);
const { alterGitConfigWithRetry } = __nccwpck_require__(561);
const fs = __nccwpck_require__(24);
const os = __nccwpck_require__(161);
Expand All @@ -3680,7 +3687,7 @@ function restoreGitConfig(maxTries = 3) {
console.log("Restoring git config");
const result = alterGitConfigWithRetry(() => {
return execSync(
`${gitCmd} config --global --get-regexp ".git@${keyFilePrefix}."`,
`${gitCmd} config${gitGlobalConfig} --get-regexp ".git@${keyFilePrefix}."`,
);
});
const sections = result
Expand All @@ -3694,7 +3701,7 @@ function restoreGitConfig(maxTries = 3) {
console.log(`Removing git config section ${section}`);
alterGitConfigWithRetry(() => {
return execSync(
`${gitCmd} config --global --remove-section ${section}`,
`${gitCmd} config${gitGlobalConfig} --remove-section ${section}`,
);
});
}
Expand Down
20 changes: 16 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3432,13 +3432,15 @@ const defaults =
const sshAgentCmdInput = core.getInput("ssh-agent-cmd");
const sshAddCmdInput = core.getInput("ssh-add-cmd");
const gitCmdInput = core.getInput("git-cmd");
const gitGlobalConfigInput = core.getBooleanInput("git-global-config");

module.exports = {
homePath: defaults.homePath,
sshAgentCmd:
sshAgentCmdInput !== "" ? sshAgentCmdInput : defaults.sshAgentCmdDefault,
sshAddCmd: sshAddCmdInput !== "" ? sshAddCmdInput : defaults.sshAddCmdDefault,
gitCmd: gitCmdInput !== "" ? gitCmdInput : defaults.gitCmdDefault,
gitGlobalConfig: gitGlobalConfigInput ? " --global" : "",
};


Expand Down Expand Up @@ -3670,7 +3672,13 @@ const core = __nccwpck_require__(484);
const child_process = __nccwpck_require__(421);
const fs = __nccwpck_require__(24);
const crypto = __nccwpck_require__(598);
const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = __nccwpck_require__(644);
const {
gitGlobalConfig,
homePath,
sshAgentCmd,
sshAddCmd,
gitCmd,
} = __nccwpck_require__(644);
const { keyFilePrefix } = __nccwpck_require__(334);
const { alterGitConfigWithRetry } = __nccwpck_require__(561);

Expand Down Expand Up @@ -3750,17 +3758,17 @@ try {

alterGitConfigWithRetry(() => {
return child_process.execSync(
`${gitCmd} config --global --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`,
`${gitCmd} config${gitGlobalConfig} --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`,
);
});
alterGitConfigWithRetry(() => {
return child_process.execSync(
`${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`,
`${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`,
);
});
alterGitConfigWithRetry(() => {
return child_process.execSync(
`${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`,
`${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`,
);
});

Expand All @@ -3772,6 +3780,10 @@ try {
`Added deploy-key mapping: Use identity '${homeSsh}/${keyFile}' for GitHub repository ${ownerAndRepo}`,
);
});
core.info(
"Setting GIT_CONFIG_NOSYSTEM. This prevents the global git config from being read from the system-wide configuration file.",
);
core.exportVariable("GIT_CONFIG_NOSYSTEM", "1");
} catch (error) {
if (error.code === "ENOENT") {
console.log(
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const core = require("@actions/core");
const child_process = require("node:child_process");
const fs = require("node:fs");
const crypto = require("node:crypto");
const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = require("./paths.js");
const {
gitGlobalConfig,
homePath,
sshAgentCmd,
sshAddCmd,
gitCmd,
} = require("./paths.js");
const { keyFilePrefix } = require("./consts.js");
const { alterGitConfigWithRetry } = require("./utils.js");

Expand Down Expand Up @@ -82,17 +88,17 @@ try {

alterGitConfigWithRetry(() => {
return child_process.execSync(
`${gitCmd} config --global --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`,
`${gitCmd} config${gitGlobalConfig} --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`,
);
});
alterGitConfigWithRetry(() => {
return child_process.execSync(
`${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`,
`${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`,
);
});
alterGitConfigWithRetry(() => {
return child_process.execSync(
`${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`,
`${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`,
);
});

Expand All @@ -104,6 +110,10 @@ try {
`Added deploy-key mapping: Use identity '${homeSsh}/${keyFile}' for GitHub repository ${ownerAndRepo}`,
);
});
core.info(
"Setting GIT_CONFIG_NOSYSTEM. This prevents the global git config from being read from the system-wide configuration file.",
);
core.exportVariable("GIT_CONFIG_NOSYSTEM", "1");
} catch (error) {
if (error.code === "ENOENT") {
console.log(
Expand Down
2 changes: 2 additions & 0 deletions paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ const defaults =
const sshAgentCmdInput = core.getInput("ssh-agent-cmd");
const sshAddCmdInput = core.getInput("ssh-add-cmd");
const gitCmdInput = core.getInput("git-cmd");
const gitGlobalConfigInput = core.getBooleanInput("git-global-config");

module.exports = {
homePath: defaults.homePath,
sshAgentCmd:
sshAgentCmdInput !== "" ? sshAgentCmdInput : defaults.sshAgentCmdDefault,
sshAddCmd: sshAddCmdInput !== "" ? sshAddCmdInput : defaults.sshAddCmdDefault,
gitCmd: gitCmdInput !== "" ? gitCmdInput : defaults.gitCmdDefault,
gitGlobalConfig: gitGlobalConfigInput ? " --global" : "",
};