Search code examples
google-cloud-run

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.


Solution

  • 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).