skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Your cloudbuild.yaml file can pass substitution variables into a container you are deploying by using the set-env-vars flag.

      # Deploy container image to Cloud Run
      - id: "deploy"
        name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
        entrypoint: gcloud
        args:
          - 'run'
          - 'deploy'
          - '${_SERVICE_NAME}'
          - '--image'
          - 'gcr.io/$PROJECT_ID/${_SERVICE_NAME}:$COMMIT_SHA'
          - '--platform=managed'
          - '--region=${_DEPLOY_REGION}'
          - '--vpc-connector redis'
          - '--set-env-vars REDISHOST=${_REDIS_HOST},REDISPORT=${_REDIS_PORT},BUILD_ID=$BUILD_ID'
    

    Here we pass _REDIS_HOST and _REDIS_PORT and BUILD_ID into the container as REDISHOST, REDISPORT and BUILD_ID.

    We can now read these within python like so:

    settings.py file:

    ...
    
    redis_host = os.environ.get('REDISHOST', 'localhost')
    redis_port = int(os.environ.get('REDISPORT', 6379))
    build_id = int(os.environ.get('BUILD_ID', None))
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search