index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const core = require('@actions/core');
  2. const child_process = require('child_process');
  3. const fs = require('fs');
  4. const crypto = require('crypto');
  5. const { home, sshAgent, sshAdd } = require('./paths.js');
  6. try {
  7. const privateKey = core.getInput('ssh-private-key');
  8. if (!privateKey) {
  9. core.setFailed("The ssh-private-key argument is empty. Maybe the secret has not been configured, or you are using a wrong secret name in your workflow file.");
  10. return;
  11. }
  12. const homeSsh = home + '/.ssh';
  13. console.log(`Adding GitHub.com keys to ${homeSsh}/known_hosts`);
  14. fs.mkdirSync(homeSsh, { recursive: true });
  15. fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=\n');
  16. fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n');
  17. fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n');
  18. console.log("Starting ssh-agent");
  19. const authSock = core.getInput('ssh-auth-sock');
  20. const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
  21. // Extract auth socket path and agent pid and set them as job variables
  22. child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
  23. const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
  24. if (matches && matches.length > 0) {
  25. // This will also set process.env accordingly, so changes take effect for this script
  26. core.exportVariable(matches[1], matches[2])
  27. console.log(`${matches[1]}=${matches[2]}`);
  28. }
  29. });
  30. console.log("Adding private key(s) to agent");
  31. privateKey.split(/(?=-----BEGIN)/).forEach(function(key) {
  32. child_process.execFileSync(sshAdd, ['-'], { input: key.trim() + "\n" });
  33. });
  34. console.log("Key(s) added:");
  35. child_process.execFileSync(sshAdd, ['-l'], { stdio: 'inherit' });
  36. console.log('Configuring deployment key(s)');
  37. child_process.execFileSync(sshAdd, ['-L']).toString().split(/\r?\n/).forEach(function(key) {
  38. const parts = key.match(/\bgithub\.com[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)/i);
  39. if (!parts) {
  40. console.log(`Comment for (public) key '${key}' does not match GitHub URL pattern. Not treating it as a GitHub deploy key.`);
  41. return;
  42. }
  43. const sha256 = crypto.createHash('sha256').update(key).digest('hex');
  44. const ownerAndRepo = parts[1].replace(/\.git$/, '');
  45. fs.writeFileSync(`${homeSsh}/key-${sha256}`, key + "\n", { mode: '600' });
  46. child_process.execSync(`git config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`);
  47. child_process.execSync(`git config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`);
  48. child_process.execSync(`git config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`);
  49. const sshConfig = `\nHost key-${sha256}.github.com\n`
  50. + ` HostName github.com\n`
  51. + ` IdentityFile ${homeSsh}/key-${sha256}\n`
  52. + ` IdentitiesOnly yes\n`;
  53. fs.appendFileSync(`${homeSsh}/config`, sshConfig);
  54. console.log(`Added deploy-key mapping: Use identity '${homeSsh}/key-${sha256}' for GitHub repository ${ownerAndRepo}`);
  55. });
  56. } catch (error) {
  57. if (error.code == 'ENOENT') {
  58. console.log(`The '${error.path}' executable could not be found. Please make sure it is on your PATH and/or the necessary packages are installed.`);
  59. console.log(`PATH is set to: ${process.env.PATH}`);
  60. }
  61. core.setFailed(error.message);
  62. }