Ruby on Rails

Ruby on Rails

Overview

Ruby on Rails is an open source web application framework which runs on the Ruby programming language. It is a full-stack framework: it allows creating pages and applications that gather information from the web server, talk to or query the database, and render templates out of the box. As a result, Rails features a routing system that is independent of the web server.

Clever Cloud allows you to deploy any Ruby on Rails application. This page will explain you how to set up your application to run it on our service. You do not need to change a lot in your application, the requirements will help you configure your applications with some mandatory files to add, and properties to setup.

You can find here an example of Ruby on Rails application on Clever Cloud.

Create an application on Clever Cloud

With the web console

Refer to Quickstart for more details on application creation via the console.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally or follow our CLI getting started guide.
  2. In your code folder, do clever create --type <type> <app-name> --region <zone> --org <org> where :
    1. type is the type of technology you rely on
    2. app-name the name you want for your application,
    3. zone deployment zone (par for Paris and mtl for Montreal)
    4. org the organization ID the application will be created under.

Refer to clever create for more details on application creation with Clever Tools.

Setting up environment variables on Clever Cloud

With the Clever Cloud console

  1. Go to the Clever Cloud console, and find the app you want to fine tune under it’s organization.
  2. Find the Environment variables menu and select it.
  3. In this menu, you will see a form with VARIABLE_NAME and variable value fields.
  4. Fill them with the desired values then select Add.
  5. Don’t forget to “Update Changes” at the end of the menu.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally. Refer to our CLI getting started.
  2. In your code folder, do clever env set <variable-name> <variable-value>

Refer to environment variables reference for more details on available environment variables on Clever Cloud.

You can of course create custom ones with the interface we just demonstrated, they will be available for your application.

Configure your Ruby on Rails application

Mandatory configuration

Be sure that:

  • you push to the master branch
  • you commit your gems.locked or Gemfile.lock file
  • you have a config.ru file (this one is most of the time generated by rails)
  • you have gem puma in your Gemfile (puma is the default application server from rails, when creating a new ruby application, an environment variable is automatically added: CC_RACKUP_SERVER=puma)

You need to provide a gems.locked or Gemfile.lock file. To do that ensure you have at least once run bundle install in your terminal at the root of your rails project.

Choose ruby version

If you specify a ruby version in your gems.rb of Gemfile, we’ll use it, otherwise; keep reading.

On your Clever Cloud application create an environment variable CC_RUBY_VERSION=rubyversion where rubyversion represents:

  • “3” will select the greatest “3.X.Y” version available.
  • “3.3” will select the greatest “3.3.Y” version available.
  • “3.3.1” will select the “3.3.1” version.

Due to current landscape in ruby applications, the default version is the greatest 3.3.Y. We also provide versions 2.3.Y, 2.4.Y, 2.5.Y, 2.6.Y and 2.7.Y.

If given rubyversion does not match any available version, your deployment will fail.

Choose your environment

You can set the RUBY_ENV environment variable to the value you want. By default it is set to production. Most used values are production and development but you can use any custom one as long as you have set up of the required variables for this environment in your ./config/ folder files.

Secure configuration of secret key

There are many way to add secret key to your environment and each one is valid. Clever Cloud provides you a secure way so you don’t have to commit any file containing it.

  1. generate the secret key locally with rake secret
  2. add it to your environment in ./config/secret.yml with:
production:
    secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

Configure Rake goals

You can specify a list of rake goals to execute before the deployment of your application by using the CC_RAKEGOALS environment variable.

The value of this variable must be a comma-separated list of goals, for instance:

CC_RAKEGOALS="db:migrate, assets:precompile"

We do not execute any rake goals by default.

Configure Sidekiq

To run a Sidekiq process in background you will need to enable it with the CC_ENABLE_SIDEKIQ=true environment variable.

Please note you will need a Redis instance to use this feature.

You can specify multiple Sidekiq configuration files with the CC_SIDEKIQ_FILES environment variable.

The value of this variable must be a comma-separated list of files:

CC_SIDEKIQ_FILES="./config/sidekiq_1.yml,./config/sidekiq_2.yml,./config/sidekiq_3.yml"

Note: CC_SIDEKIQ_FILES have precedence over CC_ENABLE_SIDEKIQ which means that even if CC_SIDEKIQ_FILES is defined and CC_ENABLE_SIDEKIQ is set to false, Sidekiq will still be enabled.

Each path is the path from the root of the repository to a sidekiq configuration file.

Each file might contain something like this standard sidekiq.yml file:

---
:concurrency: 4
:pidfile:     tmp/pids/sidekiq_1.pid
:queues:
  - [critical, 2]
  - default

production:
  :logfile: ./log/sidekiq.log
---

Manage your static files and assets

Static files are configured with environment variables:

STATIC_FILES_PATH: should point to a directory where your static files are stored.

STATIC_URL_PREFIX: the URL path under which you want to serve static files (for example /public/)

Note: the path of your folder must be absolute regarding the root of your application.

Note: setting the STATIC_URL_PREFIX to / will make the deployment to fail.

If you use the asset pipeline, make sure to include the assets:precompile task in the CC_RAKEGOALS environment variable value.

CC_RAKEGOALS="db:migrate, assets:precompile"

Note: if your project uses webpacker, make sure to enable the dedicated build instance option in the Information menu of your application in the Clever Cloud console because webpacker needs a lot a resources when starting.

Using Active Storage and Cellar S3

Only for Rails >= 5.2.

  • Enable Active Storage for your application: $ rails active_storage:install then $ rake db:migrate

  • Add gem "aws-sdk-s3", require: false to your Gemfile, run $ bundle install

  • Add config.active_storage.service = :clevercloud in config/environments/production.yml

  • Add in config/storage.yml:

    clevercloud:
      service: S3
      access_key_id: <%= ENV['CELLAR_ADDON_KEY_ID'] %>
      secret_access_key: <%= ENV['CELLAR_ADDON_KEY_SECRET'] %>
      region: us-east-1
      bucket: <%= ENV['CELLAR_ADDON_BUCKET_NAME'] %>
      endpoint: <%= ENV['CELLAR_ADDON_ENDPOINT'] %>
      force_path_style: true
  • In the clever cloud console create a Cellar S3 storage add-on, name it, link it to your rails application and create a bucket.

  • In the environment variables section of your Ruby on Rails application on Clever Cloud add the following environment variables:

    CELLAR_ADDON_BUCKET_NAME="<your bucket name>"
    CELLAR_ADDON_ENDPOINT="https://cellar-c2.services.clever-cloud.com"

You can now commit and push your changes.

Also, you are able to use a Filesystem Bucket to store your static files. Please refer to the File System Buckets section.

NGINX configuration

NGINX settings can be configured with environment variables:

  • NGINX_READ_TIMEOUT: the response timeout in seconds. (Default: 300)

Basic authentication

If you need basic authentication, you can enable it using environment variables. You will need to set CC_HTTP_BASIC_AUTH variable to your own login:password pair. If you need to allow access to multiple users, you can create additional environment CC_HTTP_BASIC_AUTH_n (where n is a number) variables.

NGINX optional configuration with clevercloud/http.json

NGINX settings can be configured further in clevercloud/http.json. All its fields are optional.

  • languages: configure a default language and redirections
  • error_pages: configure custom files for error pages
  • force_https: automatically redirect HTTP traffic to HTTPS
  • aliases: set up redirections
  • charset: force a specific charset
{
    "languages": {
        "default": {"rewrite": "en"},
        "fr": {"rewrite": "en"}
    },
    "error_pages": {
        "404": "path/to/page"
    },
    "force_https": true,
    "aliases": {
        "/path": "redirection"
    },
    "charset": "latin-1"
}

Puma configuration

Puma reads its configuration from the config/puma.rb file. See the puma documentation for more information.

You can override this configuration with environment variables. Each of them, when specified, will be preferred over the setting from config/puma.rb.

  • CC_PUMA_WORKERS overrides the number of workers (for example, CC_PUMA_WORKERS=2)
  • CC_PUMA_THREADS overrides the number of threads per worker, can be a raw number or a range (for example, CC_PUMA_THREADS=6 or CC_PUMA_THREADS=4:8)

If they are not defined in the environment nor in config/puma.rb we will setup the values depending on the size of the scaler your application is running on. We also fill the WEB_CONCURRENCY and RAILS_MAX_THREADS environment variable if they are not present as they may be used by rails’ puma configuration.

Overriding rackup application server

You can override the puma default server by setting an environment variable CC_RACKUP_SERVER=yourserver. We do not recommend you do it.

Monitor your application with New Relic

You can use New Relic to monitor your application on Clever Cloud.

Please refer to our New Relic documentation to configure it for your application.

Environment injection

Clever Cloud injects environment variables from your application settings as mentioned in setting up environment variables and is also injecting in your application production environment, those from your linked add-ons.

Custom build configurations

On Clever Cloud you can define some build configuration: like the app folder to deploy or the path to validate your application deployment is ready To do that follow the documentation here and add the environement variable you need.

To access environment variables from your code, just get them from the environment with ENV["MY_VARIABLE"].

Git Deployment on Clever Cloud

You need Git on your computer to deploy via this tool. Here is the official website of Git to get more information: git-scm.com

Setting up your remotes

  1. The “Information” page of your app gives you your Git deployment URL, it looks like this:

    1. git+ssh://git@push.clever-cloud.com/<your_app_id>.git
    2. Copy it in your clipboard
  2. Locally, under your code folder, type in git init to set up a new git repository or skip this step if you already have one

  3. Add the deploy URL with git remote add <name> <your-git-deployment-url>

  4. Add your files via git add <files path> and commit them via git commit -m <your commit message>

  5. Now push your application on Clever Cloud with git push <name> master

Refer to git deployments for more details.

Linking a database or any other add-on to your application

By linking an application to an add-on, the application has the add-on environment variables in its own environment by default.

On add-on creation

Many add-ons do exist on Clever Cloud: refer to the full list and check add-ons dedicated pages for full instructions.

During add-on creation, an Applications screen appears, with a list of your applications. You can toggle the button to Link and click next. If you finish the process of add-on creation, the application is automatically linked to it.

Add-on already exists

In the Clever Cloud console, under the Service Dependencies menu of your application, you can use the Link add-ons dropdown menu to select the name of the add-on you want to link and use the add button to finish the process.

You can also link another application from the same page in the Clever Cloud console, using the Link applications dropdown menu.

More configuration

Need more configuration? To run a script at the end of your deployment? To add your private SSH key to access private dependencies?

Go check the Common configuration page.

You may want to have an advanced usage of your application, in which case we recommend you to read the Administrate documentation section.

If you can’t find something or have a specific need like using a non supported version of a particular software, please reach out to the support.

Last updated on

Did this documentation help you ?