{"id":5360,"date":"2013-09-10T19:46:53","date_gmt":"2013-09-10T16:46:53","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=5360"},"modified":"2021-08-16T11:34:28","modified_gmt":"2021-08-16T08:34:28","slug":"capistrano-recipe-for-checking-travis-ci-build-status","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/capistrano-recipe-for-checking-travis-ci-build-status\/","title":{"rendered":"Capistrano recipe for checking Travis CI build status"},"content":{"rendered":"\n<p>Using Continuous Integration is a must for any mature development process, but in the hurry of delivering to production it&#8217;s easy to forget about checking build status before issuing &#8220;cap production deploy&#8221;. What if while running Capistrano deployment, script will automatically check CI status and prevent us from delivering a broken branch?<\/p>\n\n\n\n<p>In this article, we&#8217;ll discuss how this can be easily done via Capistrano recipe. It supports only Travis CI, but can be customised to use with different CI systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<p>Travis CI provides a gem for their API. This snippet allows us to fetch branch build status:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">require 'travis\/pro'\n\n# this should be pro-token!\nci_access_token = \"your-pro-access-token\"\nci_repository = \"organisation-or-user\/private-repository-name\"\nbranch = \"master\"\n\nTravis::Pro.access_token = ci_access_token\nrepo = Travis::Pro::Repository.find(ci_repository)\nbranch_state = repo.branch(branch).state\n<\/pre>\n\n\n\n<p>To access private repository, we need access_token and Pro-account on Travis CI. I hardly imagine somebody deploying from public repository, but anyway, here is how it can be done:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">require 'travis'\n\nci_repository = \"organisation-or-user\/public-repository-name\"\nbranch = \"master\"\nrepo = Travis::Repository.find(ci_repository)\nbranch_state = repo.branch(branch).state\n<\/pre>\n\n\n\n<p>Now we should wrap this snippet into Capistrano recipe and prepare all stuff, like obtaining access to Travis and getting access_token.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step by step tutorial<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">0. Prerequisites<\/h3>\n\n\n\n<p>You or somebody from your GitHub organization bought Pro-account for Travis CI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create account in Travis CI<\/h3>\n\n\n\n<p>Just login via <a href=\"https:\/\/magnum.travis-ci.com\/auth\">https:\/\/magnum.travis-ci.com\/auth<\/a> with your Github account and grant Travis CI access to your repositories (in case you haven&#8217;t done it before).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Obtaining access_token<\/h3>\n\n\n\n<p>To get access to Pro account API, you need to obtain Travis access_token:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted theme:tomorrow-night lang:sh nums:false\">$ gem install travis &amp;&amp; travis login --pro &amp;&amp; travis token --pro\n<\/pre>\n\n\n\n<p>This command asks your GitHub credentials and outputs access_token:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"225\" src=\"\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/09\/token-generation.png\" alt=\"travis token generation\" class=\"wp-image-5420\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. Add &#8216;travis&#8217; gem dependency<\/h3>\n\n\n\n<p>Put it into deployment Gemfile:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gem 'travis', require: false<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Capistrano settings<\/h3>\n\n\n\n<p>Put these variables into configuration part of your Capistrano recipes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">set :ci_access_token, \"your-pro-access-token\"\nset :ci_repository, \"organisation-or-user\/repository-name\"\nset :branch do\n  ENV[\"BRANCH\"] || 'master'\nend\n<\/pre>\n\n\n\n<p>Notice, we need &#8220;branch&#8221;-variable and this part can differ for your capistrano configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Capistrano recipe<\/h3>\n\n\n\n<p>Copy this recipe into your recipes directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ci.rb\nrequire 'travis\/pro'\n\nnamespace :ci do\n  desc \"verification of branch build status on Travis CI\"\n  task :verify do\n    begin\n      Travis::Pro.access_token = ci_access_token\n      repo = Travis::Pro::Repository.find(ci_repository)\n      branch_state = repo.branch(branch).state\n\n      unless branch_state == \"passed\"\n        Capistrano::CLI.ui.say \"Your '#{branch}' branch has '#{branch_state}' state on CI.\"\n        Capistrano::CLI.ui.ask(\"Continue anyway? (y\/N)\") == 'y' or abort\n      end\n    rescue =&gt; e\n      Capistrano::CLI.ui.say e.message\n    end\n  end\nend\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Enable ci:verify task<\/h3>\n\n\n\n<p>Drop this line in front of your deployment steps:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">before 'deploy', 'ci:verify'<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Run deploy<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted theme:tomorrow-night lang:sh nums:false\">$ cap production deploy\n<\/pre>\n\n\n\n<p>And what we have on the failed build status:<br><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5421\" src=\"\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/09\/failed-branch.png\" alt=\"travis ci verify in action\" width=\"579\" height=\"226\"><\/p>\n\n\n\n<p>That&#8217;s it. Now on every deploy ci:verify will check build status and notify you in case of broken build.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Continuous Integration is a must for any mature development process, but in the hurry of delivering to production it&#8217;s easy to forget about checking build status before issuing &#8220;cap production deploy&#8221;. What if while running Capistrano deployment, script will automatically check CI status and prevent us from delivering a broken branch? In this article,&#8230;<\/p>\n","protected":false},"author":25,"featured_media":5520,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Sergii Boiko"],"class_list":["post-5360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/09\/token-generation.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=5360"}],"version-history":[{"count":85,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5360\/revisions"}],"predecessor-version":[{"id":14085,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5360\/revisions\/14085"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/5520"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=5360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=5360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=5360"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=5360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}