public
Last active

Jenkins build and deploy script per Github branch

  • Download Gist
ci_runner.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
# Build script for Jenkins. This builds the checked out code and will deploy it if the commit came from
# an environment specified in DEPLOYABLE_ENVIRONMENTS after a successful build.
require 'open-uri'
 
HIPCHAT_AUTH_TOKEN = "FILL ME IN"
HIPCHAT_ROOM = "FILL ME IN"
PROJECT_NAME = "FILL ME IN"
STAGING_URL = "https://staging.example.com"
PRODUCTION_URL = "https://www.example.com"
 
# Update this constant with branches from which we should deploy
DEPLOYABLE_ENVIRONMENTS = [
{
:branch => "develop",
:environment => "FILL ME IN",
:msg => "#{PROJECT_NAME} deployed to staging (#{STAGING_URL})",
:url => STAGING_URL
},
{
:branch => "master",
:environment => "FILL ME IN",
:msg => "#{PROJECT_NAME} deployed to production (#{PRODUCTION_URL})",
:url => PRODUCTION_URL
}
]
 
# Define methods to print out colors.
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end
def yellow(text); colorize(text, 33); end
 
# Check out a branch from git. Optionally you can specify :rebase => true and this will rebase from the :origin branch,
# which defaults to "origin"
def checkout(branch, opts = {})
`git checkout #{branch}`
if opts[:rebase]
origin = opts[:origin] || "origin"
`git rebase #{origin}/#{branch}`
end
end
 
# Returns the SHA1 hash of the most recent git commit.
def most_recent_commit
`git log | head -n 1 | cut -d " " -f 2`
end
 
# Stash the commit that we are about to build so we can determine which branch it came from.
$commit_of_build = most_recent_commit
 
# Returns the current branch we're building on, or nil if we're not on a branch.
def current_branch
branch = `git branch | grep "*" | cut -d " " -f 2`.strip
branch == "(no" ? nil : branch
end
 
# Returns the SHA1 hash of the most recent git commit on branch
def last_commit_on_branch(branch)
checkout(branch, :rebase => true)
most_recent_commit
end
 
# Runs bundle install and bundle exec rake
def build
#########################
# RUN BUILD SCRIPT HERE #
#########################
end
 
def run_smoke_tests(url)
####################################
# RUN SMOKE TEST HERE, RETURN BOOL #
####################################
end
 
def rollback(environment)
###########################
# ROLLBACK CODE GOES HERE #
###########################
end
 
# Posts a message to HipChat
def hipchat(msg, color = "purple", strong = true)
puts(green("Posting to HipChat: \"#{msg}\""))
if strong
msg = "<strong>#{msg}</strong>"
end
msg = URI.encode(msg)
`curl -d "auth_token=#{HIPCHAT_AUTH_TOKEN}&room_id=#{HIPCHAT_ROOM}&from=Jenkins&color=#{color}&message=#{msg}" https://api.hipchat.com/v1/rooms/message`
end
 
# Iterates over DEPLOYABLE_ENVIRONMENTS to see if those caused the commit, and if so, deploys to them.
def deploy
DEPLOYABLE_ENVIRONMENTS.each do |env|
commit_occurred_on_branch = $commit_of_build == last_commit_on_branch(env[:branch])
if commit_occurred_on_branch
puts(green("Deploying to #{env[:environment]}"))
 
########################
# RUN DEPLOY CODE HERE #
########################
 
if $?.success?
hipchat(env[:msg])
success = run_smoke_tests(env[:url])
if !success
msg = "Smoke tests failed for #{PROJECT_NAME} #{env[:url]}! Rolling back."
puts(msg)
hipchat(msg, :red)
rollback(env[:environment])
hipchat("Rollback complete for #{PRODUCTION_URL} #{env[:environment]} (#{env[:url]})")
end
else
hipchat("#{PROJECT_NAME} did not deploy successfully to #{env[:environment]} (#{env[:url]})", :red)
end
end
end
end
 
##############################
##### BEGIN BUILD SCRIPT #####
##############################
build()
deploy()

I wrote this short build and deploy script for our Jenkins instance for www.appboy.com. Our requirements are:

  • Build every branch
  • Continuously deploy to our staging environment from develop
  • Continuously deploy to our production environment from master
  • After deploy, run smoke tests against the live server
    • If there are problems, rollback
  • Post status updates to Hipchat

Fill in the appropriate values and steps. If you're on some cloud hosting service like Heroku, Engine Yard, or DotCloud, deploys and rollbacks are straightforward. Then, set your Jenkins build script to just be something like

#!/bin/bash
source ~/.bashrc
ruby ci_runner.rb

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.