Automate Cloud Run deployment in a minute

Deploying an app has become so easy that you can deploy it to any server with a single click or a simple code push.

Here, I am going to show you how to automate your deployment of dockerized GO app to Google Cloud Run with the help of Github Action Workflow on every push within a minute with following steps -:

  1. Run Go locally
  2. Run Go on Google Cloud Run

Note -: I assume you have a basic understanding of working with docker & have worked with Google Cloud Run to some extent

  1. Running Go locally -:
+-- Dockerfile
+-- docker-compose.yml
+-- app
| +-- app.go

This is what my docker file looks like


FROM golang:1.10COPY ./app /go/src/
WORKDIR /go/src/
RUN go get -u && go get ./RUN go buildCMD go get && \
fresh; \

and I have created docker-compose.yml with the following content


version: '2'
container_name: golang.go
build: ./
- VIRTUAL_HOST=custom.pv
- APP_ENV=dev
- ./app:/go/src/
- "5000:8080"

It will pull GO from the main image source, setup the current working directory along with GIN package and build binary. Fresh is used for hot reloading, whenever something changes in code which makes coding a lot easier in GO.

And my final file app.go looks like this


package mainimport (
func regRoutes(routes *gin.Engine) { routes.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "up",
})}func main() { routes := gin.Default()
routes.Run() // run in 8080 port

As our local setup has been ready to kick in. Let’s test by running .

Go to your browser and run http://localhost:5000.

If you should see then that’s great! You have finally containerized GO locally.

You can take a break now :)

2) Run Go on Google Cloud Run -: Now we will be using Github Action Workflows which is the GitHub default pipeline for CI / CD where you can do a lot of things like build, run test, deploy, etc. However, in this session, we will only be deploying our code to Cloud Run on every push.

We will now add a file called on with following contents


Let’s take a quick walk through this code. On every push to the branch, this script will get triggered. This script uses the Ubuntu environment.

So are generally part of partial code in someone's GitHub with some action defined on docker. We use the default to check out our code to current pushed branch i.e .

After we give the name which uses actions/gcloud/auth@master to authenticate in Gcloud with given `GCLOUD_AUTH`. This should be 64baseEncoded string of your JSON credentials. You can use it to convert to JSON.

Note: Your account should have Cloud Build Service Account & Cloud Run Admin access

` GCLOUD_PROJECT_ID` to specify a project.

After successful authentication, we will build our docker image to give it a name and push it to Gooogle Container Registry (GCR).

Let’s install the required libs and configure docker for Google Cloud

components install beta --quiet gcloud auth configure-docker

Now we need to build an image and push it to Google Cloud so we will build and image tag and push that tagged image in Google Cloud.

docker build -t golangcloudrun:latest .docker tag  golangcloudrun:latest${{ secrets.GCLOUD_PROJECT_ID }}/golangcloudrun docker push${{ secrets.GCLOUD_PROJECT_ID }}/golangcloudrun:latest 

While deploying to Google Cloud Run we will be asked which service to select, so we either need to use an existing service or create a new one.

You can create a service on run time programmatically. There are great tools like to help you out.

For this, we will create a service manually. Go to Google Cloud and click on Google console. Now a dashboard will appear, click the navigation menu and click on create a new service.

Once we get an image in our GCR now we can easily deploy our image to Cloud Run with commands -:

** components install beta --quiet **** gcloud beta run deploy ${CLOUD_RUN_SERVICE_NAME} — image${{ GCLOUD_PROJECT_ID }}/golangcloudrun — platform managed — region us-central1 — project=${{ secrets.GCLOUD_PROJECT_ID }} **

This will install required libs, take our image from GCR and deploy it on Cloud Run with given service and project id. Now you are done. Its time to watch green signals now. Now go to your repo and click on actions you should see something like this.

The above script runs only when something is pushed on branch, if you want more you can have a look at for doing more greater stuff.

It’s just a small piece of an example to show you that with less effort you can really automate your workflows.

You can find the complete code at

Happy Coding!

Backend Dev@ Werkspot