ssh-deploy/src/index.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-10-03 00:52:52 +03:00
#!/usr/bin/env node
const nodeRsync = require('rsyncwrapper');
const { validateRsync, validateInputs } = require('./rsyncCli');
const { addSshKey } = require('./sshKey');
2019-10-03 00:52:52 +03:00
const {
REMOTE_HOST, REMOTE_USER,
REMOTE_PORT, SSH_PRIVATE_KEY, DEPLOY_KEY_NAME,
SOURCE, TARGET, ARGS,
GITHUB_WORKSPACE
} = process.env;
2019-10-03 00:52:52 +03:00
const defaultOptions = {
ssh: true,
sshCmdArgs: ['-o StrictHostKeyChecking=no'],
recursive: true
};
2019-10-03 00:52:52 +03:00
console.log('[general] GITHUB_WORKSPACE: ', GITHUB_WORKSPACE);
2019-10-03 00:52:52 +03:00
const sshDeploy = (() => {
const rsync = ({ privateKey, port, src, dest, args }) => {
console.log(`[Rsync] Starting Rsync Action: ${src} to ${dest}`);
try {
// RSYNC COMMAND
nodeRsync({
src, dest, args, privateKey, port, ...defaultOptions
}, (error, stdout, stderr, cmd) => {
if (error) {
console.error('⚠️ [Rsync] error: ', error.message);
console.log('⚠️ [Rsync] stderr: ', stderr);
console.log('⚠️ [Rsync] stdout: ', stdout);
console.log('⚠️ [Rsync] cmd: ', cmd);
process.abort();
2019-10-03 00:52:52 +03:00
} else {
console.log('✅ [Rsync] finished.', stdout);
2019-10-03 00:52:52 +03:00
}
});
} catch (err) {
console.error('⚠️ [Rsync] command error: ', err.message, err.stack);
process.abort();
2019-10-03 00:52:52 +03:00
}
};
2019-10-03 00:52:52 +03:00
const init = ({ src, dest, args, host = 'localhost', port, username, privateKeyContent }) => {
validateRsync(() => {
const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME || 'deploy_key');
const remoteDest = `${username}@${host}:${dest}`;
2019-10-03 00:52:52 +03:00
rsync({ privateKey, port, src, dest: remoteDest, args });
2019-10-03 00:52:52 +03:00
});
};
2019-10-03 00:52:52 +03:00
return {
init
};
})();
2019-10-03 00:52:52 +03:00
const run = () => {
validateInputs([SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER]);
sshDeploy.init({
src: `${GITHUB_WORKSPACE}/${SOURCE}` || '',
dest: TARGET || `/home/${REMOTE_USER}/`,
args: ARGS ? [ARGS] : ['-rltgoDzvO'],
host: REMOTE_HOST,
port: REMOTE_PORT || '22',
username: REMOTE_USER,
privateKeyContent: SSH_PRIVATE_KEY
});
2019-10-03 00:52:52 +03:00
};
run();