From af6b17319dd65845c969bf22bc5429ca0b6618b8 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 21 Dec 2022 10:40:05 +0100 Subject: [PATCH] include version and commit hash in status endpoint --- .gitlab-ci.yml | 2 ++ README.md | 2 +- app/main.py | 7 ++++++- requirements.txt | 1 + version.env | 1 + 5 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 version.env diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ea93548..ffa1295 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,6 +8,8 @@ build: rules: - if: $CI_COMMIT_BRANCH tags: [ docker ] + before_script: + - echo "COMMIT=`git rev-parse HEAD`" >> version.env script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build . --tag ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} diff --git a/README.md b/README.md index 0599da0..7f021cb 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Just a simple *hello world* endpoint. ### `GET /status` -Status endpoint, used for *healthcheck*. +Status endpoint, used for *healthcheck*. Shows also current version and commit hash. ### `GET /-/origins` diff --git a/app/main.py b/app/main.py index 13f0fad..813b5fe 100644 --- a/app/main.py +++ b/app/main.py @@ -17,6 +17,11 @@ import dataset from Crypto.PublicKey import RSA from Crypto.PublicKey.RSA import RsaKey +logger = logging.getLogger() +dotenv_values('version.env') + +VERSION, COMMIT, DEBUG = getenv('VERSION', 'unknown'), getenv('COMMIT', 'unknown'), bool(getenv('DEBUG', False)) + def load_file(filename) -> bytes: with open(filename, 'rb') as file: @@ -60,7 +65,7 @@ async def index(): @app.get('/status') async def status(request: Request): - return JSONResponse({'status': 'up'}) + return JSONResponse({'status': 'up', 'version': VERSION, 'commit': COMMIT, 'debug': DEBUG}) @app.get('/-/origins') diff --git a/requirements.txt b/requirements.txt index 03e8842..413b6d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ pycryptodome==3.16.0 python-dateutil==2.8.2 dataset==1.5.2 markdown==3.4.1 +python-dotenv==0.21.0 diff --git a/version.env b/version.env new file mode 100644 index 0000000..3407a86 --- /dev/null +++ b/version.env @@ -0,0 +1 @@ +VERSION=0.5