Skip to content

Instantly share code, notes, and snippets.

@dbellotti
Last active December 29, 2015 01:09
Show Gist options
  • Save dbellotti/7590874 to your computer and use it in GitHub Desktop.
Save dbellotti/7590874 to your computer and use it in GitHub Desktop.
# sample rake tasks for deploying to cloud foundry
require 'fileutils'
namespace :cf do
desc 'Deploy to staging on Cloud Foundry'
task :deploy_staging do
deploy('staging')
end
desc 'Deploy to production on Cloud Foundry'
task :deploy_production do
deploy('production')
end
def deploy(environment)
cf_target = 'api.run.pivotal.io'
deploy_space = 'space_name'
deploy_org = "pivotallabs"
deploy_repo_subdir = "tmp/cf_deploy"
tmp_copy_dir = "/tmp/cf_deploy"
check_for_cli
check_for_dirty_git
# Copy repo to tmp dir so we can continue working while it deploys
puts "Copying repo to #{deploy_repo_subdir}..."
FileUtils.rm_rf("#{Rails.root.to_s}/#{deploy_repo_subdir}")
FileUtils.cp_r Rails.root.to_s, tmp_copy_dir
FileUtils.mv tmp_copy_dir, "#{Rails.root.to_s}/#{deploy_repo_subdir}"
# Change working directory to copied repo
Dir.chdir("#{Rails.root.to_s}/#{deploy_repo_subdir}")
# Delete symbolic links before deploy because cf doesn't like them
sh 'find . -type l -exec rm -f {} \;'
sh "cf target #{cf_target} -o #{deploy_org} -s #{deploy_space}"
sh "cf push -m config/cf-#{environment}.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_dirty_git
if `git status --porcelain`.present?
puts "Unstaged or uncommitted changes will be deployed! continue? (y/n)"
raise "Aborted!" unless STDIN.gets.strip == "y"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment