Uncovering the Magic of Click-to-Deploy for Cloud Run

James Ward
Google Cloud - Community
4 min readJan 21, 2020

I love using a command line but when I want to give something a quick spin I don’t always want to install tools and then copy & paste a bunch of commands. I’d rather just click a button. That is exactly what Cloud Run Button does for deploying GitHub repos on Cloud Run. Check it out:

Any GitHub repo containing a serverless app can enable the Cloud Run Button simply by adding the following markdown to the README.md file:

[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run)

When a user clicks the button it will launch Cloud Shell and do a bunch of magic to make those “quick spins” easier. To skip right to the actual docs, check out the GitHub repo for Cloud Run Button. If you are still reading, let’s walk through all the stuff that Cloud Run Button does.

1. Git Clone

The repo the button was clicked from is cloned into a temporary Cloud Shell environment. A branch and subdirectory can be manually specified, or it will be automatically detected based on the referrer URL.

2. Parse app.json

Sometimes a repo needs to configure some things about how the Button will build and deploy the project. Following the Heroku Button’s app settings style, those settings are specified in an app.json file in the project directory. There are a bunch of settings we will walk through in a minute, but the one to point out here is that the Cloud Run service name and Container Registry image name can be set using a name field. (Check out the full docs for Cloud Run Button’s app.json settings.)

3. Project Setup / Selection

Everything in Google Cloud Platform is part of a “project” thus users need to select a project. There is a nice project selector if the user already has a bunch. If the user doesn’t have a project, the Button helps them do that.

4. Billing Account Verification

To deploy apps on Cloud Run you need to have a billing account setup, even if you won’t actually get charged anything because you are in the Cloud Run free tier, or using some of your free trial credits. Cloud Run Button verifies that you have a billing account associated with your project and if not, it helps you set one up.

5. Cloud Run API

For Cloud Run Button to deploy an app on Cloud Run, it needs to enable the Cloud Run API. So it does that if needed.

6. Region Selection

Google Cloud Platform is organized into regions which are made up of zones delineating network segments and Service Level Agreements. Cloud Run is regional as it is automatically redundant across the underlying zones of a given region. You can select from a number of regions to deploy your app in, so the Button prompts the user for the region.

7. Environment Variables

Cloud Run uses environment variables for configuration and so a project’s app.json file can specify those. The values can either be generated randomly or the Button will prompt the user for the values. (Check out the full docs for Cloud Run Button’s app.json settings.)

8. Build the project

The Button will now build the project from source into a container image since that is the format needed for Cloud Run. First the Button checks for a Dockerfile and if it finds one it builds it with Docker. If there wasn’t a Dockerfile then the Button checks for a Maven (Java build tool) project that has the Jib plugin (Java build tool plugin for creating container images) setup. In that case it runs the Maven Jib build task to build the container image. If there wasn’t a Maven Jib-enabled project, then the final thing the Button tries is to build the project using the Cloud Native Buildpacks via the Heroku Buildpacks which can build most Java, Python, Node, Ruby, Go, and PHP projects.

9. Push Container Image

Cloud Run needs the container image to be in the Google Container Registry so unless the build step did the push, the Button pushes it from the local docker daemon (i.e. it uploads it from Cloud Shell to GCR).

10. Run Pre-Deployment Hook Commands

A project’s app.json file can specify commands that will be run before the Cloud Run service is created. Those scripts are run if the service does not yet exist (Note: The Button can deploy a new version of an existing service too.)

11. Deploy Cloud Run Service

The project is now deployed to Cloud Run using the container image in GCR! This sets the environment variables and options (currently just whether or not to allow unauthenticated requests) on the new service.

12. Run Post-Deployment Hook Commands

Just like the precreate commands, if postcreate commands are specified in app.json then those are run. This can be used for provisioning other resources like storage buckets, Cloud SQL instances, service accounts, etc. These commands can use the gcloud command to add new env vars or change other settings on the newly deployed service.

So there you have the magic of a Button! Just click and all sorts of things just happen to get an app running on Cloud Run. Add it to your repos and let us know if we can help with anything!

--

--

James Ward
Google Cloud - Community

I write code, talk about Monads, and help devs learn Google Cloud.