Cloud Run Button: Deploy Docker Images from Public Repositories to Google Cloud Run with a Single Click

If you have a public repository with a Dockerfile you can have users automatically deploy the container to Google Cloud Run by adding a Cloud Run Button. It’s no more than an image that links to https://deploy.cloud.run, like so:

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

Add that code to your README.md and when a visitor follows that link, Cloud Shell will open and it will Clone, Build, and Deploy the project onto Cloud Run for them. No need to manually create things in the Google Cloud Console nor use the gcloud binary 🙂

You basically only need the button, but you can tweak the source repository parameters through the querystring:

  • When no parameters are passed, the referer is used to detect the git repo and branch
  • To specify a git repo, add a git_repo=URL query parameter
  • To specify a git branch, add a revision=BRANCH_NAME query parameter.
  • To run the build in a subdirectory of the repo, add a dir=SUBDIR query parameter.

There’s also an app.json that you can optionally add to the root of your repo to tweak the deployment parameters.

{
    "name": "foo-app",
    "env": {
        "BACKGROUND_COLOR": {
            "description": "specify a css color",
            "value": "#fefefe",
            "required": false
        },
        "TITLE": {
            "description": "title for your site"
        },
        "APP_SECRET": {
            "generator": "secret"
        }
    },
    "options": {
        "allow-unauthenticated": false
    },
    "hooks": {
        "precreate": {
            "commands": [
                "echo 'test'"
            ]
        },
        "postcreate": {
            "commands": [
                "./setup.sh"
            ]
        }
    }
}

The values defined in the env key will be translated to prompts for Cloud Shell to ask. The props of each prompt is pretty straightforward but the special case of "generator": "secret" will ask/generate a secret.

By default it will deploy with allow-unauthenticated set to true but through the options you can override that.

The hooks part finally allows you to run commands in separate bash shells with the environment variables configured for the application and environment variables for GOOGLE_CLOUD_PROJECT, GOOGLE_CLOUD_REGION, and K_SERVICE.

~

Did this help you out? Like what you see?
Thank me with a coffee.

I don't do this for profit but a small one-time donation would always put a smile on my face. Thanks!

☕️ Buy me a Coffee (€3)

About the Author

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.