Skip to content

Commit

Permalink
improve version management
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Apr 13, 2024
1 parent 27b252e commit 68cb021
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 30 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
*.DS_Store
*~$*
**/*.mo
.git*
.pytest*
.idea*
11 changes: 8 additions & 3 deletions .github/workflows/docker-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get git version
run: echo "VERSION=$(git describe --tags --always)" >> $GITHUB_ENV

- name: Get git version and build
run: |
echo "VERSION=$(git describe --tags --always)" >> $GITHUB_ENV
echo "BUILD=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Build and Push Backend Docker Image
uses: docker/build-push-action@v5
with:
Expand All @@ -45,6 +47,9 @@ jobs:
ghcr.io/${{ github.repository }}/backend:${{ env.VERSION }}
ghcr.io/${{ github.repository }}/backend:latest
platforms: linux/amd64,linux/arm64,linux/arm64/v8
build-args: |
VERSION=${{ env.VERSION }}
BUILD=${{ env.BUILD }}
- name: Build and Push Frontend Docker Image
uses: docker/build-push-action@v5
Expand Down
1 change: 0 additions & 1 deletion backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
*.DS_Store
*~$*
**/*.mo
.git*
.pytest*
.idea*
25 changes: 19 additions & 6 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@ ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE=1

WORKDIR /code

# Remove Git installation steps since you will pass VERSION and BUILD as build-args

# Configure locales
RUN apt update && \
apt install -y gettext locales && \
apt clean && \
rm -rf /var/lib/apt/lists/* && \
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
sed -i -e 's/# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/' /etc/locale.gen && \
locale-gen

COPY . /code/
COPY startup.sh /code/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN apt update && \
apt install -y gettext && \
apt install -y locales

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
&& sed -i -e 's/# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/' /etc/locale.gen \
&& locale-gen
# Define ARGs for VERSION and BUILD, which will be passed at build time
ARG VERSION=unknown
ARG BUILD=unknown

# Set the ARG values as ENV variables so they're available at runtime
ENV CISO_ASSISTANT_VERSION=$VERSION
ENV CISO_ASSISTANT_BUILD=$BUILD

ENTRYPOINT ["bash", "startup.sh"]
EXPOSE 8000
30 changes: 13 additions & 17 deletions backend/ciso_assistant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from pathlib import Path
import os
from dotenv import load_dotenv
import subprocess
import json
import logging.config
import structlog
Expand All @@ -19,10 +21,16 @@
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')
load_dotenv(dotenv_path)

LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO")
LOG_FORMAT = os.environ.get("LOG_FORMAT", "plain")

CISO_ASSISTANT_URL = os.environ.get("CISO_ASSISTANT_URL", "http://localhost:5173")
VERSION = os.getenv("CISO_ASSISTANT_VERSION", "unset")
BUILD = os.getenv("CISO_ASSISTANT_BUILD", "unset")



def set_ciso_assistant_url(_, __, event_dict):
Expand Down Expand Up @@ -63,7 +71,8 @@ def set_ciso_assistant_url(_, __, event_dict):
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(), # Include stack information in log entries
# Include stack information in log entries
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
Expand All @@ -76,20 +85,7 @@ def set_ciso_assistant_url(_, __, event_dict):
logger = structlog.getLogger(__name__)

logger.info("BASE_DIR: %s", BASE_DIR)

with open(BASE_DIR / "ciso_assistant/VERSION") as f:
VERSION = f.read().strip()
logger.info("CISO Assistant Version: %s" % VERSION)

try:
with open(BASE_DIR / "ciso_assistant/build.json") as f:
BUILD = json.load(f)["build"]
logger.info("CISO Assistant Build: %s" % BUILD)
except FileNotFoundError:
BUILD = "unset"
logger.warning(
"CISO Assistant Build: unset. Please refer to the documentation to set it."
)
# TODO: multiple paths are explicit, it should use path join to be more generic

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
Expand Down Expand Up @@ -270,7 +266,7 @@ def set_ciso_assistant_url(_, __, event_dict):
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

# SQLIte file can be changed, useful for tests
SQLITE_FILE = os.environ.get('SQLITE_FILE', BASE_DIR / "db/ciso-assistant.sqlite3")
SQLITE_FILE = os.environ.get("SQLITE_FILE", BASE_DIR / "db/ciso-assistant.sqlite3")


if "POSTGRES_NAME" in os.environ:
Expand All @@ -290,7 +286,7 @@ def set_ciso_assistant_url(_, __, event_dict):
"ENGINE": "django.db.backends.sqlite3",
"NAME": SQLITE_FILE,
"OPTIONS": {
'timeout': 120,
"timeout": 120,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ django-tailwind==3.8.0
pyyaml==6.0.1
django-structlog==8.0.0
structlog==24.1.0
python-dotenv==1.0.1
3 changes: 3 additions & 0 deletions docker-compose-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /usr/bin/env bash

export VERSION=$(git describe --tags --always 2> /dev/null || echo "unknown")
export BUILD=$(git rev-parse --short HEAD 2> /dev/null || echo "unknown")

if [ -f db/ciso-assistant.sqlite3 ] ; then
echo "the database seems already created"
echo "you should launch docker compose up -d"
Expand Down
7 changes: 6 additions & 1 deletion docker-compose-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ version: "3.9"
services:
backend:
container_name: backend
build: ./backend
build:
context: ./backend
dockerfile: Dockerfile
args:
VERSION: ${VERSION:-unknown}
BUILD: ${BUILD:-unknown}
restart: always
environment:
- ALLOWED_HOSTS=backend
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /usr/bin/env bash

export VERSION=$(git describe --tags --always 2> /dev/null || echo "unknown")
export BUILD=$(git rev-parse --short HEAD 2> /dev/null || echo "unknown")

if [ -f db/ciso-assistant.sqlite3 ] ; then
echo "the database seems already created"
echo "you should launch docker compose up -d"
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
- ALLOWED_HOSTS=backend
- CISO_ASSISTANT_URL=https://localhost:8443
- DJANGO_DEBUG=True
- CISO_ASSISTANT_VERSION=${VERSION:-unknown}
- CISO_ASSISTANT_BUILD=${BUILD:-unknown}
volumes:
- ./db:/code/db

Expand Down
1 change: 0 additions & 1 deletion frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Dockerfile
.dockerignore
.git
.gitignore
.gitattributes
README.md
Expand Down

0 comments on commit 68cb021

Please sign in to comment.