Class: Code42Template::Adapters::Heroku

Inherits:
Object
  • Object
show all
Defined in:
lib/code42template/adapters/heroku.rb

Constant Summary collapse

HEROKU_BUILDPACK_URLS =
%w(
  heroku/nodejs
  heroku/ruby
  https://github.com/febeling/webpack-rails-buildpack.git
)

Instance Method Summary collapse

Constructor Details

#initialize(app_builder) ⇒ Heroku

Returns a new instance of Heroku.



10
11
12
# File 'lib/code42template/adapters/heroku.rb', line 10

def initialize(app_builder)
  @app_builder = app_builder
end

Instance Method Details

#configure_heroku_buildpacksObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/code42template/adapters/heroku.rb', line 117

def configure_heroku_buildpacks
  %w(staging production).each do |environment|
    app_name = heroku_app_name_for(environment)

    run_toolbelt_command("buildpacks:clear -a #{app_name}", environment)

    HEROKU_BUILDPACK_URLS.each do |buildpack_url|
      run_toolbelt_command(
        "buildpacks:add #{buildpack_url} -a #{app_name}",
        environment
      )
    end
  end
end

#create_deploy_scriptObject



19
20
21
22
# File 'lib/code42template/adapters/heroku.rb', line 19

def create_deploy_script
  @app_builder.copy_file "bin_deploy", "bin/deploy"
  @app_builder.run "chmod a+x bin/deploy"
end

#create_heroku_apps(flags) ⇒ Object



14
15
16
17
# File 'lib/code42template/adapters/heroku.rb', line 14

def create_heroku_apps(flags)
  create_staging_heroku_app(flags)
  create_production_heroku_app(flags)
end

#create_heroku_pipelineObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/code42template/adapters/heroku.rb', line 103

def create_heroku_pipeline
  run_toolbelt_command(
    "pipelines:create #{heroku_app_name} \
      -a #{heroku_app_name}-staging --stage staging",
    'staging',
  )

  run_toolbelt_command(
    "pipelines:add #{heroku_app_name} \
      -a #{heroku_app_name}-production --stage production",
    'production',
  )
end

#create_production_heroku_app(flags) ⇒ Object



73
74
75
76
77
# File 'lib/code42template/adapters/heroku.rb', line 73

def create_production_heroku_app(flags)
  app_name = heroku_app_name_for('production')

  run_toolbelt_command "create #{app_name} #{flags}", "production"
end

#create_review_apps_setup_scriptObject



94
95
96
97
98
99
100
101
# File 'lib/code42template/adapters/heroku.rb', line 94

def create_review_apps_setup_script
  @app_builder.template(
    'bin_setup_review_app.erb',
    'bin/setup_review_app',
    force: true,
  )
  @app_builder.run 'chmod a+x bin/setup_review_app'
end

#create_staging_heroku_app(flags) ⇒ Object



67
68
69
70
71
# File 'lib/code42template/adapters/heroku.rb', line 67

def create_staging_heroku_app(flags)
  app_name = heroku_app_name_for('staging')

  run_toolbelt_command "create #{app_name} #{flags}", "staging"
end

#set_heroku_application_hostObject



141
142
143
144
145
146
147
148
# File 'lib/code42template/adapters/heroku.rb', line 141

def set_heroku_application_host
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "config:add APPLICATION_HOST=#{heroku_app_name}-#{environment}.herokuapp.com",
      environment,
    )
  end
end

#set_heroku_rails_environmentObject



88
89
90
91
92
# File 'lib/code42template/adapters/heroku.rb', line 88

def set_heroku_rails_environment
  %w(staging production).each do |environment|
    run_toolbelt_command("config:add RAILS_ENV=production", environment)
  end
end

#set_heroku_rails_secretsObject



79
80
81
82
83
84
85
86
# File 'lib/code42template/adapters/heroku.rb', line 79

def set_heroku_rails_secrets
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "config:add SECRET_KEY_BASE=#{generate_secret}",
      environment,
    )
  end
end

#set_heroku_remotesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/code42template/adapters/heroku.rb', line 48

def set_heroku_remotes
  staging_app_name = heroku_app_name_for('staging')
  production_app_name = heroku_app_name_for('production')

  remotes = "    `git remote add staging [email protected]:\#{staging_app_name}.git 2> /dev/null`\n    `git remote add production [email protected]:\#{production_app_name}.git 2> /dev/null`\n    `git config heroku.remote staging`\n\n    puts\n    puts 'If you already have access, try joining this app as a collaborator with the following commands:'\n    puts\n    puts \"heroku join --app \#{staging_app_name}\"\n    puts \"heroku join --app \#{production_app_name}\"\n  SHELL\n\n  @app_builder.append_file 'bin/setup', remotes\nend\n"

#set_heroku_serve_static_filesObject



132
133
134
135
136
137
138
139
# File 'lib/code42template/adapters/heroku.rb', line 132

def set_heroku_serve_static_files
  %w(staging production).each do |environment|
    run_toolbelt_command(
      'config:add RAILS_SERVE_STATIC_FILES=true',
      environment,
    )
  end
end

#update_readme_with_deployObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/code42template/adapters/heroku.rb', line 24

def update_readme_with_deploy
  instructions = "  ## Deploying\n\n  If you have previously run the `./bin/setup` script,\n  you can deploy to staging and production with:\n\n      $ ./bin/deploy staging\n      $ ./bin/deploy production\n\n  We currently use the following buildpacks:\n\n  - heroku/nodejs\n  - heroku/ruby\n  - https://github.com/febeling/webpack-rails-buildpack.git\n\n  Please be sure to configure these buildpacks in the same sequence\n  presented here in both staging and production, or else the deploy will\n  not work.\n  MARKDOWN\n\n  @app_builder.append_file \"README.md\", instructions\nend\n"