We want to use django’s redis cache feature that allows us to specify version numbers which will effectively invalidate cache values of a previous build (before the code changed).
GCP’s Cloud build has a default $BUILD_ID value available to the build yaml files, but is there a way for a deployed container to access this BUILD_ID value? If we could, we could us it (or a modulo value of it) to be our unique cache version.
- See https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values for GCP build variables
- See https://docs.djangoproject.com/en/4.0/topics/cache/#cache-arguments for django cache documentation
2
Answers
Your
cloudbuild.yaml
file can pass substitution variables into a container you are deploying by using theset-env-vars
flag.Here we pass
_REDIS_HOST
and_REDIS_PORT
andBUILD_ID
into the container asREDISHOST
,REDISPORT
andBUILD_ID
.We can now read these within python like so:
settings.py file:
Use Method:prjects.builds.list API, you could use this API to get all list, and also you can query pageSize to get the number of results in the list.
When you get response from the API, you can just do what you want with your $BUILD_ID
I hope this information above is helpful.