# Verify your application after deploying it

This quickstart shows you how to use Cloud Deploy to verify your deployed
application, as part of the delivery pipeline.

In this quickstart, you'll do the following:

1. Create one GKE cluster or one Cloud Run
   service.

   You can use deploy verification on GKE attached clusters too, but
   this quickstart uses GKE and Cloud Run
   only.
2. Define your Cloud Deploy delivery pipeline and deployment
   target.

   The delivery pipeline includes only one stage and uses only one target.
   You define the configuration for the deploy verification in the `stage`
   stanza.
3. Create a [Skaffold](https://cloud.google.com/skaffold) configuration which is needed by the release, and either a Kubernetes
   manifest or a Cloud Run service definition.

4. Create a release, which automatically deploys to the target.

   After the application is deployed, the
   [verification](https://docs.cloud.google.com/deploy/docs/verify-deployment) is run as a
   [phase](https://docs.cloud.google.com/deploy/docs/deployment-strategies/manage-rollout#phases) of the
   rollout.
5. View the results of the verification in the Cloud Build logs,
   using the Cloud Deploy **Rollout details** page in
   Google Cloud console.

## Before you begin

1. If you already have the CLI installed, make sure you're running the latest version:
2. 

```
gcloud components update
```
3. Make sure the [default
   Compute Engine service account](https://docs.cloud.google.com/deploy/docs/cloud-deploy-service-account#execution_service_account) has sufficient permissions.

   The service account might already have the necessary permissions. These steps
   are included for projects that disable automatic role grants for default service
   accounts.

   <br />

   1. First add the `clouddeploy.jobRunner` role:

      <br />

      ```
      gcloud projects add-iam-policy-binding PROJECT_ID \
          --member=serviceAccount:$(gcloud projects describe PROJECT_ID \
          --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
          --role="roles/clouddeploy.jobRunner"
      ```

      <br />

   2. Add the developer role for your specific runtime.
      - For GKE:

        ```
        gcloud projects add-iam-policy-binding PROJECT_ID \
            --member=serviceAccount:$(gcloud projects describe PROJECT_ID \
            --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
            --role="roles/container.developer"
        ```

        <br />

      - For Cloud Run:

        ```
        gcloud projects add-iam-policy-binding PROJECT_ID \
            --member=serviceAccount:$(gcloud projects describe PROJECT_ID \
            --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
            --role="roles/run.developer"
        ```

        <br />

   3. Add the `iam.serviceAccountUser` role, which includes the `actAs`permission to deploy to the runtime:

      <br />


      ```
      gcloud iam service-accounts add-iam-policy-binding $(gcloud projects describe PROJECT_ID \
          --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
          --member=serviceAccount:$(gcloud projects describe PROJECT_ID \
          --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
          --role="roles/iam.serviceAccountUser" \
          --project=PROJECT_ID
      ```

      <br />

## Create your runtime environment

**If you're deploying to Cloud Run, you can skip this command**.

For GKE, create one cluster: `deploy-verification-cluster`,
with default settings. The cluster's Kubernetes API endpoint must be
network-reachable from the public internet. GKE clusters
are externally accessible by default.


```
gcloud container clusters create-auto deploy-verification-cluster \
                 --project=PROJECT_ID \
                 --region=us-central1
```

<br />

## Create your delivery pipeline and targets

You can define your pipeline and targets in one file or in separate files. In
this quickstart, you create a single file.

1. Open a terminal window.

2. Create a new directory and navigate into it.

   ### GKE

       mkdir deploy-verification-gke-quickstart
       cd deploy-verification-gke-quickstart

   ### Cloud Run

       mkdir deploy-verification-run-quickstart
       cd deploy-verification-run-quickstart

3. Create your delivery pipeline and target definition:

   ### GKE

   In the `deploy-verification-gke-quickstart` directory, create a new file:
   `clouddeploy.yaml`, with the following contents:

       apiVersion: deploy.cloud.google.com/v1
       kind: DeliveryPipeline
       metadata:
         name: deploy-verification-demo-app-gke-1
       description: main application pipeline
       serialPipeline:
         stages:
         - targetId: verify-staging
           profiles: []
           strategy:
             standard:
               verify:
                 tasks:
                 - type: container
                   image: ubuntu
                   command: ["/bin/sh"]
                   args: ["-c", 'echo "Your test or tests would go here."' ]
       ---

       apiVersion: deploy.cloud.google.com/v1
       kind: Target
       metadata:
         name: verify-staging
       description: verification staging cluster
       gke:
         cluster: projects/PROJECT_ID/locations/us-central1/clusters/deploy-verification-cluster

   ### Cloud Run

   In the `deploy-verification-run-quickstart` directory, create a new file:
   `clouddeploy.yaml`, with the following contents:

       apiVersion: deploy.cloud.google.com/v1
       kind: DeliveryPipeline
       metadata:
         name: deploy-verification-demo-app-run-1
       description: main application pipeline
       serialPipeline:
         stages:
         - targetId: verify-staging
           profiles: []
           strategy:
             standard:
               verify:
                 tasks:
                 - type: container
                   image: ubuntu
                   command: ["/bin/sh"]
                   args: ["-c", 'echo "Your test or tests would go here."' ]
       ---

       apiVersion: deploy.cloud.google.com/v1
       kind: Target
       metadata:
         name: verify-staging
       description: staging Run service
       run:
         location: projects/PROJECT_ID/locations/us-central1

   > [!NOTE]
   > **Note:** In this file, the target is included with the delivery pipeline, but you can define targets in a separate file or multiple separate files.

   This file includes the `verify` stanza within the delivery pipeline's `strategy` definition.
   It defines the container and command
   to run after deploying the application. In this case, we're deploying an
   Ubuntu container and running a simple `echo` command. You can view the
   results of this command in the logs. In a more realistic scenario, you would
   run tests against your deployed application.
4. Register your pipeline and targets with the Cloud Deploy service:


   ```
   gcloud deploy apply --file=clouddeploy.yaml --region=us-central1 --project=PROJECT_ID
   ```

   <br />

   You now have a delivery pipeline, with one target, ready to deploy your
   application and verify that deployment.
5. Confirm your pipeline and targets:

   In the Google Cloud console, navigate to the Cloud Deploy
   **Delivery pipelines** page to view of list of your available delivery
   pipelines.

   [Open the Delivery pipelines page](https://console.cloud.google.com/deploy/delivery-pipelines)

   The delivery pipeline you just created is shown, with one target listed in
   the **Targets** column.

   ![delivery pipeline pipeline page in Google Cloud console, showing your pipeline](https://docs.cloud.google.com/static/deploy/images/pipelines-page-verify.png)

## Create your Skaffold configuration file and application manifest

Cloud Deploy uses [Skaffold](https://docs.cloud.google.com/deploy/docs/using-skaffold) to identify
the manifest to be used to deploy the sample app to your
[target](https://docs.cloud.google.com/deploy/docs/terminology#target).

1. Create a file named `skaffold.yaml` with the following contents:

   ### GKE

       apiVersion: skaffold/v4beta7
       kind: Config
       manifests:
         rawYaml:
         - k8s-pod.yaml
       deploy:
         kubectl: {}

   ### Cloud Run

       apiVersion: skaffold/v4beta7
       kind: Config
       manifests:
         rawYaml:
         - service.yaml
       deploy:
         cloudrun: {}

   See the [`skaffold.yaml` reference](https://skaffold.dev/docs/references/yaml/)
   for more information about this configuration file.
2. Create the definition for your application---a service definition for
   Cloud Run or a Kubernetes manifest for GKE.

   ### GKE

   Create a file named `k8s-pod.yaml`, with the following contents:

       apiVersion: v1
       kind: Pod
       metadata:
         name: my-verification-pod
       spec:
         containers:
         - name: nginx
           image: my-app-image

   This file is a basic Kubernetes
   [manifest](https://kubernetes.io/docs/reference/glossary/?fundamental=true#term-manifest), which is applied to the cluster to
   deploy the application. The container image to deploy is set here as a
   placeholder, `my-app-image`, which is replaced with the specific image
   when you [create the release](https://docs.cloud.google.com/deploy/docs/deploy-app-verification#create_a_release).

   ### Cloud Run

   Create a file named `service.yaml`, with the following contents:

       apiVersion: serving.knative.dev/v1
       kind: Service
       metadata:
         name: my-verification-run-service
       spec:
         template:
           spec:
             containers:
             - image: my-app-image

   This file is a basic Cloud Run service definition,
   which is used to deploy the application. The container image to deploy
   is set here as a placeholder, `my-app-image`, which is replaced with the
   specific image when you [create the release](https://docs.cloud.google.com/deploy/docs/deploy-app-verification#create_a_release).

## Create a release

A release is the central Cloud Deploy resource representing the
changes being deployed. The delivery pipeline defines the lifecycle of that
release. See
[Cloud Deploy service architecture](https://docs.cloud.google.com/deploy/docs/architecture#how_they_fit_together_to_deliver_your_release)
for details about that lifecycle.

### GKE

Run the following command from the `deploy-verification-gke-quickstart`
directory to create a `release` resource that represents the container
image to deploy:

     gcloud deploy releases create test-release-001 \
       --project=PROJECT_ID \
       --region=us-central1 \
       --delivery-pipeline=deploy-verification-demo-app-gke-1 \
       --images=my-app-image=gcr.io/google-containers/nginx@sha256:f49a843c290594dcf4d193535d1f4ba8af7d56cea2cf79d1e9554f077f1e7aaa

Notice the
[`--images=` flag](https://docs.cloud.google.com/sdk/gcloud/reference/deploy/releases/create#--images),
which you use to replace the placeholder (`my-app-image`) in the
[manifest](https://docs.cloud.google.com/deploy/docs/deploy-app-verification#prepare_your_skaffold_configuration_and_application_manifest)
with the specific, SHA-qualified image. Google recommends that you
templatize your manifests this way, and that you use SHA-qualified image
names at release creation.

### Cloud Run

Run the following command from the `deploy-verification-run-quickstart`
directory to create a `release` resource that represents the container
image to deploy:

     gcloud deploy releases create test-release-001 \
       --project=PROJECT_ID \
       --region=us-central1 \
       --delivery-pipeline=deploy-verification-demo-app-run-1 \
       --images=my-app-image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a

Notice the
[`--images=` flag](https://docs.cloud.google.com/sdk/gcloud/reference/deploy/releases/create#--images),
which you use to replace the placeholder (`my-app-image`) in the
[service definition](https://docs.cloud.google.com/deploy/docs/deploy-app-verification#prepare_your_skaffold_configuration_and_application_manifest)
with the specific, SHA-qualified image. Google recommends that you
templatize your service definitions this way, and that you use
SHA-qualified image names at release creation.

As with all releases (unless they include `--disable-initial-rollout`),
Cloud Deploy automatically creates a
[rollout](https://docs.cloud.google.com/deploy/docs/terminology#rollout) resource too. The application is
automatically deployed into the one target configured for this delivery
pipeline.

The verification job we specified in our delivery pipeline runs as part of this
rollout, after the application is deployed.

## View the results in Google Cloud console

After a few minutes, your release is deployed into your target runtime.

The verification job that we configured (for example purposes) prints a string
to the Cloud Build log. We can view that log to confirm that the
verification worked as expected.

1. In the Google Cloud console, navigate to the Cloud Deploy
   **Delivery pipelines** page to view your delivery pipeline
   ("deploy-verification-demo-app-gke-1" or "deploy-verification-demo-app-run-1").

   <br />

   [Open the Delivery pipelines page](https://console.cloud.google.com/deploy/delivery-pipelines)

   <br />

2. Click the name of your delivery pipeline
   ("deploy-verification-demo-app-gke-1" or "deploy-verification-demo-app-run-1").

   The pipeline visualization shows the app's deployment status. Because there's
   only one stage in the pipeline, the visualization shows only one node.

   ![Delivery pipeline visualization showing success](https://docs.cloud.google.com/static/deploy/images/pipeline_viz_verify.png)

   And your release is listed on the **Releases** tab under **Delivery pipeline
   details**.
3. Click the **Rollouts** tab, under **Delivery pipeline details**.

4. Click the rollout name to view the rollout details.

   ![rollouts in Google Cloud console](https://docs.cloud.google.com/static/deploy/images/verification-rollout-details.png)

   **Verify** is listed as a job.
5. Click **Verify**

   The job run log is displayed.
6. Scroll down in the list of log entries to find `verification-test`, and click
   it.

   ![Log entry showing output from the Verify job](https://docs.cloud.google.com/static/deploy/images/log-verify-echo.png)

   Notice the `textPayload`. That string is what was configured in the
   `Verify` stanza in your
   [Delivery Pipeline](https://docs.cloud.google.com/deploy/docs/deploy-app-verification#create_your_delivery_pipeline_and_targets).

## Clean up


To avoid incurring charges to your Google Cloud account for
the resources used on this page, follow these steps.

1. Delete the GKE cluster or Cloud Run service:

   ### GKE

       gcloud container clusters delete deploy-verification-cluster --region=us-central1 --project=PROJECT_ID

   ### Cloud Run

       gcloud run services delete my-verification-run-service --region=us-central1 --project=PROJECT_ID

2. Delete the delivery pipeline, target, release, and rollout:

       gcloud deploy delete --file=clouddeploy.yaml --force --region=us-central1 --project=PROJECT_ID

3. Delete the Cloud Storage buckets that Cloud Deploy created.

   One ends with `_clouddeploy`, and the other is `[region].deploy-artifacts.[project].appspot.com`.

   [Open the Cloud Storage browser page](https://console.cloud.google.com/storage/browser)

That's it, you completed this quickstart!

## What's next

- [Learn more about Cloud Deploy](https://docs.cloud.google.com/deploy/docs/overview).

- [Learn more about deploy verification](https://docs.cloud.google.com/deploy/docs/verify-deployment).