25

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.

fusionstrings
  • 1,025
  • 2
  • 13
  • 23

3 Answers3

24

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)'
Steren
  • 7,311
  • 3
  • 31
  • 51
7

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).
Daniel Ocando
  • 3,554
  • 2
  • 11
  • 19
7

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 )

Karol Zlot
  • 2,887
  • 2
  • 20
  • 37