Skip to main content

Google Cloud Run

Requirements

The following code sample demonstrates how to build and deploy an application on Google Cloud Run using a Dagger module. It assumes:

  • A Node.js Web application
  • A Google Cloud service account with all necessary privileges and with the Google Cloud APIs enabled
  • A Google Cloud Run service with a public URL and defined resource/capacity/access rules
  • A Google Artifact Registry repository

In the application directory, create a new Dagger module:

dagger init --name=my-module --sdk=go --source=./dagger

Install the Google Cloud Run module from the Daggerverse:

dagger install github.com/vvaswani/daggerverse/google-cloud-run

Update the generated dagger/main.go file with the following code:

package main

import (
"context"
"fmt"

"main/internal/dagger"
)

type MyModule struct{}

// build an image
func (m *MyModule) Build(source *dagger.Directory) *dagger.Container {
return dag.Container().
From("node:21").
WithDirectory("/home/node", source).
WithWorkdir("/home/node").
WithExec([]string{"npm", "install"}).
WithEntrypoint([]string{"npm", "start"})
}

// publish an image
// example: dagger call publish --source . --project PROJECT --location LOCATION --repository REPOSITORY/APPNAME --credential env:GOOGLE_JSON
func (m *MyModule) Publish(ctx context.Context, source *dagger.Directory, project string, location string, repository string, credential *dagger.Secret) (string, error) {
registry := fmt.Sprintf("%s-docker.pkg.dev/%s/%s", location, project, repository)
return m.Build(source).
WithRegistryAuth(fmt.Sprintf("%s-docker.pkg.dev", location), "_json_key", credential).
Publish(ctx, registry)
}

// deploy an image to Google Cloud Run
// example: dagger call deploy --source . --project PROJECT --registry-location LOCATION --repository REPOSITORY/APPNAME --service-location LOCATION --service SERVICE --credential env:GOOGLE_JSON
func (m *MyModule) Deploy(ctx context.Context, source *dagger.Directory, project, registryLocation, repository, serviceLocation, service string, credential *dagger.Secret) (string, error) {
// publish image
addr, err := m.Publish(ctx, source, project, registryLocation, repository, credential)
if err != nil {
return "", err
}

// update service with new image
return dag.GoogleCloudRun().UpdateService(ctx, project, serviceLocation, service, addr, 3000, credential)
}

This Dagger module contains three Dagger Functions. Let's look at each in detail.

The Build() function takes care of building the application image.

  • It accepts one argument: a Directory representing the directory containing the application source code.
  • It uses the client's Container().From() method to initialize a new container from a base image. In this example, the base image is the node:21 image. This method returns a Container representing an OCI-compatible container image.
  • It uses the Container.WithDirectory() method to return the container image with the application source code written at the /home/node path, and the Container.WithWorkdir() method to set the working directory in the container.
  • It chains the Container.WithExec() method again to install dependencies with npm install and sets the container entrypoint using the Container.WithEntrypoint() method.

The Publish() function takes care of publishing the container image to Google Artifact Registry.

  • It accepts six arguments: the Go context, a Directory representing the directory containing the application source code, and strings representing the Google Cloud project name, Google Artifact Registry location and Google Artifact Registry repository name, and a Secret representing Google Cloud credentials.
  • It invokes the Build() function to produce a Container representing the application image.
  • It uses the Container.Publish() method to publish the container to the Google Artifact Registry repository and returns the SHA identifier of the published image.

The Deploy() function takes care of deploying the application to an existing Google Cloud Run service.

  • It accepts eight arguments: the Go context, a Directory representing the directory containing the application source code, and strings representing the Google Cloud project name, Google Artifact Registry location, Google Artifact Registry repository name, Google Cloud Run service location, Google Cloud Run service name, and a Secret representing Google Cloud credentials.
  • It invokes the Publish() function to build and publish a Container representing the application image.
  • It uses the Google Cloud Run module to deploy the container image to the named Google Cloud Run service.
warning

The code sample above is illustrative only. Modify it to your application's specific requirements.

Here is an example of calling the Dagger Function to deploy the application to Google Cloud Run. Remember to update the registry-location, repository, service, and service-location arguments, and update the SERVICE-ACCOUNT-KEY-FILE placeholder with the local filesystem path to your JSON service key file.

dagger call deploy \
--source=. \
--project=myproject \
--registry-location=us-central1 \
--repository=myregistry/myapp \
--service-location=us-central1 \
--service=myservice \
--credential=file:SERVICE-ACCOUNT-KEY-FILE