Blue Green deployments - with Jenkins and the blue-green command

Some time back there was a popular post on blue-green deployment - when you deploy your app - it deploys to a “standby” copy of it - and when ready - you flip over to it (this gives zero down time - even if your app needs time to warm up, and lets you warm up app caches, inspect things, test things etc).

This has now been made even easier thanks to Fabians bg-plugin for the bees command line - see here.

Summary on setup: 

Set up 2 apps: app1, and app2 - these will act as your active/standby app pair (either of them could be production at any time, so name them sensibly), then:

bees plugin:install com.cloudbees.sdk.plugins:bg-plugin
bees app:bg:config -n yourRealAppName -a1 app1 -a2 app2 -pal

To deploy:

bees app:bg:deploy -n yourRealAppName

It then will deploy that app to the “standby” app. To flip it - once it is ready:

bees app:bg:switch -n yourRealAppName

If it is all HORRIBLY WRONG and people are screaming at you - run the above again - and it is back where you started!

Finally, you can stop the “standby” app once you are happy the cut over went smooth, to save money:

bees app:bg:stop -n yourRealAppName

(it will start up the standby environment next time you deploy to it).


Automating with Jenkins


You can have a job that always deploys to the standby, warms it up, maybe validates it, before cutting over automatically (and then shutting down the standby to save money). 
With a freestyle job this is quite simple, the SDK can be scripted like this:


export BEES_HOME=/opt/cloudbees/cloudbees-sdk/
if [ ! -d ~/.bees ]; then
    bees init -f -a  -ep us -k $BEES_API -s $BEES_SECRET
bees plugin:install com.cloudbees.sdk.plugins:bg-plugin

bees app:bg:deploy -n  target/web-webapp.war

# WARM NEW SERVERS - for example - could be a smoke test here:
echo "Preparing new servers for router switch over..."
for i in {1..50}
    curl -s "" > /dev/null
    sleep 5

echo "Switching router over to new servers..."
bees app:bg:switch -n  -f

echo "Shutting down old servers..."
bees app:bg:stop -n int -f
Also note you can do what you want in the “warm up” section - you may do some additional smoke tests - and if it fails - fail the build - the production version won’t change! Take this and remove the bits you don’t want or add more to it.

See here for a gist of the script.