Skip to content

Instantly share code, notes, and snippets.

@alvaro-canepa
Last active July 31, 2022 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvaro-canepa/b1df9ab3a1c7566eb860947499b744c6 to your computer and use it in GitHub Desktop.
Save alvaro-canepa/b1df9ab3a1c7566eb860947499b744c6 to your computer and use it in GitHub Desktop.
Deploy a versioned vue project throw ssh (using linux)
const chalk = require("chalk");
const { exec } = require("child_process");
const { version } = require("../package.json");
const sSourcePath = `./dist/${version}`;
const sDestPath = `./dist`;
const sFile = `${version}.zip`;
const sServer = "root@gestionweb.uy";
const sServerDest =
"/var/www/devel.gestionweb.uy/plugins/planetadeleste/gw/assets/app/";
const title = (sMessage) => {
console.log(chalk.blue(sMessage));
console.log(chalk.blue("========================"));
};
const log = (sMessage, stdout, error, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(chalk.green(sMessage));
console.log(chalk.grey(`Stdout: ${stdout}`));
};
const compress = () => {
title(`Compressing ${sSourcePath}`);
exec(
`cd ${sDestPath} && zip -r ${sFile} ${version}`,
(error, stdout, stderr) => {
log("Compression complete", stdout, error, stderr);
upload();
}
);
};
const upload = () => {
title(`Uploading ${sFile} to ${sServer}`);
exec(
`cd ${sDestPath} && scp -r ${sFile} ${sServer}:${sServerDest}`,
(error, stdout, stderr) => {
log("Upload complete", stdout, error, stderr);
extract();
}
);
};
const extract = () => {
const arCommands = [
`cd ${sServerDest}`,
`unzip -o ${sFile}`,
`chown -R www-data:www-data ./${version}`,
];
const sCommand = arCommands.join(" && ");
title(`Uncompressing ${sFile} on ${sServer}`);
exec(`ssh ${sServer} "${sCommand}"`, (error, stdout, stderr) => {
log("Uncompress complete", stdout, error, stderr);
removeCompress();
});
};
const removeCompress = () => {
title(`Deleting file ${sFile}`);
exec(`rm -f ${sDestPath}/${sFile}`, (error, stdout, stderr) => {
log("File deleted", stdout, error, stderr);
});
};
const deploy = () => {
compress();
};
deploy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment