Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: How to deploy a Ruby Discourse app on Google Cloud Platform description: Learn how to set up a Ruby Discourse app on Google Cloud Platform. author: hxiong388 tags: App Engine, Ruby, Ruby on Rails, Discourse, Redis, Postgres date_published: 2017-06-30


This tutorial shows how to create and configure a Ruby Discourse application to run on Google Cloud Platform (GCP) using the App Engine flexible environment.

Objectives

  • Set up dependency components, such as Redis and Postgres on GCP.
  • Deploy a Discourse application to App Engine flexible environment.
  • Verify the application is launched properly.

Before you begin

You'll need the following:

  • A GCP project. You can use an existing project or click the button to create a new project.
  • Ruby 2.3+ installation.
  • Cloud SDK installation.
  • A Redis instance running in your project. To set up Redis on Compute Engine, see Setting up Redis. This tutorial assumes the Redis instance is running in the default network so that the App Engine services can access it without restriction. Note the internal IPv4 address for later use.
  • A Postgres Cloud SQL instance running in your project. To set up a Postgres instance, see Creating instances.

Costs

This tutorial uses billable components of GCP including:

  • App Engine flexible environment
  • Cloud SQL instance
  • Compute Engine instance

Use the pricing calculator to generate a cost estimate based on your projected usage. GCP users might be eligible for a free trial.

Configure Cloud SQL Postgres instance

We're going to create a database named discourse on the Cloud SQL Postgres instance. This is going to be the production database your Discourse app will use.

  1. In the Cloud Platform Console, go to the Cloud SQL Postgres instance you have created.

  2. Go to the Databases tab. Create a new database with name discourse.

  3. Go to Access Control tab, and then Users sub-tab. Create a new user with username discourse and password discourse.

  4. Go back to the Overview tab, and click on the Connect using Cloud Shell button.

  5. In the Cloud Shell terminal that appears, run:

    gcloud beta sql connect  --user=discourse
    
  6. When prompted, type in the password discourse.

  7. After you connect to the instance, run:

    discourse=> GRANT ALL PRIVILEGES ON DATABASE discourse TO discourse;
    
  8. Close the Cloud Shell when done.

  9. On the same Overview page, note the Instance connection name for later use.

Configure Discourse Rails application

To download and install the Discourse Rails application locally, follow the official Discourse Advanced Developer Install Guide. You can also set up the local Postgres database and Redis server to test the app on your local machine in a development environment. We'll configure the Discourse app to run on the App Engine flexible environment with the production environment.

  1. Under the config/ directory in the Discourse app, create a file with name discourse.conf:

    # password used to access the db
    db_password = discourse
    
    # socket name for database connection
    db_host = /cloudsql/
    
    # Redis host address
    redis_host =
    
    # enable serve_static_assets for dockerized app
    serve_static_assets = true
    

This file sets configuration parameters in the production environment for Discourse.

  1. Open the config/puma.rb file, then change `APP_ROOT = '/home/discourse/discourse':

    APP_ROOT = Rails.root
    
  2. In the same config/puma.rb file, delete the line that says daemonize true. This is because we shouldn't run a daemonized process in Docker container.

  3. Save and close the file.

  4. Add the appengine gem to the application's Gemfile:

    gem "appengine", "~> 0.4"
    
  5. Install the gems:

    $ bundle install
    

Deploy Discourse app to the App Engine flexible environment

  1. Under the application root directory of the Discourse app, create a file named app.yaml:

    runtime: ruby
    env: flex
    entrypoint: bundle exec rails s -p 8080
    beta_settings:
      cloud_sql_instances:
    
  2. In the same application root directory, run this gcloud SDK command to deploy:

    VERSION=$(date +%Y%m%dt%H%M%S); \
        gcloud app deploy --version=$VERSION --no-promote \
        && bundle exec rake appengine:exec -- bundle exec rake db:migrate \
        && gcloud app services set-traffic default $VERSION=1
    

This will deploy the app to the App Engine flexible environment, run database migration, and then switch the load balancer to the new deployment.

Verify deployment

In your local terminal, run gcloud app browse. This will launch a new browser window that points to your new deployment. You should see the default Discourse welcome page.

Congratulations, you have successfully set up Discourse on the App Engine flexible environment with Redis and Postgres!

Cleaning up

After you've finished this tutorial, you can clean up the GCP resources you created so you won't be billed for them in the future. The following sections describe how to delete or turn off these resources.

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.
  2. Click the trash can icon to the right of the project name.

Warning: Deleting a project has the following consequences:

If you used an existing project, you'll also delete any other work you've done in the project. You can't reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL, remain available.

Deleting App Engine services

To delete an App Engine service:

  1. In the Cloud Platform Console, go to the App Engine Services page.
  2. Click the checkbox next to the service you wish to delete.
  3. Click Delete at the top of the page to delete the service.

If you are trying to delete the default service, you cannot. Instead:

  1. Click on the number of versions which will navigate you to App Engine Versions page.
  2. Select all the versions you wish to disable and click Stop at the top of the page. This will free all of the Compute Engine resources used for this App Engine service.
See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.