(content, privateKeyPath, isRequired, label)
| 12 | }; |
| 13 | |
| 14 | const remoteCmd = async (content, privateKeyPath, isRequired, label) => new Promise((resolve, reject) => { |
| 15 | const uuid = crypto.randomUUID(); |
| 16 | const filename = `local_ssh_script-${label}-${uuid}.sh`; |
| 17 | try { |
| 18 | writeToFile({ dir: githubWorkspace, filename, content }); |
| 19 | const dataLimit = 10000; |
| 20 | const rsyncStdout = (process.env.RSYNC_STDOUT || '').substring(0, dataLimit); |
| 21 | console.log(`Executing remote script: ssh -i ${privateKeyPath} ${sshServer}`); |
| 22 | exec( |
| 23 | `DEBIAN_FRONTEND=noninteractive ssh -p ${(remotePort || 22)} -i ${privateKeyPath} -o StrictHostKeyChecking=no ${sshServer} 'RSYNC_STDOUT="${rsyncStdout}" bash -s' < ${filename}`, |
| 24 | (err, data = '', stderr = '') => { |
| 25 | if (err) { |
| 26 | const message = `⚠️ [CMD] Remote script failed: ${err.message}`; |
| 27 | console.warn(`${message} \n`, data, stderr); |
| 28 | handleError(message, isRequired, reject); |
| 29 | } else { |
| 30 | const limited = data.substring(0, dataLimit); |
| 31 | console.log('✅ [CMD] Remote script executed. \n', limited, stderr); |
| 32 | deleteFile({ dir: githubWorkspace, filename }); |
| 33 | console.log('✅ [FILE] Script file deleted.'); |
| 34 | resolve(limited); |
| 35 | } |
| 36 | } |
| 37 | ); |
| 38 | } catch (err) { |
| 39 | handleError(err.message, isRequired, reject); |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | module.exports = { |
| 44 | remoteCmdBefore: async (cmd, privateKeyPath, isRequired) => remoteCmd(cmd, privateKeyPath, isRequired, 'before'), |
no test coverage detected