The script to deploy

background

Jenkins had a problem, and it took too long to pack. During this time, the code was not updated to the test environment in time, resulting in various problems detected by the test. And through Xshell to change the code, more cumbersome, always easy to forget. (especially at the end of the day)

Since our front-end environment includes Node, why not upload the packaged code directly through Node?

implementation

  1. The introduction ofnode-ssh
  2. Establish a connection
  3. Copy folder
  4. Close the connection

Basic steps, basically not too great difficulty. The Node-SSH README is basically there.

/** * @file: deploy.js * @author: duanjl * @date: 2019/8/6 * @description: The code after submitting the build via Node-SSH ** /
// eslint-disable-next-line
const NodeSSH = require('node-ssh');
// const open = require('open'); // Can be used to open a browser
const ssh = new NodeSSH();
const localDir = './build';
const remoteDir = '/opt/front-end';
const removeCommand = 'rm -rf ./build';
const host = 'localhost';
const password: '* * * *'.// Create a new connection
ssh.connect({
  host,
  username: 'front'.port: 22,
  password,
	privateKey: '/home/.ssh/***',
})
.then((a)= > {
	 // Delete the original directory
  ssh.execCommand(removeCommand, { cwd: remoteDir }).then((result) = > {
		// Some branch processes can be implemented by returning values and making some simple judgments
    console.log(`STDOUT: ${result.stdout}`);
    console.log(`STDERR: ${result.stderr}`);
		// Submit the specified directory
    ssh.putDirectory(localDir, `${remoteDir}/build`).then(
      (a)= > {
        console.log('The File thing is done');
        ssh.dispose();
				// open(`http://${host}`, { app: ['chrome'] });
      },
      (error) => {
        console.log("Something's wrong");
        console.log(error); ssh.dispose(); }); }); });Copy the code

Here, WHEN I delete the original directory, I specify to delete the Build directory in the parent directory of the build directory. In case I accidentally assign the remote directory to a root directory….

With the Open package, we can automatically open the browser after the transfer is complete.

more

What if we thought a little bit more and committed to multiple servers.

/** * @file: deploy.js * @author: duanjl * @date: 2019/8/6 * @description: The code after submitting the build via Node-SSH ** /
// eslint-disable-next-line
const NodeSSH = require('node-ssh');
// const open = require('open'); // Can be used to open a browser
const ssh = new NodeSSH();
const localDir = './build';
const remoteDir = '/opt/front-end';
const removeCommand = 'rm -rf ./build';

const hostWithPasswordArr = [
  {
    host: 'localhost'.password: '* * * *',},];// Iterate over multiple arrays
hostWithPasswordArr.forEach((hostWithPassword) = > {
  const { host, password } = hostWithPassword;
  ssh.connect({
    host,
    username: 'root'.port: 22,
    password,
  }).then((a)= > {
    ssh.execCommand(removeCommand, { cwd: remoteDir }).then((result) = > {
      console.log(`STDOUT: ${result.stdout}`);
      console.log(`STDERR: ${result.stderr}`);
      ssh.putDirectory(localDir, `${remoteDir}/build`).then(
        (a)= > {
          console.log('The File thing is done');
          ssh.dispose();
        },
        (error) => {
          console.log("Something's wrong");
          console.log(error); ssh.dispose(); }); }); }); });Copy the code

With array traversal, we can commit to multiple servers. Just be sure to close the connection.

Using a script

The final step is to use scripts. In package.json, add a line to scripts.

"deploy": "npm run build && node ./deploy.js".Copy the code

After the package is packaged, deploy it.

Why not just copy the build command? Because there is often a need to package a variety of environments, whether it is to change a URL, to remove a prompt, or to hide some menus, to meet the needs of different environments. This code change should not occur on the test server.

Can git be automatically packaged and deployed once committed? With Husky, it should be possible to deploy the code remotely after committing it.

prompt

Because files contain sensitive data such as server passwords, it is best not to upload to gi T