Skip to content

Instantly share code, notes, and snippets.

@nzoschke
Last active June 23, 2018 22:06
Show Gist options
  • Save nzoschke/4ef6ba7d96828007c4214fc4d56e4102 to your computer and use it in GitHub Desktop.
Save nzoschke/4ef6ba7d96828007c4214fc4d56e4102 to your computer and use it in GitHub Desktop.

First, check out Discourse and patch it for Heroku. To get the first push to build we need to run migrations before asset precompilation. To get the web dyno to start, we need to configure Puma to stay in the foreground.

## Check out Discourse

$ git clone https://github.com/discourse/discourse.git
$ cd discourse

## Patch Discourse for Heroku

$ git apply <<EOF
diff --git a/config/puma.rb b/config/puma.rb
index 85f3825..9ebc77f 100644
--- a/config/puma.rb
+++ b/config/puma.rb
@@ -1,7 +1,6 @@
 if ENV['RAILS_ENV'] == 'production'
 
   # First, you need to change these below to your situation.
-  APP_ROOT = '/home/discourse/discourse'
   num_workers = ENV["NUM_WEBS"].to_i > 0 ? ENV["NUM_WEBS"].to_i : 4
 
   # Second, you can choose how many threads that you are going to run at same time.
@@ -9,11 +8,6 @@ if ENV['RAILS_ENV'] == 'production'
   threads 8, 32
 
   # Unless you know what you are changing, do not change them.
-  bind "unix://#{APP_ROOT}/tmp/sockets/puma.sock"
-  stdout_redirect "#{APP_ROOT}/log/puma.log", "#{APP_ROOT}/log/puma.err.log"
-  pidfile "#{APP_ROOT}/tmp/pids/puma.pid"
-  state_path "#{APP_ROOT}/tmp/pids/puma.state"
-  daemonize true
   preload_app!
 
 end
diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake
index 75f7db1..b16385b 100644
--- a/lib/tasks/assets.rake
+++ b/lib/tasks/assets.rake
@@ -1,4 +1,5 @@
 task 'assets:precompile:before' do
+  Rake::Task["db:migrate"].invoke
 
   require 'uglifier'
   require 'open3'
EOF
$ git add . && git commit -m 'run puma in foreground; run migrations during build'

Next, configure the app's addons and environment variables. Discourse needs a Postgres and Redis database, and uses config vars to configure an external Redis server.

## Configure a Heroku app with Postgres and Redis

$ heroku apps:create discourse-edge
$ heroku addons:add heroku-postgresql
$ heroku addons:add heroku-redis --wait

## Configure Discourse with Redis connection info

$ REDIS_URL=$(heroku config:get REDIS_URL)
$ heroku config:set DISCOURSE_REDIS_HOST=$(echo $REDIS_URL | egrep -o 'ec2[^:]+')
$ heroku config:set DISCOURSE_REDIS_PASSWORD=$(echo $REDIS_URL | egrep -o 'p[a-z0-9]{64}')
$ heroku config:set DISCOURSE_REDIS_PORT=$(echo $REDIS_URL | egrep -o '[0-9]+$')

We also need to configure Rails to serve the static '/public' folder in production. See the Configuring Rails Applications guide for more information.

## Configure Rails to serve static assets

$ heroku config:set DISCOURSE_SERVE_STATIC_ASSETS=true

Finally we can deploy to Heroku:

## Deploy to Heroku

$ git push heroku master
...
Writing objects: 100% (291436/291436), 184.90 MiB | 1.10 MiB/s, done.
remote: Building source:
...
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.3.4
remote: -----> Installing dependencies using bundler 1.15.2
...
remote:        Fetching rake 12.1.0
remote:        Fetching concurrent-ruby 1.0.5
...
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        == 20000225050318 AddSchemaMigrationDetails: migrating ========================
...
remote:        Bundling assets
remote:        I, [2017-12-21T18:03:24.428105 #5526]  INFO -- : Writing /tmp/build_d934d3b7398951364a32f3aa2455287a/public/assets/vendor-5a5945eb745675941f6bc539d75501b8c77028c134a7d343f51931413dae7fe1.js
...
remote: -----> Launching...
remote:        Released v15
remote:        https://discourse-edge.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.

Run heroku open to see the Discourse installation page, and browse to /latest to see the Discussion forum:

https://discourse-edge.herokuapp.com/latest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment