Deploying Discourse to Heroku

Recently I stumbled upon Discourse. Finally someone tackled that problem. Forums, while rich in content, have been so dull and unfriendly for so long. Anyway, I wanted to get  it up and running for myself, preferably on some cloud infrastructure, to play around. I’ve had previous experience with Heroku, so chose that setup, for no other reason. The Discourse’s own forum has plenty of other options so feel free to investigate.

As for the Heroku setup, there are existing guides, see [4] and [5]. The difference between them is that when using Discourse’s default configuration samples, you get Open Redis support, while I wanted to use Redis Cloud add-on, similar to Swrobel’s take on it. Also, I wanted to use Autoscaler for Sidekiq to reduce total cost on Heroku, as noted in Discourse’s document. All in all, I ended up with a mix of ideas from both sources. Add-ons used on Heroku:

Currently free plans are used for all of them as you can see below.

Heroku Configuration for a Discourse instance

To be able to repeat the process and easily deploy updates using Discourse’s Github repo, I’ve created a small Bash script for this. It basically performs the following tasks:

  • goes into your local Discourse git repo
  • creates a new branch, based on the current branch you’re on (all changes must be committed)
  • creates Redis configuration using provided sample and tweaked for Redis Cloud
  • adds mail configuration to production environment
  • removes clockwork from Procfile
  • set’s ruby version to 2.0 in Gemfile
  • configures Sidekiq for Autoscaler
  • creates temporary database and migrates it
  • precompiles assets using the above database
  • drops the temporary database
  • commits all configuration changes along with precompiled assets
  • pushes it all to Heroku
  • migrates the database on Heroku
  • and finally deletes the deployment branch

You can find the script here, feel free to use it, change it, do things to it you see fit 🙂 The main idea was to do as minimum manual work as possible, at least for this phase where I follow the default instructions closely. This of course might change, but for now is quite all right.

Prerequisites for executing the script:

  • You need to be able to run Discourse locally or at least to be able to precompile assets
  • This means you need:
    • PostgreSQL
    • Ruby 2.0
    • Cloned Discourse Github repo

When setting up PostgreSQL, take care to enable HStore on it [2] and to set appropriate discourse user permissions [3]. You can change username and password easily to match your setup.

Also, you need to replace the SECRET_TOKEN within the script and match it to your own setup. I took care to match it to the one set for the Heroku instance (heroku config:get SECRET_TOKEN).

Aside from the script, you should still follow the instructions on mentioned documents to e.g. create initial user, setup Amazon S3 upload etc.

Finally, the forum is up:

Running Discourse

Resources:

  1. Installing PostgreSQL on Ubuntu
  2. Enabling HStore for PostgreSQL
  3. Setting PostgreSQL permissions
  4. Swrobel’s take on Heroku deployment
  5. Discourse’s own Heroku deployment instruction
  6. Related topic on Discourse’s forum
Tagged , , , , , ,

9 thoughts on “Deploying Discourse to Heroku

  1. J says:

    firstly thank you for your write up on installing discourse on heroku, there’s no way being a complete rails newbie I could of got as far as I have without it.

    Before I forget, you might have a typo in your code here: “:port => ENV[‘SMTP_PORT’]’,\n \”

    I’m hitting abit of a roadblock near the end of the script I can’t figure out. the output is:

    ============ dropping deployment database ============
    ============ commiting changes ============
    [deploy_to_heroku cafb8d3] prepared for Heroku deployment
    7 files changed, 135 insertions(+), 9 deletions(-)
    create mode 100644 config/environments/production.rb
    rewrite config/initializers/sidekiq.rb (73%)
    create mode 100644 config/redis.yml
    ============ pushing to heroku ============
    fatal: ‘heroku’ does not appear to be a git repository
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.
    ============ migrating heroku database ============
    ! No app specified.
    ! Run this command from an app folder or specify which app to use with –app APP.
    ============ returning to trigger branch ============
    Switched to branch ‘master’
    ============ removing deploy branch ============
    Deleted branch deploy_to_heroku (was cafb8d3).
    ====================================================
    THAT’S ALL FOLKS
    ====================================================

    Everything looks good up to that point. Any ideas?

  2. elvanja says:

    Regarding the typo, corrected, thanks!

    And as for the other error, you have issue with pushing to heroku.
    This line says it: fatal: ‘heroku’ does not appear to be a git repository
    This line is executing: git push -f heroku HEAD:master

    This means that “heroku” is the name of the remote repository.
    E.g. when you git cloned the Discourse repo, you probably got “origin” remote repo.
    You need another remote repo that targets Heroku platform, and to which the script pushes the code.

    In my case, I named that remote repo “heroku”, hence the command in the script.
    If you already have that remote in your setup, just change the name in the script, or rename the remote in git.
    If you don’t have it, then you need to add it. https://devcenter.heroku.com/articles/git has all the info.

  3. alex says:

    I followed the steps above, but got an ‘ERR invalid DB index’ while trying to run the migration on heroku. I dug around and found that Discourse relies on multiple redis instances, which the free RedisCloud extension doesn’t support. I emailed the guys at garantiadata and confirmed that its not possible with the free package. Am I missing something?

    • elvanja says:

      @alex, I just upgraded Discourse to the latest version. Everything seems to be working OK.

      Mind you, with the latest version I had to change the script a bit, due to sidekiq configuration changes (they started using Sidetiq as well). Pick the new version from the same gist.

      Also, did you replace the SECRET_TOKEN in the script and set in heroku environment?

      Nothing else springs to mind at this time. Let me know how it goes.

  4. elvanja says:

    @J I am not running a production instance of Discourse on Heroku, just experimenting. That’s why I don’t have it running behind some real domain, but within my app subdomain on Heroku.

    That being said, I got the emails going out, that’s not an issue, but with inbound emails I didn’t even try since I can’t receive emails on that subdomain.

    Any luck for you?

    • J says:

      I wasn’t ever able to get emails going out or in. After all the problems I had setting it up on heroku the emails turned out to be a bridge too far for me. Would love to hear if someone manages it though

      • elvanja says:

        @J here is what I have set up for email:

        * Mandrill add-on on Heroku enabled
        * heroku config:set MANDRILL_APIKEY=…read_from_add-on….
        * heroku config:set MANDRILL_USERNAME=…read_from_add-on….
        * heroku config:set SMTP_PORT=587
        * heroku config:set SMTP_SERVER=smtp.mandrillapp.com

        In Discourse settings:
        * notification_email = info@YOUR_APP_(SUB)DOMAIN
        * reply_by_email_enabled = true
        * reply_by_email_address = %{reply_key}@YOUR_APP_(SUB)DOMAIN

        That is pretty much it.
        Hope it helps.

Leave a comment