Jenkins is a powerful tool to help automate software development.

In a previous company I worked for, one of the projects I worked on was setting up jeknins on out staging and production servers to speed up the build time and bring us closer to being able to do multiple releases a day instead of one every few weeks.

Normally we would ssh into the server and build it by hand which would sometimes take up to 30 minutes for some larger projects with a lot of dependencies.

But after we got Jenkins set up, it was as easy as clicking a button.

We were using the MEAN stack with pm2, so this tutorial will be based around that.

From the jenkins website:
wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

Then after it gets installed, it should be automatically started on port 8080.

After going to the dashboard, you should be contronted with a screen that asks you for an administrator password which is at the given location.

To get it, just type
$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword
3de96e6ce06a4dff0460653036ea6a16                
                

Then enter in the displayed password into the dashboard to get access.

It will show a screen asking about plugins, I usually just install the recommended plugins to make things easier.

Fill out the form with your info

Then when it asks about the url, I don't think it matters at all for the scope of this project so you can either save or skip.

Then create a new job. I use the Freestyle project configuration

Then go to the project screen, click configure.

Optionally you can add in parameters which you can fill out when you build it. I add branch and tag parameters to give more flexibility.

In the Source Code Management tab, add a git SCM and enter in the git repo. If it is private you will have to add a credentials to it, which in my experience SSH keys are the easiest. And also in Aditional Behaviors, add Wipe out repository & force clone. This makes sure that no old files get left behind and potentially break things.

Then we can ignore build triggers for now. You can do a lot of cool things with it, like using a webhook to trigger a build, like when someone pushes a commit. But with no triggers set, they have a button on the project page which allows you to trigger it manually, which is the desired action here.

And what I also do for ease and security is create a separate user to hold the pm2 process. Normally all of the pm2 process info goes into $HOME/.pm2 and when it gets run with the jenkins user it gets a little weird.

So to create the pm2 user I run these commands:
sudo adduser pm2-user
sudo adduser jenkins pm2-user
Restart jenkins so the new group takes effect
sudo service jenkins restart
And to login as the user:
sudo -u pm2-user -s
And now to update the permissions so the jenkins user can access everything.
export HOME=/home/pm2-user
cd ~
chmod g+rwx .
chmod -R g+rwx .pm2

Now back in Jenkins, install the NodeJS plugin which just allows jenkins to use npm and node. Add in a pm2 dependency and that should be good to go.

And then edit the config to add in NodeJS and the build steps like so.

git checkout $Branch
npm install
export HOME=/home/pm2-user
npm run restart:pm2

And now it should be good to go. Test the build by going to the job page, click Build With Parameters, then Build

If everything is done right, the build should succeed and the pm2 process should be running.