Automate Cloud Run deployment in a minute

ujwal dhakal
4 min readNov 6, 2019

--

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

Dockerfile

FROM golang:1.10COPY ./app /go/src/github.com/user/sites/app
WORKDIR /go/src/github.com/user/sites/app
RUN go get -u github.com/gin-gonic/gin && go get ./RUN go buildCMD go get github.com/pilu/fresh && \
fresh; \
EXPOSE 8080

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

docker-compose.yml

version: '2'
services:
golang.go:
container_name: golang.go
build: ./
environment:
- VIRTUAL_HOST=custom.pv
- APP_ENV=dev
volumes:
- ./app:/go/src/github.com/user/sites/app
ports:
- "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

app.go

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

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

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

If you should see {“status”: “up”}, 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 deploy.yml on .github/workflows/ with following contents

deploy.yml

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

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

After we give the name Authenticate on Google Cloud 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 base64 path/to/jsonFile 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 gcr.io/${{ secrets.GCLOUD_PROJECT_ID }}/golangcloudrun docker push gcr.io/${{ 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 https://www.terraform.io/docs/providers/google/r/cloud_run_service.html 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 Cloud Run 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 gcr.io/${{ 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 master branch, if you want more you can have a look at https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions. 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 https://github.com/ujwaldhakal/Github-action-cloud-run-demo.

Happy Coding!

--

--