Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.
Draft
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
55 changes: 55 additions & 0 deletions .github/workflows/docker-build-push-cron.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Build and Push Cron Docker Image

on:
push:
branches: [main]
paths:
- "src/**"
- ".github/workflows/docker-build-push-cron.yml"
workflow_dispatch:

env:
REGISTRY: ghcr.io
IMAGE_NAME: Life-USTC/server-cron

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,format=short
type=ref,event=branch
latest

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ./src
file: ./src/Dockerfile.cron
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
17 changes: 17 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ services:
networks:
- backend

cron:
image: ghcr.io/life-ustc/server-cron:latest
restart: always
depends_on:
- db
environment:
- DJANGO_SECRET_KEY=${DJANGO_SECRET_KEY}
- DJANGO_DEBUG=False
- DJANGO_ALLOWED_HOSTS=life-ustc.tiankaima.dev
- POSTGRES_DB=${POSTGRES_DB:-postgres}
- POSTGRES_USER=${POSTGRES_USER:-user}
- POSTGRES_PASSWORD=${POSTGRES_password}
- POSTGRES_HOST=${POSTGRES_HOST:-db}
- POSTGRES_PORT=${POSTGRES_PORT:-5432}
networks:
- backend

networks:
backend:
driver: bridge
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ services:
networks:
- backend

cron:
build:
context: ./src
dockerfile: Dockerfile.cron
volumes:
- ./src:/app
depends_on:
- db
environment:
- DJANGO_DEBUG=True
- DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0
- POSTGRES_DB=${POSTGRES_DB:-postgres}
- POSTGRES_USER=${POSTGRES_USER:-user}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
- POSTGRES_HOST=${POSTGRES_HOST:-db}
- POSTGRES_PORT=${POSTGRES_PORT:-5432}
networks:
- backend

networks:
backend:
driver: bridge
27 changes: 27 additions & 0 deletions src/Dockerfile.cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM python:3.13-slim

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

COPY . .

# Install cron, gettext, and git (needed for load_from_static to clone repos)
RUN apt-get update && apt-get install -y cron gettext git && rm -rf /var/lib/apt/lists/* \
&& pip install --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt

RUN python manage.py compilemessages

# Copy crontab file and set up cron
COPY crontab /etc/cron.d/django-cron
RUN chmod 0644 /etc/cron.d/django-cron \
&& crontab /etc/cron.d/django-cron \
&& touch /var/log/cron.log

# Create entrypoint script
RUN echo '#!/bin/bash\nset -e\nprintenv | grep -v "no_proxy" >> /etc/environment\ncron && tail -f /var/log/cron.log' > /entrypoint.sh \
&& chmod +x /entrypoint.sh

CMD ["/entrypoint.sh"]
4 changes: 4 additions & 0 deletions src/crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run load_from_static command 4 times per day (every 6 hours)
# At 00:00, 06:00, 12:00, and 18:00 every day
0 0,6,12,18 * * * cd /app && python manage.py load_from_static >> /var/log/cron.log 2>&1