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
- Make sure you have clever-tools installed locally or follow our CLI getting started guide.
- In your code folder, do
clever create --type <type> <app-name> --region <zone> --org <org>
where :type
is the type of technology you rely onapp-name
the name you want for your application,zone
deployment zone (par
for Paris andmtl
for Montreal)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
- Go to the Clever Cloud console, and find the app you want to fine tune under it’s organization.
- Find the Environment variables menu and select it.
- In this menu, you will see a form with VARIABLE_NAME and variable value fields.
- Fill them with the desired values then select Add.
- Don’t forget to “Update Changes” at the end of the menu.
With the Clever Tools CLI
- Make sure you have clever-tools installed locally. Refer to our CLI getting started.
- 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
orGemfile.lock
file - you have a
config.ru
file (this one is most of the time generated by rails) - you have
gem puma
in yourGemfile
(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.
- generate the secret key locally with
rake secret
- 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
inconfig/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 redirectionserror_pages
: configure custom files for error pagesforce_https
: automatically redirect HTTP traffic to HTTPSaliases
: set up redirectionscharset
: 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
orCC_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"]
.
Make sure to log on stdout
Since January 2024, your logs may no longer show in your Clever Cloud console. In production.rb, you should find something like:
if ENV['RAILS_LOG_TO_STDOUT'].present?
logger = ActiveSupport::Logger.new($stdout)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
It means you need to add RAILS_LOG_TO_STDOUT=true
in your environment variables.
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
The “Information” page of your app gives you your Git deployment URL, it looks like this:
git+ssh://git@push.clever-cloud.com/<your_app_id>.git
- Copy it in your clipboard
Locally, under your code folder, type in
git init
to set up a new git repository or skip this step if you already have oneAdd the deploy URL with
git remote add <name> <your-git-deployment-url>
Add your files via
git add <files path>
and commit them viagit commit -m <your commit message>
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.
Enable health check during deployment
The healthcheck allows you to limit downtimes. Indeed, you can provide Clever Cloud with paths to check. If these paths return something other than 200, the deployment will fail.
Add one (or several) environment variable as such:
CC_HEALTH_CHECK_PATH=/my/awesome/path
Or
CC_HEALTH_CHECK_PATH_0=/my/awesome/path
CC_HEALTH_CHECK_PATH_1=/my/other/path
The deployment process checks all paths. All of them must reply with a 200 OK
response code.
By default, when no environment variable (for ex: APP_HOME
) is defined, the monitoring checks your repository root path /
.
Example
Using the path listed above, below are the expected logs:
Response from GET /my/awesome/path is 200
Response from GET /my/other/path is 500
Health check failed:
- GET /my/other/path returned 500.
If the deployment fails after this message, please update your configuration and redeploy.
In this example, the first path is OK, but the second one failed. This gives you a hint on what failed in your application.
Best practice for healthcheck endpoints
To make the most of a healthcheck endpoint, have it check your critical dependencies. For example:
- execute
SELECT 1 + 1;
on your database - retrieve a specific Cellar file
- ping a specific IP through a VPN