09/06/2017 - CAPISTRANO, JENKINS
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 a new user called deployer
in jenkins. You should dedicate it to deployment process.
http://jenkins.dev
and login as "admin".deployer
user. E.g. deployer:deployer
http://jenkins.dev
and login as "deployer".http://jenkins.dev/user/deployer/configure
page.deployer:2ead0d921b125a65ca8cc9d8cb5120b1
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
.
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
$ 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