- You have a Jenkins server with a working testing Job
- You have Capistrano configured, so you can call
cap staging deployand 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
Build Triggerscheck the
Trigger 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'.
So, Jenkins you provide you a URL in the format:
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:
post-receive.example example file to a new file named
~/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.
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
Advanced Project Optionsclick on
- Check the
Use custom workspace
- Under directory put your app directory
- Choose a name of you want
Execute Shellfrom the
Add build stepdropdown
- Put yout capistrano command in the Script:
#!/bin/bash -el cap staging deploy
Make sure it's working (run the job manually!!)
Enviroment, check and configure
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 Actionschoose
Build other projects.
- Put you Capistrano Job's name in the field
- Check the
Trigger only if this build is stableradio
To test this, manually start the Testing Job and see if the Capistrano Job is triggered after that.
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!