Merge pull request #4 from GarryOne/patch-1

Improved error handling. Added to README.md PM key generation details
This commit is contained in:
Dragan Filipović 2020-04-11 16:40:33 +02:00 committed by GitHub
commit 1a29114d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 26 deletions

2
.gitignore vendored
View File

@ -17,3 +17,5 @@ node_modules/
.env .env
.env.test .env.test
# jetbrains
.idea

View File

@ -14,7 +14,11 @@ Pass configuration with `env` vars
1. `SSH_PRIVATE_KEY` [required] 1. `SSH_PRIVATE_KEY` [required]
This should be the private key part of an ssh key pair. The public key part should be added to the authorized_keys file on the server that receives the deployment. This should be the private key part of an ssh key pair.
The public key part should be added to the authorized_keys file on the server that receives the deployment.
The keys should be generated using the PEM format. You can use this command
`ssh-keygen -m PEM -t rsa -b 4096`
2. `REMOTE_HOST` [required] 2. `REMOTE_HOST` [required]

30
dist/index.js vendored
View File

@ -492,6 +492,9 @@ const sshDeploy = (() => {
nodeRsync({ src, dest, args, privateKey, ssh: true, port, sshCmdArgs: ['-o StrictHostKeyChecking=no'], recursive: true }, (error, stdout, stderr, cmd) => { nodeRsync({ src, dest, args, privateKey, ssh: true, port, sshCmdArgs: ['-o StrictHostKeyChecking=no'], recursive: true }, (error, stdout, stderr, cmd) => {
if (error) { if (error) {
console.error('⚠️ Rsync error', error.message); console.error('⚠️ Rsync error', error.message);
console.log('stderr: ', stderr);
console.log('stdout: ', stdout);
console.log('cmd: ', cmd);
process.abort(); process.abort();
} else { } else {
console.log("✅ Rsync finished.", stdout); console.log("✅ Rsync finished.", stdout);
@ -504,14 +507,14 @@ const sshDeploy = (() => {
}; };
const init = ({ const init = ({
src, src,
dest, dest,
args, args,
host = 'localhost', host = 'localhost',
username, username,
privateKeyContent, privateKeyContent,
port port
}) => { }) => {
validateRsync(() => { validateRsync(() => {
const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME ||'deploy_key'); const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME ||'deploy_key');
@ -596,26 +599,27 @@ const sshDeploy = (() => {
})(); })();
const validateInputs = (inputs) => { const validateInputs = (inputs) => {
const validInputs = inputs.filter(input => { const validInputs = Object.keys(inputs).filter((key) => {
const input = inputs[key];
if (!input) { if (!input) {
console.error(`⚠️ ${input} is mandatory`); console.error(`⚠️ ${key} is mandatory`);
} }
return input; return input;
}); });
if (validInputs.length !== inputs.length) { if (validInputs.length !== Object.keys(inputs).length) {
process.abort(); process.abort();
} }
}; };
const run = () => { const run = () => {
validateInputs([SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER]); validateInputs({SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER});
sshDeploy.init({ sshDeploy.init({
src: GITHUB_WORKSPACE + '/' + SOURCE || '', src: GITHUB_WORKSPACE + '/' + SOURCE || '',
dest: TARGET || '/home/' + REMOTE_USER + '/', dest: TARGET || '/home/' + REMOTE_USER + '/',
args: [ARGS] || false, args: ARGS ? [ARGS] : ['-rltgoDzvO'],
host: REMOTE_HOST, host: REMOTE_HOST,
port: REMOTE_PORT || '22', port: REMOTE_PORT || '22',
username: REMOTE_USER, username: REMOTE_USER,

View File

@ -17,6 +17,9 @@ const sshDeploy = (() => {
nodeRsync({ src, dest, args, privateKey, ssh: true, port, sshCmdArgs: ['-o StrictHostKeyChecking=no'], recursive: true }, (error, stdout, stderr, cmd) => { nodeRsync({ src, dest, args, privateKey, ssh: true, port, sshCmdArgs: ['-o StrictHostKeyChecking=no'], recursive: true }, (error, stdout, stderr, cmd) => {
if (error) { if (error) {
console.error('⚠️ Rsync error', error.message); console.error('⚠️ Rsync error', error.message);
console.log('stderr: ', stderr);
console.log('stdout: ', stdout);
console.log('cmd: ', cmd);
process.abort(); process.abort();
} else { } else {
console.log("✅ Rsync finished.", stdout); console.log("✅ Rsync finished.", stdout);
@ -29,14 +32,14 @@ const sshDeploy = (() => {
}; };
const init = ({ const init = ({
src, src,
dest, dest,
args, args,
host = 'localhost', host = 'localhost',
username, username,
privateKeyContent, privateKeyContent,
port port
}) => { }) => {
validateRsync(() => { validateRsync(() => {
const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME ||'deploy_key'); const privateKey = addSshKey(privateKeyContent, DEPLOY_KEY_NAME ||'deploy_key');
@ -121,21 +124,22 @@ const sshDeploy = (() => {
})(); })();
const validateInputs = (inputs) => { const validateInputs = (inputs) => {
const validInputs = inputs.filter(input => { const validInputs = Object.keys(inputs).filter((key) => {
const input = inputs[key];
if (!input) { if (!input) {
console.error(`⚠️ ${input} is mandatory`); console.error(`⚠️ ${key} is mandatory`);
} }
return input; return input;
}); });
if (validInputs.length !== inputs.length) { if (validInputs.length !== Object.keys(inputs).length) {
process.abort(); process.abort();
} }
}; };
const run = () => { const run = () => {
validateInputs([SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER]); validateInputs({SSH_PRIVATE_KEY, REMOTE_HOST, REMOTE_USER});
sshDeploy.init({ sshDeploy.init({
src: GITHUB_WORKSPACE + '/' + SOURCE || '', src: GITHUB_WORKSPACE + '/' + SOURCE || '',