Skip to content

Commit

Permalink
Provide gitPath for Windows to avoid failures on windows-2022 (Gi…
Browse files Browse the repository at this point in the history
…tHub-hosted runner) (#137)

### Problem:
Observed error on `windows-2022` ([GitHub-hosted runner](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources)) that `git` command cannot be found.

### Issue: 

Cannot find git executable on on windows-2022 (GitHub-hosted runner) #136 

### Solution:
This path improvement makes use of existing `path.js` to resolve and return correct `git.exe` path for Windows, leaving the executable name as it was for other operating systems.

### Caveats:
No idea how and why this `c://progra~1//git//usr//bin//git.exe` mumbo-jumbo works but it apparently did for other executables so figured it should work for `git.exe` (and it does).
  • Loading branch information
ochococo authored Oct 19, 2022
1 parent fbef2c7 commit df2f741
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 55 deletions.
4 changes: 2 additions & 2 deletions cleanup.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const core = require('@actions/core');
const { execFileSync } = require('child_process');
const { sshAgent } = require('./paths.js');
const { sshAgentCmd } = require('./paths.js');

try {
// Kill the started SSH agent
console.log('Stopping SSH agent');
execFileSync(sshAgent, ['-k'], { stdio: 'inherit' });
execFileSync(sshAgentCmd, ['-k'], { stdio: 'inherit' });
} catch (error) {
console.log(error.message);
console.log('Error stopping the SSH agent, proceeding anyway');
Expand Down
24 changes: 11 additions & 13 deletions dist/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,12 @@ exports.debug = debug; // for test

const core = __webpack_require__(470);
const { execFileSync } = __webpack_require__(129);
const { sshAgent } = __webpack_require__(972);
const { sshAgentCmd } = __webpack_require__(972);

try {
// Kill the started SSH agent
console.log('Stopping SSH agent');
execFileSync(sshAgent, ['-k'], { stdio: 'inherit' });
execFileSync(sshAgentCmd, ['-k'], { stdio: 'inherit' });
} catch (error) {
console.log(error.message);
console.log('Error stopping the SSH agent, proceeding anyway');
Expand Down Expand Up @@ -2824,23 +2824,21 @@ exports.default = _default;
const os = __webpack_require__(87);

module.exports = (process.env['OS'] != 'Windows_NT') ? {

// Use getent() system call, since this is what ssh does; makes a difference in Docker-based
// Action runs, where $HOME is different from the pwent
home: os.userInfo().homedir,
sshAgent: 'ssh-agent',
sshAdd: 'ssh-add'

homePath: os.userInfo().homedir,
sshAgentCmd: 'ssh-agent',
sshAddCmd: 'ssh-add',
gitCmd: 'git'
} : {

home: os.homedir(),
sshAgent: 'c://progra~1//git//usr//bin//ssh-agent.exe',
sshAdd: 'c://progra~1//git//usr//bin//ssh-add.exe'

// Assuming GitHub hosted `windows-*` runners for now
homePath: os.homedir(),
sshAgentCmd: 'c://progra~1//git//usr//bin//ssh-agent.exe',
sshAddCmd: 'c://progra~1//git//usr//bin//ssh-add.exe',
gitCmd: 'c://progra~1//git//usr//bin//git.exe'
};



/***/ })

/******/ });
38 changes: 18 additions & 20 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const core = __webpack_require__(470);
const child_process = __webpack_require__(129);
const fs = __webpack_require__(747);
const crypto = __webpack_require__(417);
const { home, sshAgent, sshAdd } = __webpack_require__(972);
const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = __webpack_require__(972);

try {
const privateKey = core.getInput('ssh-private-key');
Expand All @@ -334,7 +334,7 @@ try {
return;
}

const homeSsh = home + '/.ssh';
const homeSsh = homePath + '/.ssh';

console.log(`Adding GitHub.com keys to ${homeSsh}/known_hosts`);

Expand All @@ -349,7 +349,7 @@ try {
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];

// Extract auth socket path and agent pid and set them as job variables
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
child_process.execFileSync(sshAgentCmd, sshAgentArgs).toString().split("\n").forEach(function(line) {
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);

if (matches && matches.length > 0) {
Expand All @@ -362,16 +362,16 @@ try {
console.log("Adding private key(s) to agent");

privateKey.split(/(?=-----BEGIN)/).forEach(function(key) {
child_process.execFileSync(sshAdd, ['-'], { input: key.trim() + "\n" });
child_process.execFileSync(sshAddCmd, ['-'], { input: key.trim() + "\n" });
});

console.log("Key(s) added:");

child_process.execFileSync(sshAdd, ['-l'], { stdio: 'inherit' });
child_process.execFileSync(sshAddCmd, ['-l'], { stdio: 'inherit' });

console.log('Configuring deployment key(s)');

child_process.execFileSync(sshAdd, ['-L']).toString().split(/\r?\n/).forEach(function(key) {
child_process.execFileSync(sshAddCmd, ['-L']).toString().split(/\r?\n/).forEach(function(key) {
const parts = key.match(/\bgithub\.com[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)/i);

if (!parts) {
Expand All @@ -386,9 +386,9 @@ try {

fs.writeFileSync(`${homeSsh}/key-${sha256}`, key + "\n", { mode: '600' });

child_process.execSync(`git config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`);
child_process.execSync(`git config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "[email protected]:${ownerAndRepo}"`);
child_process.execSync(`git config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://[email protected]/${ownerAndRepo}"`);
child_process.execSync(`${gitCmd} config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`);
child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "[email protected]:${ownerAndRepo}"`);
child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://[email protected]/${ownerAndRepo}"`);

const sshConfig = `\nHost key-${sha256}.github.com\n`
+ ` HostName github.com\n`
Expand Down Expand Up @@ -2903,23 +2903,21 @@ exports.default = _default;
const os = __webpack_require__(87);

module.exports = (process.env['OS'] != 'Windows_NT') ? {

// Use getent() system call, since this is what ssh does; makes a difference in Docker-based
// Action runs, where $HOME is different from the pwent
home: os.userInfo().homedir,
sshAgent: 'ssh-agent',
sshAdd: 'ssh-add'

homePath: os.userInfo().homedir,
sshAgentCmd: 'ssh-agent',
sshAddCmd: 'ssh-add',
gitCmd: 'git'
} : {

home: os.homedir(),
sshAgent: 'c://progra~1//git//usr//bin//ssh-agent.exe',
sshAdd: 'c://progra~1//git//usr//bin//ssh-add.exe'

// Assuming GitHub hosted `windows-*` runners for now
homePath: os.homedir(),
sshAgentCmd: 'c://progra~1//git//usr//bin//ssh-agent.exe',
sshAddCmd: 'c://progra~1//git//usr//bin//ssh-add.exe',
gitCmd: 'c://progra~1//git//usr//bin//git.exe'
};



/***/ })

/******/ });
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const core = require('@actions/core');
const child_process = require('child_process');
const fs = require('fs');
const crypto = require('crypto');
const { home, sshAgent, sshAdd } = require('./paths.js');
const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = require('./paths.js');

try {
const privateKey = core.getInput('ssh-private-key');
Expand All @@ -14,7 +14,7 @@ try {
return;
}

const homeSsh = home + '/.ssh';
const homeSsh = homePath + '/.ssh';

console.log(`Adding GitHub.com keys to ${homeSsh}/known_hosts`);

Expand All @@ -29,7 +29,7 @@ try {
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];

// Extract auth socket path and agent pid and set them as job variables
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
child_process.execFileSync(sshAgentCmd, sshAgentArgs).toString().split("\n").forEach(function(line) {
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);

if (matches && matches.length > 0) {
Expand All @@ -42,16 +42,16 @@ try {
console.log("Adding private key(s) to agent");

privateKey.split(/(?=-----BEGIN)/).forEach(function(key) {
child_process.execFileSync(sshAdd, ['-'], { input: key.trim() + "\n" });
child_process.execFileSync(sshAddCmd, ['-'], { input: key.trim() + "\n" });
});

console.log("Key(s) added:");

child_process.execFileSync(sshAdd, ['-l'], { stdio: 'inherit' });
child_process.execFileSync(sshAddCmd, ['-l'], { stdio: 'inherit' });

console.log('Configuring deployment key(s)');

child_process.execFileSync(sshAdd, ['-L']).toString().split(/\r?\n/).forEach(function(key) {
child_process.execFileSync(sshAddCmd, ['-L']).toString().split(/\r?\n/).forEach(function(key) {
const parts = key.match(/\bgithub\.com[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)/i);

if (!parts) {
Expand All @@ -66,9 +66,9 @@ try {

fs.writeFileSync(`${homeSsh}/key-${sha256}`, key + "\n", { mode: '600' });

child_process.execSync(`git config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`);
child_process.execSync(`git config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "[email protected]:${ownerAndRepo}"`);
child_process.execSync(`git config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://[email protected]/${ownerAndRepo}"`);
child_process.execSync(`${gitCmd} config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`);
child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "[email protected]:${ownerAndRepo}"`);
child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://[email protected]/${ownerAndRepo}"`);

const sshConfig = `\nHost key-${sha256}.github.com\n`
+ ` HostName github.com\n`
Expand Down
20 changes: 9 additions & 11 deletions paths.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
const os = require('os');

module.exports = (process.env['OS'] != 'Windows_NT') ? {

// Use getent() system call, since this is what ssh does; makes a difference in Docker-based
// Action runs, where $HOME is different from the pwent
home: os.userInfo().homedir,
sshAgent: 'ssh-agent',
sshAdd: 'ssh-add'

homePath: os.userInfo().homedir,
sshAgentCmd: 'ssh-agent',
sshAddCmd: 'ssh-add',
gitCmd: 'git'
} : {

home: os.homedir(),
sshAgent: 'c://progra~1//git//usr//bin//ssh-agent.exe',
sshAdd: 'c://progra~1//git//usr//bin//ssh-add.exe'

// Assuming GitHub hosted `windows-*` runners for now
homePath: os.homedir(),
sshAgentCmd: 'c://progra~1//git//usr//bin//ssh-agent.exe',
sshAddCmd: 'c://progra~1//git//usr//bin//ssh-add.exe',
gitCmd: 'c://progra~1//git//usr//bin//git.exe'
};

0 comments on commit df2f741

Please sign in to comment.