Skip to main content

Deploy to Google Cloud Run with Dagger

This tutorial illustrates how to use Dagger to build, push and deploy Docker images to Cloud Run.

Initialize a Dagger Workspace and Environment

(optional) Setup example app

You will need the local copy of the Dagger examples repository used in previous guides

git clone https://github.com/dagger/examples

Make sure that all commands are being run from the todoapp directory:

cd examples/todoapp

(optional) Initialize a Cue module

This guide will use the same directory as the root of the Dagger workspace and the root of the Cue module, but you can create your Cue module anywhere inside the Dagger workspace.

cue mod init

Organize your package

Let's create a new directory for our Cue package:

mkdir gcpcloudrun

Create a basic plan

todoapp/gcpcloudrun/source.cue
package gcpcloudrun
import (    "alpha.dagger.io/dagger"    "alpha.dagger.io/docker"    "alpha.dagger.io/gcp"    "alpha.dagger.io/gcp/cloudrun"    "alpha.dagger.io/gcp/gcr")
// Source code of the sample applicationsrc: dagger.#Artifact & dagger.#Input
// GCR full image nameimageRef: string & dagger.#Input
image: docker.#Build & {    source: src}
gcpConfig: gcp.#Config
creds: gcr.#Credentials & {    config: gcpConfig}
push: docker.#Push & {    target: imageRef    source: image    auth: {        username: creds.username        secret: creds.secret    }}
deploy: cloudrun.#Service & {    config: gcpConfig    image:  push.ref}

Set up the environment

Create a new environment

Now that your Cue package is ready, let's create an environment to run it:

dagger new 'gcpcloudrun' -p ./gcpcloudrun

Configure user inputs

dagger input dir src . -e gcpcloudrundagger input text deploy.name todoapp -e gcpcloudrundagger input text imageRef gcr.io/<your-project>/todoapp -e gcpcloudrundagger input text gcpConfig.region us-west2 -e gcpcloudrundagger input text gcpConfig.project <your-project> -e gcpcloudrundagger input secret gcpConfig.serviceKey -f ./gcp-sa-key.json -e gcpcloudrun

Deploy

Now that everything is set correctly, let's deploy on Cloud Run:

dagger up -e gcpcloudrun