Deploy to a remote server on Jenkins using Capistrano

Minh Reigen
3 min readDec 31, 2017

--

Permissions

First off, you will need to make sure your jenkins server have permissions to SSH pull code from your git repo and your jenkins user has permissions to SHH to deploy to the remote server. No worries, I’ll show you step by step:

SSH into your Jenkins server, and cat the public key!

cat ~/.ssh/id_rsa.pub

Copy the content of the file which starts with ssh-rsa

Now, ssh into the server where the code will be deploy and add this key to the authorized key set.

vim / or nano / ~/.ssh/authorized_keys

Paste the content of the public key as a new line in this file.

Voila! You have authorized the deployment server to receive the deployment commands from your Jenkins server. Let’s set up your Jenkins build project!

Source Code Management

Create a free style project. Authorize your GitHub access with your GitHub credentials.

Choose the branch you want to deploy from. Mine is master

Build Triggers

Build triggers are the actions that signal the build to start. In this case, I will choose GitHub hook trigger for GITScm polling. With this kind of triggers, the project will start to run when Jenkins server polls and detect changes in the repo and branch it watches as described in the last step.

Build Environment

Set up the environmental conditions so that your build can run correctly. For example, I use Ruby 2.3.0 for my test project with a test gemset. I also need to set up the SSH login so that Jenkins can run the deploy code into the deployment server via SSH.

Build Commands

Finally! This is the meat of the deploy build to get the code deployed. In my case, I’m using Capistrano with Ruby on Rails (the setup is a little lengthy and will be another post); but all I need to do to deploy to production is to run cap production deploy 😺

That’s it! Remember to save the configuration and start the build!

Test

If everything is set up correctly, after a Pull Request is merged in your GitHub repo to the master branch, the Deployment Prod build project should run.

...
INFO [8c838f17] Finished in 0.053 seconds with exit status 0 (successful).
INFO [115a0511] Finished in 0.054 seconds with exit status 0 (successful).
[Deployment_Prod] $ /bin/sh -xe /tmp/jenkins5123703400948884375.sh
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 3699 killed;
[ssh-agent] Stopped.
Finished: SUCCESS

If the result of the deploy script is SUCCESS, you should see the changes on your deployment server!

Good luck and have fun!

--

--

Responses (2)