-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antti Viitala
committed
Dec 18, 2023
0 parents
commit d431fe4
Showing
17 changed files
with
679 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: "Build Django image" | ||
|
||
concurrency: | ||
group: ${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
on: | ||
push: | ||
branches: | ||
- "main" | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
jobs: | ||
build-push: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
|
||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ github.token }} | ||
|
||
- name: Build and push | ||
uses: docker/build-push-action@v4 | ||
id: build-image | ||
env: | ||
REGISTRY: ghcr.io/${{ github.repository }} | ||
with: | ||
context: ./. | ||
file: ./Dockerfile | ||
push: true | ||
tags: ${{env.REGISTRY}}:${{github.run_number}} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
test_emails/ | ||
test_backups/ | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
# Ignore 'real' .env files, but ensure that .erb ending env files are committed as these are templates | ||
.env | ||
.env.* | ||
!.env*.erb | ||
*.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ | ||
|
||
.DS_Store | ||
|
||
# Django | ||
*.sql | ||
|
||
# Linters | ||
**.ruff_cache | ||
|
||
# Profilers | ||
importtime.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
ARG PYTHON_BUILD_VERSION=3.11 | ||
|
||
FROM python:$PYTHON_BUILD_VERSION-slim AS BASE | ||
WORKDIR /app | ||
|
||
ENV PIP_DEFAULT_TIMEOUT=100 \ | ||
# Allow statements and log messages to immediately appear | ||
PYTHONUNBUFFERED=1 \ | ||
PYTHONDONTWRITEBYTECODE=1 \ | ||
# disable a pip version check to reduce run-time & log-spam | ||
PIP_DISABLE_PIP_VERSION_CHECK=1 \ | ||
# cache is useless in docker image, so disable it to reduce image size | ||
PIP_NO_CACHE_DIR=1 | ||
|
||
# Update and install dependencies | ||
RUN apt-get update && \ | ||
apt-get -y upgrade && \ | ||
apt-get -y install --no-install-recommends python3-dev gcc libc-dev vim curl postgresql-client && \ | ||
# Clean up | ||
apt-get autoremove -y && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* && \ | ||
# Add a non-root user | ||
groupadd -g 999 python && \ | ||
useradd -r -u 999 -g python python | ||
|
||
WORKDIR /app | ||
|
||
# Python Dependencies | ||
COPY --chown=python:python ./requirements.txt /app/ | ||
RUN pip install --upgrade pip \ | ||
&& pip install -r requirements.txt --no-cache-dir --compile | ||
|
||
# Clean up | ||
RUN apt-get -y purge gcc libc-dev python3-dev | ||
|
||
# Add all application code from this folder, including deployment entrypoints | ||
COPY --chown=python:python ./ /app | ||
|
||
# Make entrypoints executable | ||
RUN chmod +x /app/deployment/server-entrypoint.sh && \ | ||
chmod +x /app/deployment/worker-entrypoint.sh | ||
|
||
USER 999 | ||
|
||
EXPOSE 8000 | ||
CMD [ "/app/deployment/server-entrypoint.sh" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# `railway_django_stack` | ||
|
||
<!-- BUTTON HERE --> | ||
|
||
## Resources | ||
|
||
This template deploys: | ||
|
||
- 1 container running Django | ||
- 1 container running Celery (same as container #1 but with different startup command) | ||
- 1 service running Redis | ||
- 1 service running Postgres | ||
|
||
You can test the setup locally with docker compose: | ||
|
||
```bash | ||
git clone https://github.com/Antvirf/railway_django_stack | ||
cd railway_django_stack | ||
docker-compose up | ||
``` | ||
|
||
## Service diagram | ||
|
||
```mermaid | ||
flowchart LR | ||
subgraph rwp["Your Railway Project"] | ||
subgraph public["Publicly exposed services"] | ||
django["App container\n(Django server)"] | ||
end | ||
subgraph private["Private services"] | ||
celery["App container\n(Celery worker)"] | ||
psql["PostgreSQL"] | ||
redis["Redis"] | ||
end | ||
end | ||
users["Users"] --> django | ||
django --> celery | ||
django --> psql | ||
celery --> psql | ||
celery --> redis | ||
django --> redis | ||
``` | ||
|
||
## Django project setup | ||
|
||
This is a barebones Django-project with the following additions/updates: | ||
|
||
- Configures a PostgreSQL database | ||
- Configures a Redis cache | ||
- Configures Celery, and installs the following add-on apps: | ||
- [`django-celery-beat`](https://github.com/celery/django-celery-beat) for periodic task management | ||
- [`django-celery-results`](https://github.com/celery/django-celery-results) for viewing results of Celery tasks in Django Admin | ||
- Uses [`python-decouple`](https://github.com/HBNetwork/python-decouple) to manage settings via environment varialbes | ||
- Installs and runs with [`gunicorn`](https://github.com/benoitc/gunicorn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
|
||
if [ "$RUN_MIGRATIONS" = "True" ]; then | ||
echo "Running migrations..." | ||
until python manage.py migrate | ||
do | ||
echo "Waiting for db to be ready..." | ||
sleep 2 | ||
done | ||
fi | ||
|
||
echo "Starting app server..." | ||
python -m gunicorn railway_django_stack.wsgi:application \ | ||
--bind 0.0.0.0:8000 \ | ||
--log-level info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
celery -A railway_django_stack worker --beat -l INFO |
Oops, something went wrong.