Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get or generate deploy URL for Google Cloud Run services

How to get the URL of deployed service programmatically in CI environments? The URL does get's logged after successful deploy but what if I want to extract and use the URL programmatically, as part of post deploy needs, e.g. posting the URL for acceptance test.

like image 308
fusionstrings Avatar asked Dec 20 '19 09:12

fusionstrings


People also ask

What is the URL for the Google Cloud Platform?

Access to public objects All requests to the storage.cloud.google.com URI require authentication.

What is the URL to be used for deploy to Docker cloud function?

Enable it by visiting https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=<project-number> then retry.


3 Answers

Simply use the flag: --format='value(status.url)' with gcloud run services describe

Here is the entire command:

$ gcloud run services describe SERVICE --platform managed --region REGION --format 'value(status.url)'
like image 162
Steren Avatar answered Oct 11 '22 15:10

Steren


Extending Steren's answer:

With these Bash commands you can get url and save it in Secrets Manager:

First create empty Secret:

gcloud secrets create "CLOUDRUN_URL" --project $PROJECT_ID --replication-policy=automatic

Then:

gcloud run services describe $APP_NAME --platform managed --region $CLOUD_REGION --format 'value(status.url)' --project $PROJECT_ID | gcloud secrets versions add "CLOUDRUN_URL" --data-file=- --project $PROJECT_ID

or version with added "/some/address"

CLOUDRUN_URL=$(gcloud run services describe $APP_NAME --platform managed --region $CLOUD_REGION --format 'value(status.url)' --project $PROJECT_ID)   # capture first string.
echo "$CLOUDRUN_URL/some/address/" | gcloud secrets versions add "CLOUDRUN_URL" --data-file=- --project $PROJECT_ID

And then you can load it as needed from Secrets Manager:

export CLOUDRUN_URL=$(gcloud secrets versions access latest --secret="CLOUDRUN_URL" --project $PROJECT_ID )

like image 31
Karol Zlot Avatar answered Oct 11 '22 13:10

Karol Zlot


There are several ways to get the desired information:

  1. You can use the namespaces.services.get method from Cloud Run's API and a curl command. Notice that it will require an Authentication Header and an OAuth scope.
curl -i https://[REGION]-run.googleapis.com/apis/serving.knative.dev/v1/namespaces/[PROJECT_NAME]/services/[CLOUD_RUN_SERVICE_NAME] -H "Authorization: Bearer [YOUR-BEARER-TOKEN]" | tail -n +13 | jq -r ".status.url"
  1. You can use the gcloud run services list command in one of your build steps to get the desired value. For example if your service is fully managed you can use the following command to get the Cloud Run service that was last updated.:
gcloud run services list --platform managed | awk 'NR==2 {print $4}'
  1. Build a script using the Goolge API Client libraries (e.g. the Cloud Run Google API Client for Python).
like image 5
Daniel Ocando Avatar answered Oct 11 '22 14:10

Daniel Ocando