When we deploy applications, we tend to deploy tags rather than normal branches like develop or master. Tags represent either hotfixes or releases. When hotfix and release branches are finished, they get merged into master and develop branches at same time. If you have your GitHub repository configured to run jobs in jenkins after merging branches, your build history will contain previously run builds. In this example, when we deploy applications with capistrano, the process will run the last build in jenkins history. This is how our example works.


Create jenkins user


Create a new user called deployer in jenkins. You should dedicate it to deployment process.


  1. Go to http://jenkins.dev and login as "admin".

  2. Click "Manage Jenkins" menu option on the left.

  3. Click "Manage Users" menu option at the bottom.

  4. Click "Create User" menu option on the left.

  5. Fill in the fields for deployer user. E.g. deployer:deployer

  6. Save.

Get API user and token


  1. Go to http://jenkins.dev and login as "deployer".

  2. Click your username on right hand side of the page.

  3. Select "Configure" option which will take you to http://jenkins.dev/user/deployer/configure page.

  4. In "API Token" section click "Show API token" button.

  5. Note "User ID" and "API Token" to use in your curl command later on. e.g. deployer:2ead0d921b125a65ca8cc9d8cb5120b1

Obtain crumb


In order to obtain crumb information, you need to first run task below within your deployment script. The reason why we are doing this within the deployment script is because it generates different result compared to running the wget command directly in command line. Since this is a one-off task, delete it after obtaining the crumb information.


before "deploy:starting", "application:jenkins:build"
namespace :application do
namespace :jenkins do
task :build do
execute "wget -q --auth-no-challenge --user deployer --password deployer --output-document - 'http://jenkins.dev/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'"
end
end
end

Code above is your task in deployment script. Run command below to obtain the crumb.


$ bundle exec cap staging deploy
00:00 application:jenkins:build
01 wget -q --auth-no-challenge --user deployer --password deployer --output-document - 'http://....
01 Jenkins-Crumb:e290ffa8f41b1c22274b6875efc16b59
✔ 01 deployer@jenkins.dev 0.565s

As you can see above, our crumb info is Jenkins-Crumb:e290ffa8f41b1c22274b6875efc16b59.


Setup actual capistrano task


This will always stay in our capistrano deployment script so that jenkins jobs get triggered when we deploy applications.


before "deploy:starting", "application:jenkins:build"
namespace :application do
namespace :jenkins do
task :build do
url = "http://deployer:2ead0d921b125a65ca8cc9d8cb5120b1@jenkins.dev/job/Football/build"
crumb = "Jenkins-Crumb:e290ffa8f41b1c22274b6875efc16b59"
suppress = "> /dev/null 2>&1"
execute :curl, "-X POST #{url} -H #{crumb} #{suppress}"
end
end
end

Test


$ bundle exec cap staging deploy
00:00 application:jenkins:build
01 curl -X POST http://deployer:2ead0d921b125a65ca8cc9d8cb5120b1@192.168.99.20:8080/job/.....
✔ 01 deployer@192.168.99.40 0.449s

If you use tail -f /var/log/jenkins/jenkins.log on jenkins server, you would see something like below.


Jun 02, 2017 10:14:17 AM hudson.model.Run execute
INFO: Football #17 main build action completed: SUCCESS