From 2ff014c9280ff5cfea9362cdc41f63b797fac778 Mon Sep 17 00:00:00 2001 From: Abderrahmane Smimite Date: Sun, 14 Apr 2024 17:15:51 +0200 Subject: [PATCH] Improve version handling for multiple builds --- .github/workflows/docker-build-and-push.yml | 12 ++++--- backend/.dockerignore | 2 ++ backend/.meta | 2 ++ backend/Dockerfile | 27 ++++++---------- backend/ciso_assistant/.meta | 2 ++ backend/ciso_assistant/settings.py | 31 +++++------------- docker-compose-build.sh | 35 +++++++++++++++------ docker-compose-build.yml | 3 -- docker-compose.sh | 3 -- docker-compose.yml | 2 -- 10 files changed, 56 insertions(+), 63 deletions(-) create mode 100644 backend/.meta create mode 100644 backend/ciso_assistant/.meta diff --git a/.github/workflows/docker-build-and-push.yml b/.github/workflows/docker-build-and-push.yml index 0d8aee55e..5119fec5b 100644 --- a/.github/workflows/docker-build-and-push.yml +++ b/.github/workflows/docker-build-and-push.yml @@ -32,10 +32,17 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Get git version and build + - name: Get version and Create .meta file run: | echo "VERSION=$(git describe --tags --always)" >> $GITHUB_ENV echo "BUILD=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + + echo "CISO_ASSISTANT_VERSION=$(git describe --tags --always)" > .meta + echo "CISO_ASSISTANT_BUILD=$(git rev-parse --short HEAD)" >> .meta + + cp .meta ./backend/ + cp .meta ./backend/ciso_assistant/ + - name: Build and Push Backend Docker Image uses: docker/build-push-action@v5 @@ -47,9 +54,6 @@ 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 diff --git a/backend/.dockerignore b/backend/.dockerignore index 22b5ea72b..320c6e5dd 100644 --- a/backend/.dockerignore +++ b/backend/.dockerignore @@ -5,3 +5,5 @@ .git* .pytest* .idea* +.dockerignore +Dockerfile \ No newline at end of file diff --git a/backend/.meta b/backend/.meta new file mode 100644 index 000000000..ade2090c5 --- /dev/null +++ b/backend/.meta @@ -0,0 +1,2 @@ +CISO_ASSISTANT_VERSION=dev +CISO_ASSISTANT_BUILD=dev \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile index fd70e7372..d25aeebf7 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -2,35 +2,26 @@ # Based on https://docs.docker.com/samples/django/ FROM python:3.11 -ENV PYTHONUNBUFFERED 1 +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 + 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 - -# Define ARGs for VERSION and BUILD, which will be passed at build time -ARG VERSION=unknown -ARG BUILD=unknown +RUN pip install --upgrade pip && \ + pip install -r requirements.txt -# 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 diff --git a/backend/ciso_assistant/.meta b/backend/ciso_assistant/.meta new file mode 100644 index 000000000..ade2090c5 --- /dev/null +++ b/backend/ciso_assistant/.meta @@ -0,0 +1,2 @@ +CISO_ASSISTANT_VERSION=dev +CISO_ASSISTANT_BUILD=dev \ No newline at end of file diff --git a/backend/ciso_assistant/settings.py b/backend/ciso_assistant/settings.py index a81f56bef..975707b30 100644 --- a/backend/ciso_assistant/settings.py +++ b/backend/ciso_assistant/settings.py @@ -20,41 +20,21 @@ # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +load_dotenv(BASE_DIR / ".meta") -dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env') -load_dotenv(dotenv_path) +VERSION = os.getenv("CISO_ASSISTANT_VERSION", "unset") +BUILD = os.getenv("CISO_ASSISTANT_BUILD", "unset") 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 get_git_version_and_build(): - """Fetches version and build information using Git commands.""" - try: - # Get the latest tag or describe output - version = subprocess.check_output(["git", "describe", "--tags", "--always"], stderr=subprocess.STDOUT).strip().decode() - except subprocess.CalledProcessError: - # Default if the command fails - version = "unknown" - - try: - # Get the short commit hash - build = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT).strip().decode() - except subprocess.CalledProcessError: - # Default if the command fails - build = "unknown" - - return version, build def set_ciso_assistant_url(_, __, event_dict): event_dict["ciso_assistant_url"] = CISO_ASSISTANT_URL return event_dict -if VERSION == "unset" or BUILD == "unset": - VERSION, BUILD = get_git_version_and_build() + LOGGING = { "version": 1, @@ -103,6 +83,9 @@ def set_ciso_assistant_url(_, __, event_dict): logger = structlog.getLogger(__name__) logger.info("BASE_DIR: %s", BASE_DIR) +logger.info("VERSION: %s", VERSION) +logger.info("BUILD: %s", BUILD) + # TODO: multiple paths are explicit, it should use path join to be more generic # Quick-start development settings - unsuitable for production diff --git a/docker-compose-build.sh b/docker-compose-build.sh index 4d391b082..0c7ed5ea7 100755 --- a/docker-compose-build.sh +++ b/docker-compose-build.sh @@ -1,17 +1,34 @@ -#! /usr/bin/env bash +#!/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") +prepare_meta_file() { + VERSION=$(git describe --tags --always) + BUILD=$(git rev-parse --short HEAD) -if [ -f db/ciso-assistant.sqlite3 ] ; then - echo "the database seems already created" - echo "you should launch docker compose up -d" + echo "CISO_ASSISTANT_VERSION=${VERSION}" > .meta + echo "CISO_ASSISTANT_BUILD=${BUILD}" >> .meta + + cp .meta ./backend/ciso_assistant/.meta + cp .meta ./backend/.meta +} + +# Check if the database already exists +if [ -f db/ciso-assistant.sqlite3 ]; then + echo "The database seems already created." + echo "You should launch 'docker compose up -d'." else + prepare_meta_file + + # Build and start the containers docker compose -f docker-compose-build.yml build docker compose -f docker-compose-build.yml up -d + + # Perform database migrations docker compose exec backend python manage.py migrate - echo "initialize your superuser account..." + + # Initialize the superuser account + echo "Initialize your superuser account..." docker compose exec backend python manage.py createsuperuser - echo "connect to ciso assistant on https://localhost:8443" - echo "for successive runs you can now use docker compose up" + + echo "Connect to CISO Assistant on https://localhost:8443" + echo "For successive runs, you can now use 'docker compose up'." fi diff --git a/docker-compose-build.yml b/docker-compose-build.yml index 6df75b0fb..a8996c777 100644 --- a/docker-compose-build.yml +++ b/docker-compose-build.yml @@ -6,9 +6,6 @@ services: build: context: ./backend dockerfile: Dockerfile - args: - VERSION: ${VERSION:-unknown} - BUILD: ${BUILD:-unknown} restart: always environment: - ALLOWED_HOSTS=backend diff --git a/docker-compose.sh b/docker-compose.sh index c5f8982c3..5a20838c8 100755 --- a/docker-compose.sh +++ b/docker-compose.sh @@ -1,8 +1,5 @@ #! /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" diff --git a/docker-compose.yml b/docker-compose.yml index 1997a42f7..f7605960b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,6 @@ 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