Skip to content

Instantly share code, notes, and snippets.

@rsanheim
Created March 28, 2012 05:03
Show Gist options
  • Save rsanheim/2223786 to your computer and use it in GitHub Desktop.
Save rsanheim/2223786 to your computer and use it in GitHub Desktop.
Simple continuous deployment via Jenkins and Capistrano for all branches. Wire this up with Github post receive hooks from Jenkins for best results.
#!/bin/bash -x
# This should be your "script/ci" checked into version control, and then wired as your sole build step in Jenkins.
#
# Simplifying Assumptions:
#
# * You build all branches
# * You want to deploy all branches.
# * You wired up an SSH key to your CI server appropriately so it can talk to your deployment target(s) via Cap
set -e
script/test # or bundle exec rake, or whatever
echo "Beginning deploy from $GIT_BRANCH...."
# Do some git clean up and checkout and pull a branch
# Jenkins by default will be in a detached HEAD state, which Capistrano doesn't like
git remote prune origin
git fetch origin
git branch -t -f local/$GIT_BRANCH $GIT_BRANCH
git checkout local/$GIT_BRANCH
git pull
bundle exec cap deploy
# ...meanwhile, in your Capistrano config somewhere:
# configure your deployments to always go from the current branch
set(:current_branch) { `git branch`.match(/\* (\S+)\s/m)[1] || raise("Couldn't determine current branch") }
set :branch, defer { current_branch }
@mcblum
Copy link

mcblum commented Jun 5, 2017

Hey -- I'm trying to get this set up and for whatever reason when SSH'd in to the server Capistrano is fine. But when run through Jenkins I see:

Running shell script
+ bundle exec cap production deploy
Could not locate Gemfile or .bundle/ directory
script returned exit code 10

Any ideas why that might be happening? I'm sure it's on my end. Thanks for the script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment