Skip to content

Commit df2f741

Browse files
authored
Provide gitPath for Windows to avoid failures on windows-2022 (GitHub-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).
1 parent fbef2c7 commit df2f741

File tree

5 files changed

+49
-55
lines changed

5 files changed

+49
-55
lines changed

cleanup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const core = require('@actions/core');
22
const { execFileSync } = require('child_process');
3-
const { sshAgent } = require('./paths.js');
3+
const { sshAgentCmd } = require('./paths.js');
44

55
try {
66
// Kill the started SSH agent
77
console.log('Stopping SSH agent');
8-
execFileSync(sshAgent, ['-k'], { stdio: 'inherit' });
8+
execFileSync(sshAgentCmd, ['-k'], { stdio: 'inherit' });
99
} catch (error) {
1010
console.log(error.message);
1111
console.log('Error stopping the SSH agent, proceeding anyway');

dist/cleanup.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,12 @@ exports.debug = debug; // for test
599599

600600
const core = __webpack_require__(470);
601601
const { execFileSync } = __webpack_require__(129);
602-
const { sshAgent } = __webpack_require__(972);
602+
const { sshAgentCmd } = __webpack_require__(972);
603603

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

28262826
module.exports = (process.env['OS'] != 'Windows_NT') ? {
2827-
28282827
// Use getent() system call, since this is what ssh does; makes a difference in Docker-based
28292828
// Action runs, where $HOME is different from the pwent
2830-
home: os.userInfo().homedir,
2831-
sshAgent: 'ssh-agent',
2832-
sshAdd: 'ssh-add'
2833-
2829+
homePath: os.userInfo().homedir,
2830+
sshAgentCmd: 'ssh-agent',
2831+
sshAddCmd: 'ssh-add',
2832+
gitCmd: 'git'
28342833
} : {
2835-
2836-
home: os.homedir(),
2837-
sshAgent: 'c://progra~1//git//usr//bin//ssh-agent.exe',
2838-
sshAdd: 'c://progra~1//git//usr//bin//ssh-add.exe'
2839-
2834+
// Assuming GitHub hosted `windows-*` runners for now
2835+
homePath: os.homedir(),
2836+
sshAgentCmd: 'c://progra~1//git//usr//bin//ssh-agent.exe',
2837+
sshAddCmd: 'c://progra~1//git//usr//bin//ssh-add.exe',
2838+
gitCmd: 'c://progra~1//git//usr//bin//git.exe'
28402839
};
28412840

28422841

2843-
28442842
/***/ })
28452843

28462844
/******/ });

dist/index.js

+18-20
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ const core = __webpack_require__(470);
322322
const child_process = __webpack_require__(129);
323323
const fs = __webpack_require__(747);
324324
const crypto = __webpack_require__(417);
325-
const { home, sshAgent, sshAdd } = __webpack_require__(972);
325+
const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = __webpack_require__(972);
326326

327327
try {
328328
const privateKey = core.getInput('ssh-private-key');
@@ -334,7 +334,7 @@ try {
334334
return;
335335
}
336336

337-
const homeSsh = home + '/.ssh';
337+
const homeSsh = homePath + '/.ssh';
338338

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

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

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

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

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

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

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

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

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

377377
if (!parts) {
@@ -386,9 +386,9 @@ try {
386386

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

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

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

29052905
module.exports = (process.env['OS'] != 'Windows_NT') ? {
2906-
29072906
// Use getent() system call, since this is what ssh does; makes a difference in Docker-based
29082907
// Action runs, where $HOME is different from the pwent
2909-
home: os.userInfo().homedir,
2910-
sshAgent: 'ssh-agent',
2911-
sshAdd: 'ssh-add'
2912-
2908+
homePath: os.userInfo().homedir,
2909+
sshAgentCmd: 'ssh-agent',
2910+
sshAddCmd: 'ssh-add',
2911+
gitCmd: 'git'
29132912
} : {
2914-
2915-
home: os.homedir(),
2916-
sshAgent: 'c://progra~1//git//usr//bin//ssh-agent.exe',
2917-
sshAdd: 'c://progra~1//git//usr//bin//ssh-add.exe'
2918-
2913+
// Assuming GitHub hosted `windows-*` runners for now
2914+
homePath: os.homedir(),
2915+
sshAgentCmd: 'c://progra~1//git//usr//bin//ssh-agent.exe',
2916+
sshAddCmd: 'c://progra~1//git//usr//bin//ssh-add.exe',
2917+
gitCmd: 'c://progra~1//git//usr//bin//git.exe'
29192918
};
29202919

29212920

2922-
29232921
/***/ })
29242922

29252923
/******/ });

index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const core = require('@actions/core');
22
const child_process = require('child_process');
33
const fs = require('fs');
44
const crypto = require('crypto');
5-
const { home, sshAgent, sshAdd } = require('./paths.js');
5+
const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = require('./paths.js');
66

77
try {
88
const privateKey = core.getInput('ssh-private-key');
@@ -14,7 +14,7 @@ try {
1414
return;
1515
}
1616

17-
const homeSsh = home + '/.ssh';
17+
const homeSsh = homePath + '/.ssh';
1818

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

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

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

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

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

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

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

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

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

5757
if (!parts) {
@@ -66,9 +66,9 @@ try {
6666

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

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

7373
const sshConfig = `\nHost key-${sha256}.github.com\n`
7474
+ ` HostName github.com\n`

paths.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
const os = require('os');
22

33
module.exports = (process.env['OS'] != 'Windows_NT') ? {
4-
54
// Use getent() system call, since this is what ssh does; makes a difference in Docker-based
65
// Action runs, where $HOME is different from the pwent
7-
home: os.userInfo().homedir,
8-
sshAgent: 'ssh-agent',
9-
sshAdd: 'ssh-add'
10-
6+
homePath: os.userInfo().homedir,
7+
sshAgentCmd: 'ssh-agent',
8+
sshAddCmd: 'ssh-add',
9+
gitCmd: 'git'
1110
} : {
12-
13-
home: os.homedir(),
14-
sshAgent: 'c://progra~1//git//usr//bin//ssh-agent.exe',
15-
sshAdd: 'c://progra~1//git//usr//bin//ssh-add.exe'
16-
11+
// Assuming GitHub hosted `windows-*` runners for now
12+
homePath: os.homedir(),
13+
sshAgentCmd: 'c://progra~1//git//usr//bin//ssh-agent.exe',
14+
sshAddCmd: 'c://progra~1//git//usr//bin//ssh-add.exe',
15+
gitCmd: 'c://progra~1//git//usr//bin//git.exe'
1716
};
18-

0 commit comments

Comments
 (0)