Continuous Deployment (CD) to Cloud Run

I have a personal project to create a generic RestAPI server using FastAPI that can be use as a template to extend for any use case. It should be able to handle PUT, GET, POST, DEL, PATCH and access a backend database.

I need the code to be able to run in Google cloud serverless compute platform call Cloud Run.

First, I need to get the code working in my local machine. I am using Visual Studio Code, or VScode in short.

Since Cloud Run is a container run time that is managed by Google, I need to provide a Dockerfile to instruct Google cloud to build the container image.

Next I need to provide a requirements.txt file to tell the container build services what python packages need to be included.

Last is the actual python code. It is just a simple python script using FastAPI package. I am a fan of FastAPI.

I am able to run the code locally by the following command

Once I get the script working on my computer, next is to sync with my Github. You can find out how to do it from Google some document. I won’t describe it here. Test and make sure all changes are commited and sync with Github from local computer, then you are set.

Next is to try to deploy my code to cloud run using Google Cloud CLI on my computer. This is how I did it.

$ cd <directory of the code>

$ gcloud run deploy iotapi –source .

Then you will need to anwer a few questions on the region you want the image to run (I pick asia-southeast1), and also select unauthenticated.

After few minutes of uploading the code to Google cloud, creating the container, deploying the container and setup the route. The code is running in cloud run and I was presented with an URL for my code.

Next is to configure continuous deployment at Cloud Run. You should see “set up continuos deployment” in the service details of the cloud run that you just deploy.

I will setup the repository provider (Github in my case) and also the name of the repository (the following images are taken from Google cloud documentation).

After the above steps, cloud build will be monitoring my github repository and when any new commit come in, it will trigger cloud build to take the new changes, create a container and deploy to cloud run.

I just tested it by cloning the github repository to another computer, make a small changes and commit the code and sync it with Github. After a few mintues, I see the changes reflected on Cloud Run.

Awesome. Now the CD is working. I can start adding stuff to my python script.

1% better everyday!!!

Leave a comment

Your email address will not be published. Required fields are marked *