From a7864705e638df4162316785f56f6cbe1aa419c2 Mon Sep 17 00:00:00 2001 From: Zacharias Zacharodimos Date: Wed, 11 Oct 2023 17:13:14 +0200 Subject: [PATCH] docker: pass env variables from GH workflow --- .github/workflows/dockerpublish.yml | 15 +++++++++++++++ Dockerfile | 10 ++++++++++ site/zenodo_rdm/decorators.py | 14 ++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dockerpublish.yml b/.github/workflows/dockerpublish.yml index 7a65f493e..eafea9355 100644 --- a/.github/workflows/dockerpublish.yml +++ b/.github/workflows/dockerpublish.yml @@ -49,9 +49,24 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Populate image build timestamp + id: date + run: echo "IMAGE_BUILD_TIMESTAMP=$(date +'%Y%m%d_%H%M%S')" >> $GITHUB_ENV + + - name: Populate sentry release tag for development + run: echo "SENTRY_RELEASE=${{github.sha}} >> $GITHUB_ENV + if: ${{steps.meta.outputs.tags == 'latest'}} + + - name: Populate sentry release tag for prod/sandbox + run: echo "SENTRY_RELEASE=${{steps.meta.outputs.tags}} >> $GITHUB_ENV + if: ${{steps.meta.outputs.tags != 'latest'}} + - name: Build and publish image uses: docker/build-push-action@v3 with: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + IMAGE_BUILD_TIMESTAMP=${{env.IMAGE_BUILD_TIMESTAMP}} + SENTRY_RELEASE=${{env.SENTRY_RELEASE}} diff --git a/Dockerfile b/Dockerfile index 3ca72e4cc..f80b1c6f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,6 +32,16 @@ COPY ./app_data/ ${INVENIO_INSTANCE_PATH}/app_data/ COPY ./translations ${INVENIO_INSTANCE_PATH}/translations COPY ./ . +# application build args to be exposed as environment variables +ARG IMAGE_BUILD_TIMESTAMP +ARG SENTRY_RELEASE + +# Expose random sha to uniquely identify this build +ENV INVENIO_IMAGE_BUILD_TIMESTAMP=${IMAGE_BUILD_TIMESTAMP} +ENV SENTRY_RELEASE=${SENTRY_RELEASE} + +RUN echo "Image build timestamp $INVENIO_IMAGE_BUILD_TIMESTAMP" + RUN cp -r ./static/. ${INVENIO_INSTANCE_PATH}/static/ && \ cp -r ./assets/. ${INVENIO_INSTANCE_PATH}/assets/ && \ invenio collect --verbose && \ diff --git a/site/zenodo_rdm/decorators.py b/site/zenodo_rdm/decorators.py index e6b3a5728..cab429f4c 100644 --- a/site/zenodo_rdm/decorators.py +++ b/site/zenodo_rdm/decorators.py @@ -7,9 +7,11 @@ """Decorators.""" +import datetime + from functools import wraps -from flask import session +from flask import current_app, session from flask_login import current_user from invenio_cache import current_cache @@ -25,9 +27,17 @@ def cached_unless_authenticated_or_flashes(timeout=50, key_prefix="default"): def caching(f): @wraps(f) def wrapper(*args, **kwargs): + # we compute the cache key based on the `Dockerfile.IMAGE_BUILD_TIMESTAMP` + # env variable if set or we generate a new timestamp every time + # i.e no cache. This happens in the case of local development + cache_prefix = ( + f"{key_prefix}_{current_app.config.get('IMAGE_BUILD_TIMESTAMP')}" + if current_app.debug is not True + else f"{key_prefix}_{datetime.datetime.utcnow():%Y%m%d_%H%M%S}" + ) cache_fun = current_cache.cached( timeout=timeout, - key_prefix=key_prefix, + key_prefix=cache_prefix, unless=has_flashes_or_authenticated_user, ) return cache_fun(f)(*args, **kwargs)