Last active
August 29, 2015 14:24
-
-
Save moki9/3b126637867c5ccac0bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment