I'm trying to find the name of the latest revision of a gcloud run service, so that I can change its assigned traffic when running a Jenkins pipeline. I've tried the gcloud run revisions list with the --limit
, --sort-by
, --format
flags, but I'm not getting what I expected. When limiting to only one result, the second to last revision comes up instead of the latest one. Am I doing anything wrong?
The following are the gcloud
commands I've ran coupled with their output.
>gcloud run revisions list --service service-foo --platform managed --region europe-west4 --limit 2 --sort-by ~creationTimestamp --format="value(name,creationTimestamp)"
service-foo-00302-luq 2021-08-27T11:13:39.883161Z
service-foo-00300-san 2021-08-26T15:45:33.351646Z
>gcloud run revisions list --service service-foo --platform managed --region europe-west4 --limit 1 --sort-by ~creationTimestamp --format="value(name,creationTimestamp)"
service-foo-00300-san 2021-08-26T15:45:33.351646Z
>gcloud run revisions list --service service-foo --platform managed --region europe-west4 --limit 1 --sort-by creationTimestamp --format="value(name,creationTimestamp)"
service-foo-00300-san 2021-08-26T15:45:33.351646Z
There does seem to currently be a bug for the revisions
subgroup's list
command using both the Google Cloud Platform (GCP) SDK (gcloud
):
gcloud run revisions list \
--format="value(metadata.name)" \
--platform=managed \
--region=$CLOUD_RUN_SERVICE_REGION \
--service=$CLOUD_RUN_SERVICE_NAME \
--sort-by="~metadata.creationTimestamp"
#=>
$CLOUD_RUN_LATEST_REVISION
$CLOUD_RUN_PREVIOUS_REVISION
. . .
when using the --limit
flag:
gcloud run revisions list \
--format="value(metadata.name)" \
--limit=1 \
--platform=managed \
--region=$CLOUD_RUN_SERVICE_REGION \
--service=$CLOUD_RUN_SERVICE_NAME \
--sort-by="~metadata.creationTimestamp"
#=>
$CLOUD_RUN_PREVIOUS_REVISION
and the GCP REST API with the limit
query param.:
curl \
--location \
--request GET "https://$CLOUD_RUN_SERVICE_REGION-run.googleapis.com/apis/serving.knative.dev/v1/namespaces/$GCP_PROJECT_NAME/revisions?limit=1" \
--header "Authorization: Bearer $(gcloud auth print-access-token)"
#=>
{
. . .
"items": [
{
. . .
"metadata": {
"name": "$CLOUD_RUN_PREVIOUS_REVISION",
. . .
},
. . .
},
. . .
]
}
with the latest gcloud
version:
gcloud version
#=>
Google Cloud SDK 357.0.0
. . .
However, there is a way to get both the latest created and ready revisions of a Cloud Run service using the services
subgroup instead of the revisions
subgroup:
gcloud run services describe $GCLOUD_RUN_SERVICE_NAME \
--format="value(status.latestCreatedRevisionName)" \
--platform=managed \
--region=$CLOUD_RUN_SERVICE_REGION
#=>
$CLOUD_RUN_LATEST_REVISION
and:
gcloud run services describe $GCLOUD_RUN_SERVICE_NAME \
--format="value(status.latestReadyRevisionName)" \
--platform=managed \
--region=$CLOUD_RUN_SERVICE_REGION
#=>
$CLOUD_RUN_LATEST_REVISION
You can read more about the describe
command for the services
subgroup here.