require 'fileutils' namespace :cf do desc 'Deploy to staging on Cloud Foundry' task :deploy_staging do cf_target = 'api.run.pivotal.io' deploy_branch = 'master' deploy_space = 'staging' deploy_org = "YOUR ORGANIZATION" deploy_repo_dir = "tmp/cf_deploy" check_for_cli check_for_branch(deploy_branch) sh "git fetch" check_for_dirty_git unless File.exists? deploy_repo_dir # Copy repo to tmp dir so we can continue working while it deploys @new_repo = true puts "Copying repo to #{deploy_repo_dir}..." FileUtils.cp_r Rails.root.to_s, "/tmp/cf_deploy" FileUtils.mv "/tmp/cf_deploy", "#{Rails.root.to_s}/#{deploy_repo_dir}" end # Change working directory to copied repo Dir.chdir("#{Rails.root.to_s}/#{deploy_repo_dir}") if @new_repo # Set remote to parent git dir, so we can fetch updates on future deploys sh "git remote set-url origin ../.." sh "git checkout #{deploy_branch}" else # Otherwise, we're already on the deploy branch, so fetch any new changes from original repo sh "git fetch origin" sh "git reset --hard origin/master" end # Delete symbolic links before deploy because cf doesn't like them sh 'find . -type l -exec rm -rf {} \;' sh "cf target #{cf_target} -o #{deploy_org} -s #{deploy_space}" sh "cf push -m config/cf.yml" end desc 'Deploy to production on Cloud Foundry' task :deploy_production do cf_target = 'api.run.pivotal.io' deploy_branch = 'master' deploy_space = 'production' deploy_org = "YOUR ORGANIZATION" deploy_repo_dir = "tmp/cf_deploy" check_for_cli check_for_branch(deploy_branch) sh "git fetch" check_for_dirty_git unless File.exists? deploy_repo_dir # Copy repo to tmp dir so we can continue working while it deploys @new_repo = true puts "Copying repo to #{deploy_repo_dir}..." FileUtils.cp_r Rails.root.to_s, "/tmp/cf_deploy" FileUtils.mv "/tmp/cf_deploy", "#{Rails.root.to_s}/#{deploy_repo_dir}" end # Change working directory to copied repo Dir.chdir("#{Rails.root.to_s}/#{deploy_repo_dir}") if @new_repo # Set remote to parent git dir, so we can fetch updates on future deploys sh "git remote set-url origin ../.." sh "git checkout #{deploy_branch}" else # Otherwise, we're already on the deploy branch, so fetch any new changes from original repo sh "git fetch origin" sh "git reset --hard origin/master" end # Delete symbolic links before deploy because cf doesn't like them sh 'find . -type l -exec rm -rf {} \;' sh "cf target #{cf_target} -o #{deploy_org} -s #{deploy_space}" sh "cf push -m config/cf.yml" end def check_for_cli sh 'cf -v' do |ok, res| raise "The CloudFoundry CLI is required. Run: gem install cf" unless ok end end def check_for_branch(branch) branch = `branch_name=$(git symbolic-ref HEAD 2>/dev/null); branch_name=${branch_name##refs/heads/}; echo ${branch_name:-HEAD}`.strip unless branch == branch raise 'Your working branch must be master to deploy. Run: git checkout master' end end def check_for_dirty_git if `git status --porcelain`.present? raise "You have unstaged or uncommitted changes! Please only deploy from a clean working directory!" end end end