Skip to content

Commit

Permalink
docker: pass env variables from GH workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Oct 11, 2023
1 parent 4fe7d7b commit a786470
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/dockerpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
14 changes: 12 additions & 2 deletions site/zenodo_rdm/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit a786470

Please sign in to comment.