mirror of
https://github.com/easingthemes/ssh-deploy.git
synced 2025-06-22 04:22:10 +10:00
Add option to execute script before and after rsync
This commit is contained in:
parent
0e7d8019db
commit
5dfe8ebb5a
14 changed files with 278 additions and 213 deletions
101
src/index.js
101
src/index.js
|
@ -1,78 +1,37 @@
|
|||
#!/usr/bin/env node
|
||||
const nodeRsync = require('rsyncwrapper');
|
||||
|
||||
const { validateRsync, validateInputs } = require('./rsyncCli');
|
||||
const { sshDeploy } = require('./rsyncCli');
|
||||
const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd');
|
||||
const { addSshKey } = require('./sshKey');
|
||||
|
||||
const {
|
||||
REMOTE_HOST, REMOTE_USER,
|
||||
REMOTE_PORT, SSH_PRIVATE_KEY, DEPLOY_KEY_NAME,
|
||||
SOURCE, TARGET, ARGS, EXCLUDE,
|
||||
GITHUB_WORKSPACE
|
||||
} = require('./inputs');
|
||||
|
||||
const defaultOptions = {
|
||||
ssh: true,
|
||||
sshCmdArgs: ['-o StrictHostKeyChecking=no'],
|
||||
recursive: true
|
||||
};
|
||||
|
||||
console.log('GITHUB_WORKSPACE: ', GITHUB_WORKSPACE);
|
||||
console.log('REMOTE_HOST: ', process.env.REMOTE_HOST);
|
||||
console.log('REMOTE_USER: ', process.env.REMOTE_USER);
|
||||
|
||||
const sshDeploy = (() => {
|
||||
const rsync = ({ privateKey, port, src, dest, args, exclude }) => {
|
||||
console.log(`[Rsync] Starting Rsync Action: ${src} to ${dest}`);
|
||||
if (exclude) console.log(`[Rsync] exluding folders ${exclude}`);
|
||||
|
||||
try {
|
||||
// RSYNC COMMAND
|
||||
nodeRsync({
|
||||
src, dest, args, privateKey, port, excludeFirst: exclude, ...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();
|
||||
} else {
|
||||
console.log('✅ [Rsync] finished.', stdout);
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('⚠️ [Rsync] command error: ', err.message, err.stack);
|
||||
process.abort();
|
||||
}
|
||||
};
|
||||
|
||||
const init = ({ src, dest, args, host = 'localhost', port, username, privateKeyContent, exclude = [] }) => {
|
||||
validateRsync(() => {
|
||||
const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME || 'deploy_key');
|
||||
const remoteDest = `${username}@${host}:${dest}`;
|
||||
|
||||
rsync({ privateKey, port, src, dest: remoteDest, args, exclude });
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
init
|
||||
};
|
||||
})();
|
||||
const { validateRequiredInputs } = require('./helpers');
|
||||
const inputs = require('./inputs');
|
||||
|
||||
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,
|
||||
exclude: (EXCLUDE || '').split(',').map((item) => item.trim()) // split by comma and trim whitespace
|
||||
const {
|
||||
source, remoteUser, remoteHost, remotePort,
|
||||
deployKeyName, sshPrivateKey,
|
||||
args, exclude,
|
||||
scriptBefore, scriptAfter,
|
||||
sshServer
|
||||
} = inputs;
|
||||
// Validate required inputs
|
||||
validateRequiredInputs({ sshPrivateKey, remoteHost, remoteUser });
|
||||
// Add SSH key
|
||||
const privateKey = addSshKey(sshPrivateKey, deployKeyName);
|
||||
// Check Script before
|
||||
if (scriptBefore) {
|
||||
remoteCmdBefore(scriptBefore);
|
||||
}
|
||||
// Check script after
|
||||
let callback = () => {};
|
||||
if (scriptAfter) {
|
||||
callback = (...result) => {
|
||||
remoteCmdAfter(scriptAfter, result);
|
||||
};
|
||||
}
|
||||
/* eslint-disable object-property-newline */
|
||||
sshDeploy({
|
||||
source, sshServer, exclude, remotePort,
|
||||
privateKey, args, callback
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue