Automating Manual Deployment

Who doesn’t love automating tasks, especially the tedious deployment tasks? And yes there are tons of tools to help you make your life easier, after all who needs manual deployment in the age of Continuous Delivery? Well, it all depends on your project and whether you actually need to waste time setting up some fancy tools to help you; use the right tool for the right job.  Sometimes it’s just easier to deploy using the good old SSHy way, which is is what I chose for one of the projects I was working on. Of course I wrote scripts to handle deploying each part of the application, but I came to realize that those scripts could be abstracted into a tool in order to minimize the work in the future (and avoid mistakes).

In this tutorial we will have a look at Husky (find it on Github here) and use it to automate deploying a simple NodeJS application. Follow the installation instructions on the Github project to properly install it.

 

Before we start, Husky relies on SSH so do yourself a favor and generate an access key to the server, unless you love your password so much that you want to keep typing it.

Husky Operations

The pipeline of operations is fairly simple

huskypl

First we build (and package the files we wish to deploy), we transfer them to the remote server, and then we run the project there. In the next section we’ll see how to take care of those tasks using Husky.

Example

In this section we go through a simple scenario of deploying a toy NodeJS project to a remote server and running it.

1. Create your project

Needless to say, you need to create your project beforehand. We’re not going to go through the process of creating a new NodeJS project, there is plenty of resources about that.

2. Initialize Husky files

Make sure that you’re in the directory of the project you want to configure its deployment then run

husky init

You’ll be promoted to enter the following:

  • IP or host name of the remote server
  • The username by which you’ll login into the remote server
  • The build directory from which we’ll grab the deployable files
  • The remote directory to which we’ll transfer the deployable files

For this tutorial let’s assume the following values

remote server: tutorialdeployment.remote
remote username: user
local build directory: deployable
remote directory: /home/user/deployables/

Upon success, there should be three new files in the directory: husky.info, husky.build, and husky.deploy. The info file contains the information entered in the initialization process, while the build and deploy files are bash scripts with only the shell information, we’ll fill them up in the next step.

3. Provide your build commands (packing)

Open husky.build file in whatever editor you want and enter whatever commands you want executed. In our example, we want to pack the project, and move it to ./deployable so that the resulting files will be copied to the remote server. The file contains only a single line:

npm pack && mv web-server-0.0.0.tgz deployable/

npm pack‘ takes care of packaging your application into a single compressed file so that it could be copied from one place to another. Then we move the package file into the directory we specified in the initialization process (of course the name of the file will differ based on your project configuration). Generally speaking, your build file should contain as few commands as possible, this isn’t a build tool, it just calls one.

4. Provide your deployment commands (unpacking)

After the build process, Husky will automatically move ALL files in the build directory to the remote directory, in our case ‘/home/user/deployables/’ on the remote server. Then it’ll execute the commands in the husky.deploy on the remote machine, inside the remote deployment directory. This is important to understand: your commands here will run in the same directory as the deployment directory so there’s no need to cd. Our deployment script for our application will be:

tar -xzf web-server-0.0.0.tgz
cd package
npm rebuild
npm install

npm start &

The deployment file is also very brief, it basically extracts the package directory from the compressed file, performs rebuild and install operations inside the package directory then runs the application.

5. Run it all

After all that is ready, you can now run

husky run

This will run the pipeline, it will execute the script in husky.build, then transfer the files using scp, then it’ll run husky.deploy on the remote machine.

 

 

Leave a comment