We are going to deploy a rails app into Google Cloud Run and Cloud SQL. This will make the rails app serverless.
We will follow the following steps to deploy our app.
When running the app, it took over 20 seconds to respond for the first run. Many configurations can be adjected to improve performance. Please feel free to comment with suggestions.
Setup Google Cloud SQL for Mysql.
Click on "show more configuration options". and enable private IP.
Write down the private IP.
Connect using gcloud by pressing enter in the terminal you should see a similar command shown in your shell.
Enter the root password that you choice earlier and press enter.
Setup Cloud Run
Clone this repository https://github.com/hadyrashwan/ruby-on-rails-getting-started and enter the folder.
-
Generate the secret key using the following command. For demo purposes, we can use this one
e2348e9aaf6da01aea29f03eef0a09a6d7c330ac2571043a126335ce62bc087f70931a54e6bf3bc8f6edff93e0fc502fb34c8af2cab31581b00507138088f230
It is recommended to excuate the following command to get your own secret key.
$ rake secret
Install gcloud from gcloud download page.
Run the following command to deploy.
./deploy.sh
When asked to enable the cloud build API to enable it and
then you may be asked where to deploy it choose us central
.
- Write down the deployment URL.
Connect Cloud Run with Cloud Sql.
Update your environment variables. They should be as follow this table:
Key | Value |
---|---|
BLOG_DATABASE_HOST | Private IP |
BLOG_DATABASE_PASSWORD | Password |
SECRET_KEY_BASE | Secret key |
- It should look like this.
- Finally, click
Deploy
.
Done
Your app should be ready now go to the deployment URL on your browser.
It may ask you to do a database migration do so.
In my test, the response took over 20 seconds coming from a cold start(first time hitting the app) and 254 ms afterwards.
I disabled the Webpack caching mechanism and used environment variables instead of the secret manger for the demo purposes.
Top comments (0)