Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require_relative '../config/boot'
  4.  
  5. require 'pathname'
  6. require 'fileutils'
  7. require 'json'
  8. include FileUtils
  9.  
  10. # path to your application root.
  11. APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
  12.  
  13. def system!(*args)
  14. system(*args) || abort("\n== Command #{args} failed ==")
  15. end
  16.  
  17. chdir APP_ROOT do
  18. puts "\n== Checking requirements =="
  19. if ENV.has_key?('HEROKU_API_KEY')
  20. puts 'HEROKU_API_KEY is set'
  21. else
  22. STDERR.puts "Error: HEROKU_API_KEY environment variable is not set, exiting."
  23. abort
  24. end
  25.  
  26. puts '== Installing dependencies =='
  27. system! 'gem install bundler --conservative'
  28. system('bundle check') || system!('bundle install')
  29.  
  30. puts "\n== Retrieving application information =="
  31. require 'platform-api'
  32. client = PlatformAPI::connect(ENV['HEROKU_API_KEY'])
  33.  
  34. config_file = File.read('app.json')
  35. puts "Found application config: app.json"
  36. app_name = JSON.parse(config_file)['name']
  37. puts "Found application: #{app_name}"
  38.  
  39. pipeline = client.pipeline_coupling.info(app_name)['pipeline']
  40. puts "Found pipeline: #{pipeline['name']}"
  41. couplings = client.pipeline_coupling.list().find_all do |coupling|
  42. coupling['pipeline']['id'] == pipeline['id']
  43. end
  44.  
  45. apps = client.app.list()
  46. staging_app_id = couplings.find { |coupling| coupling['stage'] == 'staging' }['app']['id']
  47. staging_app = apps.find { |app| app['id'] == staging_app_id }
  48. puts "Found staging app: #{staging_app['name']}"
  49.  
  50. production_app_id = couplings.find { |coupling| coupling['stage'] == 'production' }['app']['id']
  51. production_app = apps.find { |app| app['id'] == production_app_id }
  52. puts "Found production app: #{production_app['name']}"
  53.  
  54. puts "\n== Deployment =="
  55. puts "Promoting #{staging_app['name']} to production..."
  56. promotion = client.pipeline_promotion.create({
  57. 'pipeline': { 'id': pipeline['id'] },
  58. 'source': {
  59. 'app': { 'id': staging_app['id'] }
  60. },
  61. 'targets': [{
  62. 'app': { 'id': production_app['id'] }
  63. }]
  64. })
  65. puts "Promoted #{staging_app['name']} successfully!"
  66.  
  67. status = promotion['status']
  68. while status == 'pending'
  69. puts "Status is #{status}, waiting..."
  70. sleep(1)
  71. status = client.pipeline_promotion.info(promotion['id'])['status']
  72. end
  73. if status == 'completed'
  74. puts "Deployment completed."
  75. else
  76. STDERR.puts "Deployment failed: #{status}"
  77. abort
  78. end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement