Skip to content

Commit

Permalink
Merge pull request #307 from dachrisch/master
Browse files Browse the repository at this point in the history
live db and deploy workflow updates
  • Loading branch information
dachrisch authored Mar 3, 2025
2 parents a380a68 + fc0f781 commit d12ac6b
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
environment:
MYSQL_DATABASE: test_test_db
MYSQL_USER: user
MYSQL_PASSWORD: user
MYSQL_PWD: user
working_directory: ~/repo

steps:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: 🏗️🧪🚀 Build, Test and Deploy everything

on:
push:
branches: [ deploy ]
branches:
- 'deploy/**' # Matches all branches under deploy/

permissions:
contents: write
Expand Down Expand Up @@ -42,6 +43,7 @@ jobs:
with:
image_name: leaguesphere/frontend
dockerfile: container/nginx.Dockerfile
test: false

deploy-be:
needs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- '*'
- '!deploy'
- '!deploy/**' # Excludes deploy/* branches
pull_request:
branches:
- '*'
Expand Down
27 changes: 26 additions & 1 deletion .github/workflows/part_docker_push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ on:
required: true
description: The dockerfile used to build the image
type: string
test:
required: false
default: true
description: Whether to test the image
type: boolean
secrets:
DOCKER_TOKEN:
required: true
Expand Down Expand Up @@ -50,6 +55,26 @@ jobs:
with:
images: ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}

- name: 🌿 Extract branch name
id: extract_branch
run: echo "TAG_NAME=${GITHUB_REF#refs/heads/deploy/}" >> $GITHUB_ENV

# Test run the container
- name: 🐳📦 Build and export 'test' to Docker
if: ${{ inputs.test }}
uses: docker/build-push-action@v6
with:
file: ${{ inputs.dockerfile }}
load: true
tags: ${{ env.DOCKER_IMAGE_NAME }}:test
cache-from: type=gha
cache-to: type=gha,mode=max

- name: 🐳🧪Test
if: ${{ inputs.test }}
run: |
docker run --rm ${{ env.DOCKER_IMAGE_NAME }}:test
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: 🐳🚀 Build and push Docker image
Expand All @@ -58,7 +83,7 @@ jobs:
with:
file: ${{ inputs.dockerfile }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.DOCKER_IMAGE_NAME }}:latest
tags: ${{ env.DOCKER_IMAGE_NAME }}:${{ env.TAG_NAME }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
45 changes: 27 additions & 18 deletions container/app.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
FROM python:3.11-slim AS app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ARG APP_USER="django"
ARG APP_DIR="/app"
# install curl for healthcheck
FROM python:3.11-slim AS app-builder

RUN apt -y update
RUN apt -y install curl
RUN apt -y install pkg-config
RUN apt -y install python3-dev
RUN apt -y install build-essential
RUN apt -y install default-libmysqlclient-dev
# add user
RUN adduser --disabled-password --home ${APP_DIR} ${APP_USER}
RUN chown ${APP_USER}:${APP_USER} -R ${APP_DIR}
RUN apt -y install default-libmysqlclient-dev # to build the mysql client
RUN apt -y install git # for development dependency in requirements.txt

COPY ../requirements.txt .

WORKDIR ${APP_DIR}
COPY --chown=${APP_USER} ../requirements.txt ${APP_DIR}
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN pip install gunicorn
RUN pip install django-debug-toolbar

RUN apt -y remove pkg-config
RUN apt -y install python3-dev
RUN apt -y install build-essential
RUN apt -y install default-libmysqlclient-dev
FROM python:3.11-slim AS app

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ARG APP_USER="django"
ARG APP_DIR="/app"

RUN apt -y update
RUN apt -y install curl # install curl for healthcheck
RUN apt -y install jq # install jq for healthcheck
RUN apt -y install default-libmysqlclient-dev # to run the mysql client
RUN pip install gunicorn

# add user
RUN adduser --disabled-password --home ${APP_DIR} ${APP_USER}
RUN chown ${APP_USER}:${APP_USER} -R ${APP_DIR}

USER ${APP_USER}
COPY --chown=${APP_USER} ../ ${APP_DIR}
RUN rm -rf .git/

COPY --chown=${APP_USER} --from=app-builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages/

COPY --chown=${APP_USER} ../container/entrypoint.sh /app/entrypoint.sh

RUN chmod 740 /app/entrypoint.sh

WORKDIR ${APP_DIR}

EXPOSE 8000

ENTRYPOINT ["/app/entrypoint.sh"]
8 changes: 6 additions & 2 deletions container/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/sh

python manage.py makemigrations --no-input
python manage.py migrate --no-input
# Run migrations only if RUN_MIGRATIONS is set to "true"
if [ "$RUN_MIGRATIONS" = "true" ]; then
echo "Running database migrations..."
python manage.py makemigrations --no-input
python manage.py migrate --no-input
fi

exec "$@"
1 change: 1 addition & 0 deletions container/nginx.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apt -y install pkg-config
RUN apt -y install python3-dev
RUN apt -y install build-essential
RUN apt -y install default-libmysqlclient-dev
RUN apt -y install git # for development dependency in requirements.txt

# install environment
COPY ../requirements.txt ${APP_DIR}
Expand Down
38 changes: 6 additions & 32 deletions deployed/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: ${SERVICE_NAME}
services:
www:
image: leaguesphere/frontend:latest
image: leaguesphere/frontend:remote_db
container_name: ${COMPOSE_PROJECT_NAME}.www
labels:
# redirect service
Expand Down Expand Up @@ -37,50 +37,24 @@ services:
restart: unless-stopped

app:
image: leaguesphere/backend:latest
image: leaguesphere/backend:remote_db
container_name: ${COMPOSE_PROJECT_NAME}.app
command: gunicorn -b 0.0.0.0:8000 league_manager.wsgi
labels:
- traefik.enable=false
# portainer team
- io.portainer.accesscontrol.teams=${SERVICE_NAME}
env_file: ls.env
environment:
MYSQL_HOST: db
MYSQL_DB_NAME: ${MYSQL_DB_NAME}
DJANGO_SETTINGS_MODULE: league_manager.settings.dev
SECRET_KEY: ${SECRET_KEY}
networks:
- backend
depends_on:
db:
condition: service_healthy
healthcheck:
test: curl -A healthcheck -I --fail http://localhost:8000 || exit 1
restart: unless-stopped

db:
image: mariadb:lts
container_name: ${COMPOSE_PROJECT_NAME}.db
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-storage-engine=InnoDB
labels:
- traefik.enable=false
# portainer team
- io.portainer.accesscontrol.teams=${SERVICE_NAME}
volumes:
- "./database:/var/lib/mysql"
RUN_MIGRATIONS: ${RUN_MIGRATIONS:-false} # Default to false if not set
networks:
- proxy
- backend
environment:
MYSQL_DATABASE: ${MYSQL_DB_NAME}
MYSQL_ALLOW_EMPTY_PASSWORD: true
healthcheck:
test: healthcheck.sh --connect --innodb_initialized
start_period: 5s
test: curl -A healthcheck http://localhost:8000/health/?format=json | jq -e '.DatabaseHeartBeatCheck == "working"'
restart: unless-stopped

volumes:
static:

networks:
backend:
internal: true
Expand Down
2 changes: 2 additions & 0 deletions league_manager/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
'accounts',
'knox',
'passcheck',
'health_check',
'health_check.contrib.db_heartbeat',
]

MIDDLEWARE = [
Expand Down
3 changes: 2 additions & 1 deletion league_manager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
path('logout/', auth_view.LogoutView.as_view(template_name='registration/logout.html'),
name='logout'),
# path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('accounts/', include('accounts.urls'))
path('accounts/', include('accounts.urls')),
path(r'health/', include('health_check.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ django-rest-knox==5.0.2
python-dotenv==1.0.1
mysqlclient==2.2.7
requests==2.32.3
git+https://github.com/revsys/django-health-check@master # simple db checks are only available in 3.18.4.dev, whhich isn't released by now

0 comments on commit d12ac6b

Please sign in to comment.