Google Cloud Run
Dagger can be used to deploy any containerized application to Google Cloud Run. This allows developers to create continuous delivery pipelines that can be used both locally and in CI, and also run faster due to Dagger's intelligent caching. This integration does not require any installation of Google Cloud tools, such as the Google Cloud CLI or the Google Cloud SDKs.
How it works
The integration depends on the google-cloud-run
Dagger module, which provides various Dagger Functions to create or update a Google Cloud Run service using a container image. These Dagger Functions make it easy to extend existing CI pipelines with additional deployment support.
To use this integration, you can call the Dagger module with the Dagger CLI directly, or you can create your own Dagger Function that uses it. In the latter case, you must:
- Create a new Dagger module in your project
- Install the
google-cloud-run
Dagger module as a dependency - Implement your CI/CD pipeline as one or more Dagger Function(s)
- Test your Dagger Function(s) locally and then transfer them to your CI/CD environment
Prerequisites
- The
google-cloud-run
Dagger module - An active Google Cloud project with the Google Cloud APIs and the Google Cloud Run Admin API enabled
- An active Google Cloud service account for the target project with Editor, Service Account Token Creator, and Cloud Run Admin roles
- A JSON keyfile with credentials for the target service account
- The address of the container image to be deployed
Example
The following example demonstrates how to deploy a containerized application on Google Cloud Run using this integration.
The simplest option is to use the Dagger CLI to directly call the Dagger Function. Remember to update the project
, location
, image
, http-port
and credential
arguments with the correct values and filesystem path.
dagger -m github.com/vvaswani/daggerverse/google-cloud-run@v0.1.5 call create-service \
--project=my-project \
--location=us-central1 \
--image=us-docker.pkg.dev/cloudrun/container/hello \
--http-port=3000 \
--credential=file:my-keyfile.json
If your requirements are more complex - for example, if you already have one or more Dagger Functions to build, test and containerize your application - you can install the google-cloud-run
module as a dependency and call it from your existing Dagger Functions using code.
- Go
- Python
- TypeScript
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@v0.1.5
Update the generated dagger/main.go
file with the following code:
package main
import (
"context"
"dagger/my-module/internal/dagger"
)
type MyModule struct{}
func (m *MyModule) Deploy(ctx context.Context, projectName, serviceLocation, imageAddress string, servicePort int, credential *dagger.Secret) (string, error) {
addr, err := dag.GoogleCloudRun().CreateService(ctx, projectName, serviceLocation, imageAddress, servicePort, credential)
if err != nil {
return "", err
}
return addr, nil
}
Create a new Dagger module:
dagger init --name=my-module --sdk=python --source=./dagger
Install the Google Cloud Run module from the Daggerverse:
dagger install github.com/vvaswani/daggerverse/google-cloud-run@v0.1.5
Update the generated dagger/src/my_module/main.py
file with the following code.
import dagger
from dagger import dag, function, object_type
@object_type
class MyModule:
@function
async def deploy(
self,
project_name: str,
service_location: str,
image_address: str,
service_port: int,
credential: dagger.Secret,
) -> str:
return await dag.google_cloud_run().create_service(
project_name, service_location, image_address, service_port, credential
)
Create a new Dagger module:
dagger init --name=my-module --sdk=typescript --source=./dagger
Install the Google Cloud Run module:
dagger install github.com/vvaswani/daggerverse/google-cloud-run@v0.1.5
Update the generated dagger/src/index.ts
file with the following code:
import { dag, Secret, object, func } from "@dagger.io/dagger"
@object()
class MyModule {
@func()
async deploy(
projectName: string,
serviceLocation: string,
imageAddress: string,
servicePort: number,
credential: Secret,
): Promise<string> {
return await dag
.googleCloudRun()
.createService(
projectName,
serviceLocation,
imageAddress,
servicePort,
credential,
)
}
}
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 Google Cloud sample application to Google Cloud Run. Remember to update the project-name
, service-location
, image-address
and credential
arguments with the correct values and filesystem path.
dagger call deploy \
--project-name=my-project \
--service-location=us-central1 \
--image-address=us-docker.pkg.dev/cloudrun/container/hello \
--service-port=3000 \
--credential=file:my-keyfile.json
The google-cloud-run
Dagger module can also modify an existing service by deploying a new container image to the service URL.
Resources
If you have any questions about additional ways to use Google Cloud Run with Dagger, join our Discord and ask your questions in our help channel.
About Google Cloud Run
Google Cloud Run is a managed service to run containerized applications using Google Cloud.