Cloudbuild based solution for performing A/B testing on Cloud Run using traffic splitting (50/50 between last and last but one)
Cloud Build revisions are updated with this procedure:
-
Deploy revision 1: 100% traffic on revision 1
-
Deploy revision 2: 50% traffic on revision 2 and 50% traffic on revision 1
-
Deploy revision N: 50% traffic on revision N and 50% traffic on revision N - 1
PROJECT_ID=<PROJECT_ID>
REGION=europe-west1
REPO=hello-go
gcloud config set project $PROJECT_ID
gcloud artifacts repositories create $REPO \
--repository-format=docker \
--location=$REGION
gcloud builds submit ./src \
--region $REGION \
--config ./cloudbuild.yaml \
--substitutions _RUN_SERVICE=hello-go,_GCP_REGION=$REGION,_PORT=8080,_ENV_VARS=FOOBAR="hello-go",_CONCURRENCY=80,_CPU=2,_MEMORY_GB=4Gi,_TIMEOUT=1000,_MIN_INSTANCES=0,_MAX_INSTANCES=1,_INGRESS_SETTINGS=all,_TAG=version1,_REPO=$REPO
gcloud builds submit ./src \
--region $REGION \
--config ./cloudbuild.yaml \
--substitutions _RUN_SERVICE=hello-go,_GCP_REGION=$REGION,_PORT=8080,_ENV_VARS=FOOBAR="hello-go",_CONCURRENCY=80,_CPU=2,_MEMORY_GB=4Gi,_TIMEOUT=1000,_MIN_INSTANCES=0,_MAX_INSTANCES=1,_INGRESS_SETTINGS=all,_TAG=version2,_REPO=$REPO
gcloud builds submit ./src \
--region $REGION \
--config ./cloudbuild.yaml \
--substitutions _RUN_SERVICE=hello-go,_GCP_REGION=$REGION,_PORT=8080,_ENV_VARS=FOOBAR="hello-go",_CONCURRENCY=80,_CPU=2,_MEMORY_GB=4Gi,_TIMEOUT=1000,_MIN_INSTANCES=0,_MAX_INSTANCES=1,_INGRESS_SETTINGS=all,_TAG=version3,_REPO=$REPO
gcloud run services update-traffic hello-go \
--region $REGION \
--to-tags=version1=33,version2=33,version3=34
gcloud run services update-traffic hello-go \
--region $REGION \
--to-tags=version3=100