Deploy Strapi V4 (CMS) in Google Cloud Run using GitHub, Google Cloud Build and integrate Google Cloud SQL (MySQL) and Google Cloud Storage — Part 1
Prerequisites
- Google Cloud Project
- Github Account
- The installation requires the following software to be already installed on your computer:
- Node.js (opens new window): only LTS versions are supported (v14 and v16). Other versions of Node.js may not be compatible with the latest release of Strapi. The 14.x version is most recommended by Strapi.
- npm (opens new window)(v6 only) or yarn (opens new window)to run the CLI installation scripts.
- Python (opens new window)when using an SQLite database
Step 1 — Install Strapi
In your local terminal run the following command. Replace my-project
with your project name. I am using yarn here, if you are interested you can use npx as well.
yarn create strapi-app my-project --quickstart
The above command will install and launch your Strapi application locally. Now you can create an account and play with it! 🥳🥳🥳
The application is using SQLite embedded database which is installed locally, it is more than enough for development purposes. You can also configure a remote database if you want.
Step 2 — Configure Strapi
Change directory to my-project
cd my-project
Install MySQL dependency
yarn add mysql
Open the Strapi project folder in Visual Studio Code and modify server.js
File Path =./config/server.js
module.exports = ({ env }) => ({
host: env.HOST,
port: env.PORT,
app: {
keys: env.array('APP\_KEYS'),
},
});
Create a folder env/production inside the config folder and create the following files in it.
File Path: ./config/env/production/database.js
module.exports = ({ env }) => ({
connection: {
client: 'mysql',
connection: {
socketPath: env('INSTANCE\_UNIX\_SOCKET'),
database: env('DB\_NAME'),
user: env('DB\_USER'),
password: env('DB\_PASS'),
}
},
});
File Path: ./config/env/production/server.js
module.exports = ({ env }) => ({
url: env('MY\_GCR\_URL'),
});
Step 3 — Create Dockerfiles
Create Dockerfile and .dockerignore files in the root directory.
File Path = ./Dockerfile
FROM node:16
\# Installing libvips-dev for sharp Compatability
RUN apt-get update && apt-get install libvips-dev -y
ARG NODE\_ENV=production
ENV NODE\_ENV=${NODE\_ENV}
WORKDIR /opt/
COPY ./package.json ./yarn.lock ./
ENV PATH /opt/node\_modules/.bin:$PATH
RUN yarn config set network-timeout 600000 -g && yarn install
WORKDIR /opt/app
COPY ./ .
RUN yarn build
EXPOSE 1337
CMD \["yarn", "start"\]
File Path = ./dockerignore
.tmp/
.cache/
.git/
build/
node\_modules/
data/
Step 4 — Publish to Github
Click on the Publish to GitHub button and select the private repo.
Step 5 — Create Google Cloud SQL MySQL instance
Customize your instance based on your needs. I have selected a lightweight configuration and enabled Public IP.
Click on Create Instance button and let us wait for some time. Go, grab some snacks and coffee, then enjoy! 👀🍔🍟🍕
After the instance is created, select Databases and create a database names strapi-prod-db.
Go to Overview and find your Public IP address and Connection name.
Step 6— Create Google Cloud Run Service
Select Continuously deploy new revisions from a source repository and Set up with cloud build.
Enable Cloud Build API and **Container Analysis API.
**Follow instructions and connect your GitHub Repository and click NEXT.
In Build Configuration select build type as Dockerfile and click SAVE.
Select the Authentication option as Allow unauthenticated invocations.
Set Container port to 1337
Add all environment variables from the .env file in the root folder to container configurations.
also, you need to add some additional environment variables
INSTANCE_UNIX_SOCKET (value = /cloudsql/Connection name)
DB_NAME (value = strapi-prod-db)
DB_USER (value = root)
DB_PASS (value = your database password)
MY_GCR_URL (value = you can update this value after the instance is created)
Now in the Container, Connections, Security section go to Connections and add a cloud SQL connection
Enable Cloud SQL Admin API and select your cloud SQL instance.
Click on Create button and wait for the deployment to be completed.
Don't worry if your build got failed for the first time. sometimes build will be failed due to time out. This is because the default time of execution is 10 minutes, for increasing this value, after the first build is completed, click on the Edit Continuous Deployment
then click on the open editor button.
Add a time-out statement as follows.
Click Done and Save.
also, you can update the MY_GCR_URL environment variable by clicking Edit and deploy new version button with the URL displayed in the instance dashboard.
and deploy.
Step7 — Get Ready for the Magic!
Commit some changes and push them to your GitHub repository, Google cloud build will auto-deploy the new version into the Google cloud run. After the deployment is completed you can see your Strapi up and running in the MY_GCR_URL.
Congratulations, you have completed the first part of this learning series.
In the upcoming part two, we will integrate Google Cloud Storage into Strapi V4. Till then, Enjoy! 🥳
Thanks.
We will integrate Google Cloud Storage in Part 2.
❤️ Feeling the love? Give me a clap on Medium!