github.com/GoogleContainerTools/skaffold/v2@v2.13.2/docs-v2/content/en/docs/deployers/cloudrun.md (about)

     1  ---
     2  title: "Google Cloud Run [NEW]"
     3  linkTitle: "Google Cloud Run [NEW]"
     4  weight: 60
     5  featureId: deploy.cloudrun
     6  aliases: [/docs/pipeline-stages/deployers/cloudrun]
     7  ---
     8  
     9  {{< alert title="Note" >}}
    10  This feature is currently experimental and subject to change. Not all Skaffold features are supported, for example `debug` is currently not supported in Cloud Run (but is on our roadmap).
    11  {{< /alert >}}
    12  
    13  [Cloud Run](https://cloud.google.com/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.
    14  
    15  
    16  ## Deploying applications to Cloud Run
    17  Skaffold can deploy [Services](https://cloud.google.com/run/docs/reference/rest/v1/namespaces.services#resource:-service) and [Jobs](https://cloud.google.com/run/docs/reference/rest/v1/namespaces.jobs#resource:-job) 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.
    18  See the [Cloud Run YAML reference](https://cloud.google.com/run/docs/reference/yaml/v1) for supported fields.
    19  
    20  ### Environment setup
    21  In order to use this deployer you'll need to configure some tools first.
    22  
    23  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](https://cloud.google.com/docs/authentication/production#automatically) to deploy.  You can configure this to use your user credentials by running:
    24  ```bash
    25  gcloud auth application-default login
    26  ```
    27  
    28  To enable [Log streaming]({{< relref "#log-streaming" >}}) and [Port forwarding]({{< relref "#port-forwarding" >}}) some extra components are needed from `gcloud`. To install them run the following comand in your terminal:
    29  ```bash
    30  gcloud components install --quiet \
    31      alpha \
    32      beta \
    33      log-streaming \
    34      cloud-run-proxy
    35  ```
    36  
    37  From the previous command, `alpha` and `log-streaming` components are needed for [Log streaming]({{< relref "#log-streaming" >}}), `beta` and `cloud-run-proxy` components are needed for [Port forwarding]({{< relref "#port-forwarding" >}}).
    38  
    39  ## Features
    40  
    41  ### Cloud Run Services and Jobs deployment
    42  With Skaffold you can deploy Cloud Run [Services](https://cloud.google.com/run/docs/overview/what-is-cloud-run#services) and [Jobs](https://cloud.google.com/run/docs/overview/what-is-cloud-run#jobs) just referencing them from the `skaffold.yaml` file. The following example ilustrates a project using the Cloud Run deployer:
    43  
    44  With the following project folder structure:
    45  ```yaml
    46  resources/
    47    cloud-run-service.yaml
    48    cloud-run-job.yaml
    49  skaffold.yaml
    50  ```
    51  
    52  `cloud-run-service.yaml` content:
    53  ```yaml
    54  apiVersion: serving.knative.dev/v1
    55  kind: Service
    56  metadata:
    57    name: cloud-run-service-name # this service will be created in Cloud Run via Skaffold
    58  spec:
    59    template:
    60      spec:
    61        containers:
    62        - image: gcr.io/cloudrun/hello
    63  ```
    64  
    65  `cloud-run-job.yaml` content:
    66  ```yaml
    67  apiVersion: run.googleapis.com/v1
    68  kind: Job
    69  metadata:
    70    name: cloud-run-job-name # this job will be created in Cloud Run via Skaffold
    71    annotations:
    72      run.googleapis.com/launch-stage: BETA
    73  spec:
    74    template:
    75      spec:
    76        template:
    77          spec:
    78            containers:
    79            - image: us-docker.pkg.dev/cloudrun/container/job
    80  ```
    81  
    82  `skaffold.yaml` content:
    83  {{% readfile file="samples/deployers/cloud-run/simple-service-job-deployment.yaml" %}}
    84  
    85  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`.
    86  
    87  {{< alert title="Note" >}}
    88  The previous example will deploy a Cloud Run job, however, it will not trigger an execution for that job. To read more about jobs execution you can check the [Cloud Run docs](https://cloud.google.com/run/docs/execute/jobs).
    89  {{< /alert >}}
    90  
    91  ### Port forwarding {#port-forwarding}
    92  
    93  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](https://cloud.google.com/sdk/gcloud/reference/beta/run/services/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:
    94  
    95  ```
    96  ...
    97  Deploying Cloud Run service:
    98           cloud-run-job-name
    99  Deploying Cloud Run service:
   100           cloud-run-service-name
   101  Cloud Run Job cloud-run-job-name finished: Job started. 1/2 deployment(s) still pending
   102  cloud-run-service-name: Service starting: Deploying Revision. Waiting on revision cloud-run-service-name-2246v.
   103  cloud-run-service-name: Service starting: Deploying Revision. Waiting on revision cloud-run-service-name-2246v.
   104  Cloud Run Service cloud-run-service-name finished: Service started. 0/2 deployment(s) still pending
   105  Forwarding service projects/<YOUR-GCP-PROJECT>/locations/<GCP-REGION>/services/cloud-run-service-name to local port 8080
   106  ...
   107  ```
   108  
   109  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:
   110  
   111  Using the previous example, changing `skaffold.yaml` to:
   112  {{% readfile file="samples/deployers/cloud-run/service-port-forward.yaml" %}}
   113  
   114  Running `skaffold dev --port-forward`, will result in:
   115  
   116  ```
   117  ...
   118  Forwarding service projects/<YOUR-GCP-PROJECT>/locations/<GCP-REGION>/services/cloud-run-service-name to local port 9001
   119  ...
   120  ```
   121  
   122  Now you will be able to access the deployed service through `localhost:9001`.
   123  
   124  
   125  ### Log streaming {#log-streaming}
   126  
   127  When doing [local development]({{< relref "docs/workflows/dev">}}), 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:
   128  
   129  ```
   130  ...
   131  Cloud Run Service cloud-run-service-name finished: Service started. 0/2 deployment(s) still pending
   132  Forwarding service projects/<YOUR-GCP-PROJECT>/locations/<GCP-REGION>/services/cloud-run-service-name to local port 9001
   133  No artifacts found to watch
   134  Press Ctrl+C to exit
   135  Watching for changes...
   136  [cloud-run-service-name] streaming logs from <YOUR-GCP-PROJECT>
   137  ...
   138  ```
   139  
   140  Now Skaffold is log streaming the output from the service. If you access it through `localhost:9001`, you'll see the logs:
   141  
   142  ```
   143  ...
   144  [cloud-run-service-name] streaming logs from renzo-friction-log-cloud-run
   145  [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
   146  [cloud-run-service-name] 2023-01-27 00:52:22 GET 200 https://cloud-run-service-name-6u2evvstna-uc.a.run.app/
   147  ```
   148  
   149  ## Configuring Cloud Run
   150  
   151  To deploy to Cloud Run, use the `cloudrun` type in the `deploy` section, together with `manifests.rawYaml` stanza of `skaffold.yaml`.
   152  
   153  The `cloudrun` type offers the following options:
   154  
   155  {{< schema root="CloudRunDeploy" >}}
   156  
   157  
   158  ### Example
   159  
   160  The following `deploy` section instructs Skaffold to deploy the artifacts under `manifests.rawYaml` to Cloud Run:
   161  
   162  {{% readfile file="samples/deployers/cloud-run/cloud-run.yaml" %}}
   163  
   164  ## Cloud Run deployer + Skaffold Local build
   165  
   166  With Skaffold you can configure your project to [locally build]({{< relref "docs/builders/build-environments/local" >}}) your images and deploy them to Cloud Run. The following example demonstrates how to set up Skaffold for this:
   167  
   168  With the following project folder structure:
   169  ```yaml
   170  resources/
   171    service.yaml
   172  skaffold.yaml
   173  Dockerfile
   174  ```
   175  
   176  `skaffold.yaml` file content:
   177  {{% readfile file="samples/deployers/cloud-run/local-build-image.yaml" %}}
   178  
   179  
   180  `resources/service.yaml` file content:
   181  ```yaml
   182  apiVersion: serving.knative.dev/v1
   183  kind: Service
   184  metadata:
   185    name: cloud-run-service
   186  spec:
   187    template:
   188      spec:
   189        containers:
   190        - image: my-img # <- Same image name from skaffold.yaml file
   191  ```
   192  
   193  A simple `Dockerfile` file:
   194  ```docker
   195  FROM gcr.io/cloudrun/hello
   196  ```
   197  
   198  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:
   199  
   200  ### Build local push option
   201  When you use [Skaffold Local build]({{< relref "docs/builders/build-environments/local#avoiding-pushes" >}}), 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`.
   202  
   203  ### Platform
   204  According to the [Cloud Run runtime contract](https://cloud.google.com/run/docs/container-contract#languages), your images must be compiled for a specific architecture. Skaffold can help us with this by using its [Cross/multi-platform build support]({{< relref "docs/builders/cross-platform" >}}).
   205  
   206  ### Registry
   207  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.