Quick Tip: Integrating Git, Jenkins & Capistrano for Continous Deploy
Assumptions:
- You have a Jenkins server with a working testing Job
- You have Capistrano configured, so you can call
cap staging deploy
and it works.
Goal: Have your app deployed to a staging area after passing the tests when you push to the remote.
Step 1: Git Hook (Getting Jenkin's URL)
Jenkins provide a triggering URL for the testing job. Of course you can poll from the repo from very x minutes, but having the testing triggered only when a push is make, makes the tests fails faster and saving you time and some processing and bandwidth in the server.
To get this link:
- Go to your job's configuration (Click on the Job and then
Configure
) - Under
Build Triggers
check theTrigger builds remotely (e.g., from scripts)
option. - Then , fill in the field with a token (can be anything you want, this will be used as authentication). In this case we are choosing 'MyJobToken'.
- Save
So, Jenkins you provide you a URL in the format:
<jenkins_url>/job/<job_name>/build?token=MyJobToken
That means, if access this URL the Job will start. You can test by calling it in the browser and checking your Job.
Step 2: Git Hook (Creating the hook)
So now we need to create the hook.
Access your repository server and go to:
<repo_dir>/hooks
Copy the post-receive.example
example file to a new file named post-receive
:
~/repo/hooks$: cp post-receive.example post-receive
Edit your file with the follwing content:
#!/bin/sh
curl http://<jenkins_url>/job/<job_name>/build?token=MyJobToken
Don't forget to put you server URL and token. Also, make sure curl is installed.
What's Happening?
A git hook is a script called in some git action. The post-receive hook is called whenever a push
is made to the remote.
When that happens we're going to call the Jenkins URL that will trigger the Testing Job, so every new push
Jenkins will test the app.
Step 3: Creating Capistrano Job
Now we need to create a Capistrano Job in Jenkins. Pay attention here, because we need to make this Job's directory the same as the testing job directory
- Create new Job
- Under
Advanced Project Options
click onAdvanced
- Check the
Use custom workspace
- Under directory put your app directory
- Choose a name of you want
- Under
Build
, chooseExecute Shell
from theAdd build step
dropdown - Put yout capistrano command in the Script:
#!/bin/bash -el
cap staging deploy
- Save
Make sure it's working (run the job manually!!)
Important: Under Enviroment
, check and configure rvm
or rbenv
if you use them!
Step 4: Triggering Capistrano Job from Testing Job
- Go back to your Testing Job Configuration.
- In the end, under
Post Build Actions
chooseBuild other projects
. - Put you Capistrano Job's name in the field
- Check the
Trigger only if this build is stable
radio
To test this, manually start the Testing Job and see if the Capistrano Job is triggered after that.
Wrapping up
Now you have everything set up!
Make a new commit, push to your repo. You Testing Job will start some seconds after that. If the test passes, Capistrano Job will start and deploy your app.
So now, you can test and deploy by only pushing to the remote!
Written by Leandro Guida
Related protips
1 Response
Hello,
I'm having trouble with the 3rd step. I'm deploying on production with 'cap production deploy', but that shouldn't make any difference. I can execute the command manually from the jenkins server terminal, but if I try to execute the command automatically in Jenkins execute shell the build fails (if i add --trace, it shows that it happens when it tries to execute rbenv:validate).
deploy@10.105.27.132's password:cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy@10.105.27.132: Inappropriate ioctl for device
Errno::ENOTTY: Inappropriate ioctl for device
What could be the problem?