Google Cloud Run [NEW]  beta 

Cloud Run is a managed compute platform on Google Cloud that allows you to run containers on Google’s infrastructure. With Skaffold, now you are able to configure your dev loop to build, test, sync and use Cloud Run as the deployer for your images.

Deploying applications to Cloud Run

Skaffold can deploy Services and Jobs to Cloud Run. If this deployer is used, all provided manifests must be valid Cloud Run services, using the serving.knative.dev/v1 schema, or valid Cloud Run jobs. See the Cloud Run YAML reference for supported fields.

Environment setup

In order to use this deployer you’ll need to configure some tools first.

The deployer uses the gcloud CLI to perform its tasks, so be sure it is installed in your environment. It will use the application default credentials to deploy. You can configure this to use your user credentials by running:

gcloud auth application-default login

To enable Log streaming and Port forwarding some extra components are needed from gcloud. To install them run the following comand in your terminal:

gcloud components install --quiet \
    alpha \
    beta \
    log-streaming \
    cloud-run-proxy

From the previous command, alpha and log-streaming components are needed for Log streaming, beta and cloud-run-proxy components are needed for Port forwarding.

Features

Cloud Run Services and Jobs deployment

With Skaffold you can deploy Cloud Run Services and Jobs just referencing them from the skaffold.yaml file. The following example ilustrates a project using the Cloud Run deployer:

With the following project folder structure:

resources/
  cloud-run-service.yaml
  cloud-run-job.yaml
skaffold.yaml

cloud-run-service.yaml content:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: cloud-run-service-name # this service will be created in Cloud Run via Skaffold
spec:
  template:
    spec:
      containers:
      - image: gcr.io/cloudrun/hello

cloud-run-job.yaml content:

apiVersion: run.googleapis.com/v1
kind: Job
metadata:
  name: cloud-run-job-name # this job will be created in Cloud Run via Skaffold
  annotations:
    run.googleapis.com/launch-stage: BETA
spec:
  template:
    spec:
      template:
        spec:
          containers:
          - image: us-docker.pkg.dev/cloudrun/container/job

skaffold.yaml content:

apiVersion: skaffold/v4beta2
kind: Config

manifests:
  rawYaml:
    - resources/*

deploy:
  cloudrun:
    projectid: YOUR-GCP-PROJECT
    region: GCP-REGION

Running skaffold run will deploy one Cloud Run service, and one Cloud Run job in the YOUR-GCP-PROJECT project, inside the given GCP-REGION.

Port forwarding

Skaffold will manage automatically the necessary configuration to open the deployed Cloud Run services URLs locally, even if they are private services, using the Cloud Run proxy and Skaffold’s Port Forwarding. To enable this, you will have to either add the --port-forward flag running Skaffold, or add a portForward stanza in your skaffold.yaml file. From the previous example, running skaffold dev --port-forward will result in the following output:

...
Deploying Cloud Run service:
         cloud-run-job-name
Deploying Cloud Run service:
         cloud-run-service-name
Cloud Run Job cloud-run-job-name finished: Job started. 1/2 deployment(s) still pending
cloud-run-service-name: Service starting: Deploying Revision. Waiting on revision cloud-run-service-name-2246v.
cloud-run-service-name: Service starting: Deploying Revision. Waiting on revision cloud-run-service-name-2246v.
Cloud Run Service cloud-run-service-name finished: Service started. 0/2 deployment(s) still pending
Forwarding service projects/<YOUR-GCP-PROJECT>/locations/<GCP-REGION>/services/cloud-run-service-name to local port 8080
...

Here you’ll see the port to use to access the deployed Cloud Run service, in this case you can access it through localhost:8080. If you need to change the local port used, you’ll need to add a portForward stanza:

Using the previous example, changing skaffold.yaml to:

apiVersion: skaffold/v4beta2
kind: Config

manifests:
  rawYaml:
    - resources/*

deploy:
  cloudrun:
    projectid: YOUR-GCP-PROJECT
    region: GCP-REGION

# Added to change local port used
portForward:
  - resourceType: service
    resourceName: cloud-run-service-name
    localPort: 9001

Running skaffold dev --port-forward, will result in:

...
Forwarding service projects/<YOUR-GCP-PROJECT>/locations/<GCP-REGION>/services/cloud-run-service-name to local port 9001
...

Now you will be able to access the deployed service through localhost:9001.

Log streaming

When doing local development, Skaffold will log stream to your console the output from the Cloud Run services deployed. From the previous example, running skaffold dev --port-forward or skaffold run --tail --port-forward in your terminal, you will see the following output:

...
Cloud Run Service cloud-run-service-name finished: Service started. 0/2 deployment(s) still pending
Forwarding service projects/<YOUR-GCP-PROJECT>/locations/<GCP-REGION>/services/cloud-run-service-name to local port 9001
No artifacts found to watch
Press Ctrl+C to exit
Watching for changes...
[cloud-run-service-name] streaming logs from <YOUR-GCP-PROJECT>
...

Now Skaffold is log streaming the output from the service. If you access it through localhost:9001, you’ll see the logs:

...
[cloud-run-service-name] streaming logs from renzo-friction-log-cloud-run
[cloud-run-service-name] 2023-01-27 00:52:22 2023/01/27 00:52:22 Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT
[cloud-run-service-name] 2023-01-27 00:52:22 GET 200 https://cloud-run-service-name-6u2evvstna-uc.a.run.app/

Configuring Cloud Run

To deploy to Cloud Run, use the cloudrun type in the deploy section, together with manifests.rawYaml stanza of skaffold.yaml.

The cloudrun type offers the following options:

Option Description
projectid the GCP Project to use for Cloud Run. If specified, all Services will be deployed to this project. If not specified, each Service will be deployed to the project specified in metadata.namespace of the Cloud Run manifest.
region GCP location to use for the Cloud Run Deploy. Must be one of the regions listed in https://cloud.google.com/run/docs/locations.
hooks describes a set of lifecycle host hooks that are executed before and after the Cloud Run deployer.

Example

The following deploy section instructs Skaffold to deploy the artifacts under manifests.rawYaml to Cloud Run:

manifests:
  rawYaml: # Here should be the list of all the Cloud Run Services and Jobs to deploy
    - cloud-run-service.yaml 

deploy:
  cloudrun:
    projectid: my-gcp-project
    region: us-central1

Cloud Run deployer + Skaffold Local build

With Skaffold you can configure your project to locally build your images and deploy them to Cloud Run. The following example demonstrates how to set up Skaffold for this:

With the following project folder structure:

resources/
  service.yaml
skaffold.yaml
Dockerfile

skaffold.yaml file content:

apiVersion: skaffold/v4beta6
kind: Config

build:
  local:
    push: true # <- We need to push the images so Cloud Run can deploy them

  platforms: ["linux/amd64"] # <- Specific platform supported by Cloud Run

  artifacts:
    - image: my-img # <- Should match the image name in the Cloud Run service.yaml
      docker:
        dockerfile: ./Dockerfile

manifests:
  rawYaml:
    - resources/service.yaml

deploy:
  cloudrun:
    projectid: YOUR-GCP-PROJECT
    region: GCP-REGION

resources/service.yaml file content:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: cloud-run-service
spec:
  template:
    spec:
      containers:
      - image: my-img # <- Same image name from skaffold.yaml file

A simple Dockerfile file:

FROM gcr.io/cloudrun/hello

Running a Skaffold command like skaffold run --default-repo=gcr.io/your-registry will build your local images, push them to the specified registry, and deploy them to Cloud Run. Please notice the following from the previous example:

Build local push option

When you use Skaffold Local build, the push option is set to false by default. However, Cloud Run will need your images published in a registry that it has access to. Therefore, we need to set this to true.

Platform

According to the Cloud Run runtime contract, your images must be compiled for a specific architecture. Skaffold can help us with this by using its Cross/multi-platform build support.

Registry

You’ll need to specify a registry so Skaffold can push your images. For this, we can use the --default-repo flag when running a command to include it in all your local images.