Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve version handling for multiple modes of build #252

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/docker-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
.git*
.pytest*
.idea*
.dockerignore
Dockerfile
2 changes: 2 additions & 0 deletions backend/.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CISO_ASSISTANT_VERSION=dev
CISO_ASSISTANT_BUILD=dev
27 changes: 9 additions & 18 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions backend/ciso_assistant/.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CISO_ASSISTANT_VERSION=dev
CISO_ASSISTANT_BUILD=dev
31 changes: 7 additions & 24 deletions backend/ciso_assistant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
35 changes: 26 additions & 9 deletions docker-compose-build.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions docker-compose-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ services:
build:
context: ./backend
dockerfile: Dockerfile
args:
VERSION: ${VERSION:-unknown}
BUILD: ${BUILD:-unknown}
restart: always
environment:
- ALLOWED_HOSTS=backend
Expand Down
3 changes: 0 additions & 3 deletions docker-compose.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading