ssh-deploy/src/remoteCmd.js

27 lines
857 B
JavaScript
Raw Normal View History

const { exec } = require('child_process');
2023-01-02 11:41:31 +01:00
const { privateKey, sshServer, githubWorkspace } = require('./inputs');
const { writeToFile } = require('./helpers');
2023-01-02 01:12:37 +01:00
const remoteCmd = (content, label) => {
const filename = `local_ssh_script-${label}.sh`;
try {
2023-01-02 01:12:37 +01:00
writeToFile({ dir: githubWorkspace, filename, content });
2023-01-02 11:41:31 +01:00
exec(`ssh -i ${privateKey} ${sshServer} 'bash -s' < ${filename}`, (err, data, stderr) => {
if (err) {
console.log('⚠️ [CMD] Remote script failed. ', err.message);
} else {
console.log('✅ [CMD] Remote script executed. \n', data, stderr);
}
});
} catch (err) {
console.log('⚠️ [CMD] Starting Remote script execution failed. ', err.message);
}
};
module.exports = {
remoteCmdBefore: (cmd) => remoteCmd(cmd, 'before'),
remoteCmdAfter: (cmd) => remoteCmd(cmd, 'after')
};