|
| 1 | +--- |
| 2 | +title: "Report Cloud Run environments to Kosli" |
| 3 | +description: "Learn how to report running artifacts from a Google Cloud Run project and region to Kosli — using the CLI for a quick test or a scheduled Cloud Run Job for production." |
| 4 | +--- |
| 5 | + |
| 6 | +By the end of this tutorial, you will have reported a snapshot of your Cloud Run environment to Kosli, making its running services and jobs visible and trackable. |
| 7 | + |
| 8 | +`kosli snapshot cloud-run` covers a specific set of GCP deploy methods. See the [`kosli snapshot cloud-run`](/client_reference/kosli_snapshot_cloud-run) reference for the current list of what's supported. |
| 9 | + |
| 10 | +There are two ways to do this: |
| 11 | + |
| 12 | +- **Kosli CLI** — quick to run, suitable for testing only |
| 13 | +- **<Tooltip tip="A Cloud Run Job that runs the Kosli CLI image on a Cloud Scheduler cron, reporting the project's Cloud Run services and jobs to Kosli automatically.">Scheduled Cloud Run Job</Tooltip>** — runs the reporter inside GCP on a schedule for continuous, production-grade reporting |
| 14 | + |
| 15 | +Follow the section that matches your needs. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +* Have access to a Google Cloud project and region with Cloud Run resources. |
| 20 | +* [Create a Cloud Run Kosli environment](/getting_started/environments#create-an-environment) named `cloud-run-tutorial`. |
| 21 | +* [Get a Kosli API token](/getting_started/service-accounts). |
| 22 | + |
| 23 | +## Report using Kosli CLI |
| 24 | + |
| 25 | +This approach is suitable for testing only. |
| 26 | + |
| 27 | +[Install Kosli CLI](/getting_started/install) if you have not done so, then authenticate to GCP with Application Default Credentials: |
| 28 | + |
| 29 | +```shell |
| 30 | +gcloud auth application-default login |
| 31 | +``` |
| 32 | + |
| 33 | +Run the snapshot command: |
| 34 | + |
| 35 | +```shell |
| 36 | +kosli snapshot cloud-run cloud-run-tutorial \ |
| 37 | + --project <your-gcp-project> \ |
| 38 | + --region <your-gcp-region> \ |
| 39 | + --resolve-names \ |
| 40 | + --api-token <your-api-token-here> \ |
| 41 | + --org <your-kosli-org-name> |
| 42 | +``` |
| 43 | + |
| 44 | +`--resolve-names` makes Cloud Run services display their image tags (for example the commit SHA) instead of bare digests by reverse-resolving the deployed digest against Artifact Registry. The forward digest lookup for tag-pinned Jobs runs automatically whether you pass the flag or not. |
| 45 | + |
| 46 | +See [`kosli snapshot cloud-run`](/client_reference/kosli_snapshot_cloud-run) for the full flag reference. |
| 47 | + |
| 48 | +## Report using a scheduled Cloud Run Job |
| 49 | + |
| 50 | +For production, run the reporter inside GCP as a Cloud Run Job triggered by Cloud Scheduler. |
| 51 | + |
| 52 | +<Steps> |
| 53 | +<Step title="Create a service account for the reporter"> |
| 54 | + |
| 55 | +```shell |
| 56 | +gcloud iam service-accounts create kosli-reporter \ |
| 57 | + --display-name="Kosli reporter" \ |
| 58 | + --project=<your-gcp-project> |
| 59 | +``` |
| 60 | + |
| 61 | +</Step> |
| 62 | + |
| 63 | +<Step title="Grant the reporter project-level access to Cloud Run"> |
| 64 | + |
| 65 | +`roles/run.viewer` is the minimum needed to list services and jobs in the project. |
| 66 | + |
| 67 | +```shell |
| 68 | +gcloud projects add-iam-policy-binding <your-gcp-project> \ |
| 69 | + --member="serviceAccount:kosli-reporter@<your-gcp-project>.iam.gserviceaccount.com" \ |
| 70 | + --role="roles/run.viewer" |
| 71 | +``` |
| 72 | + |
| 73 | +</Step> |
| 74 | + |
| 75 | +<Step title="Store the Kosli API token in Secret Manager"> |
| 76 | + |
| 77 | +Create a secret and add your token as the first version: |
| 78 | + |
| 79 | +```shell |
| 80 | +gcloud secrets create kosli-api-token \ |
| 81 | + --replication-policy=automatic \ |
| 82 | + --project=<your-gcp-project> |
| 83 | + |
| 84 | +printf "<your-api-token-here>" | gcloud secrets versions add kosli-api-token \ |
| 85 | + --data-file=- \ |
| 86 | + --project=<your-gcp-project> |
| 87 | +``` |
| 88 | + |
| 89 | +Grant the reporter service account read access to that specific secret: |
| 90 | + |
| 91 | +```shell |
| 92 | +gcloud secrets add-iam-policy-binding kosli-api-token \ |
| 93 | + --member="serviceAccount:kosli-reporter@<your-gcp-project>.iam.gserviceaccount.com" \ |
| 94 | + --role="roles/secretmanager.secretAccessor" \ |
| 95 | + --project=<your-gcp-project> |
| 96 | +``` |
| 97 | + |
| 98 | +</Step> |
| 99 | + |
| 100 | +<Step title="Grant Artifact Registry read access"> |
| 101 | + |
| 102 | +Grant `roles/artifactregistry.reader` to the reporter on each Artifact Registry repository that holds your application images. This is what lets the reporter resolve digests and tags so artifact names are useful on Kosli. |
| 103 | + |
| 104 | +```shell |
| 105 | +gcloud artifacts repositories add-iam-policy-binding <your-repo> \ |
| 106 | + --location=<your-gcp-region> \ |
| 107 | + --member="serviceAccount:kosli-reporter@<your-gcp-project>.iam.gserviceaccount.com" \ |
| 108 | + --role="roles/artifactregistry.reader" \ |
| 109 | + --project=<your-gcp-project> |
| 110 | +``` |
| 111 | + |
| 112 | +Repeat the command for every Artifact Registry repository that holds images deployed to Cloud Run in this project. |
| 113 | + |
| 114 | +<Note> |
| 115 | +If you deploy any Cloud Functions 2nd-gen functions in this project, also grant the same role on the Google-managed `gcf-artifacts` repository in the same region. 2nd-gen functions store their backing images there, and the reporter needs read access to resolve them. |
| 116 | +</Note> |
| 117 | + |
| 118 | +</Step> |
| 119 | + |
| 120 | +<Step title="Deploy the reporter as a Cloud Run Job"> |
| 121 | + |
| 122 | +```shell |
| 123 | +gcloud run jobs deploy kosli-reporter \ |
| 124 | + --image=ghcr.io/kosli-dev/cli:latest \ |
| 125 | + --region=<your-gcp-region> \ |
| 126 | + --project=<your-gcp-project> \ |
| 127 | + --service-account=kosli-reporter@<your-gcp-project>.iam.gserviceaccount.com \ |
| 128 | + --set-env-vars=KOSLI_ORG=<your-kosli-org-name>,KOSLI_HOST=https://app.kosli.com \ |
| 129 | + --set-secrets=KOSLI_API_TOKEN=kosli-api-token:latest \ |
| 130 | + --args=snapshot,cloud-run,cloud-run-tutorial,--project,<your-gcp-project>,--region,<your-gcp-region>,--resolve-names |
| 131 | +``` |
| 132 | + |
| 133 | +<Tip> |
| 134 | +Pin the CLI image to a specific version (for example `ghcr.io/kosli-dev/cli:v2.18.0`) so the reporter behaviour does not change unexpectedly when a new release is published. |
| 135 | +</Tip> |
| 136 | + |
| 137 | +<Note> |
| 138 | +Cloud Run Jobs are created with `deletionProtection=true` by default. You will need to disable it (`gcloud run jobs update kosli-reporter --no-deletion-protection --region=<your-gcp-region>`) before you can delete or replace the Job later. |
| 139 | +</Note> |
| 140 | + |
| 141 | +</Step> |
| 142 | + |
| 143 | +<Step title="Schedule the reporter with Cloud Scheduler"> |
| 144 | + |
| 145 | +Create a Cloud Scheduler job that triggers the Cloud Run Job every five minutes, and grant its service account permission to invoke the Job: |
| 146 | + |
| 147 | +```shell |
| 148 | +gcloud scheduler jobs create http kosli-reporter-schedule \ |
| 149 | + --location=<your-gcp-region> \ |
| 150 | + --schedule="*/5 * * * *" \ |
| 151 | + --uri="https://run.googleapis.com/v2/projects/<your-gcp-project>/locations/<your-gcp-region>/jobs/kosli-reporter:run" \ |
| 152 | + --http-method=POST \ |
| 153 | + --oauth-service-account-email=kosli-reporter@<your-gcp-project>.iam.gserviceaccount.com \ |
| 154 | + --project=<your-gcp-project> |
| 155 | + |
| 156 | +gcloud run jobs add-iam-policy-binding kosli-reporter \ |
| 157 | + --region=<your-gcp-region> \ |
| 158 | + --member="serviceAccount:kosli-reporter@<your-gcp-project>.iam.gserviceaccount.com" \ |
| 159 | + --role="roles/run.invoker" \ |
| 160 | + --project=<your-gcp-project> |
| 161 | +``` |
| 162 | + |
| 163 | +</Step> |
| 164 | + |
| 165 | +<Step title="Verify the reporter"> |
| 166 | + |
| 167 | +In the GCP console, open **Cloud Run** -> **Jobs** -> **kosli-reporter** and check the execution logs for a recent successful run. Then confirm that a fresh snapshot has appeared for the `cloud-run-tutorial` environment in the Kosli UI. |
| 168 | + |
| 169 | +</Step> |
| 170 | +</Steps> |
| 171 | + |
| 172 | +## What you've accomplished |
| 173 | + |
| 174 | +You have reported a snapshot of your Cloud Run environment to Kosli. Kosli now tracks the running services and jobs in that environment and will record changes as they happen. |
| 175 | + |
| 176 | +From here you can: |
| 177 | +* Query your environment with [`kosli list snapshots`](/client_reference/kosli_list_snapshots) and [`kosli get snapshot`](/client_reference/kosli_get_snapshot) |
| 178 | +* [Compare snapshots to see what changed](/client_reference/kosli_diff_snapshots) |
| 179 | +* Trace a running artifact back to its git commit with the [From commit to production](/tutorials/following_a_git_commit_to_runtime_environments) tutorial |
0 commit comments