Skip to content

Instantly share code, notes, and snippets.

@dbellotti
Last active December 29, 2015 01:09
Show Gist options
  • Select an option

  • Save dbellotti/7590874 to your computer and use it in GitHub Desktop.

Select an option

Save dbellotti/7590874 to your computer and use it in GitHub Desktop.

Revisions

  1. dbellotti renamed this gist Nov 21, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.rb → cf.rake
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # sample rake tasks for deploying to cloud foundry

    require 'fileutils'

    namespace :cf do
  2. dbellotti created this gist Nov 21, 2013.
    53 changes: 53 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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