Skip to content

Automated Deployment #33

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

Merged
merged 16 commits into from
Mar 19, 2025
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
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
__pycache__/


# Django stuff
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
static

# Environment
.env
.env*
.venv
venv/
62 changes: 62 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Deploy to EC2

on:
push:
branches:
- dev

jobs:
deploy:
runs-on: ubuntu-latest

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

- name: Set environment variables
run: |
if [[ "${{ github.ref }}" == "refs/head/main" ]]; then
echo "DEPLOY_DIR=/data/MusicCPRProd" >> $GITHUB_ENV
echo "IMAGE_NAME=backend:prod" >> $GITHUB_ENV
echo "CONTAINER_NAME=backend-prod" >> $GITHUB_ENV
echo "HOST_PORT=8001" >> $GITHUB_ENV
else
echo "DEPLOY_DIR=/data/MusicCPRDev" >> $GITHUB_ENV
echo "IMAGE_NAME=backend:dev" >> $GITHUB_ENV
echo "CONTAINER_NAME=backend-dev" >> $GITHUB_ENV
echo "HOST_PORT=8000" >> $GITHUB_ENV
fi
- name: Set up SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts

- name: Deploy to EC2
run: |
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
set -e
echo "Deploying to ${{ env.DEPLOY_DIR }}"

cd ${{ env.DEPLOY_DIR }}

echo "Pulling latest changes..."
git pull origin ${{ github.ref_name }}

echo "Stopping and removing old container"
docker stop ${{ env.CONTAINER_NAME }} || true
docker rm ${{ env.CONTAINER_NAME }} || true

echo "Removing old image"
docker rmi ${{ env.IMAGE_NAME }} || true

echo "Building new image..."
docker build -t ${{ env.IMAGE_NAME }} .

echo "Starting new container..."
docker run -d --name ${{ env.CONTAINER_NAME }} \
-p ${{ env.HOST_PORT }}:8000 \
-v ./.env:/app/.env \
${{ env.IMAGE_NAME }}
EOF
35 changes: 35 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM python:3.10-slim AS builder

RUN mkdir /app

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

COPY requirements.txt /app/
COPY requirements/ /app/requirements/
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r /app/requirements/production.txt

FROM python:3.10-slim

RUN useradd -m -r appuser && \
mkdir /app && \
chown -R appuser /app

COPY --from=builder /usr/local/lib/python3.10/site-packages/ /usr/local/lib/python3.10/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin

WORKDIR /app

COPY --chown=appuser:appuser . .

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN chmod +x /app/entrypoint.prod.sh

EXPOSE 8000

CMD ["/app/entrypoint.prod.sh"]
2 changes: 1 addition & 1 deletion config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"default": env.db("DATABASE_URL", default="sqlite:///db.sqlite"),
}
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=["localhost", "0.0.0.0", "127.0.0.1"])
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=["localhost", "0.0.0.0", "127.0.0.1", "dev-api.musiccpr.org"])

# CACHES
# ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
SECRET_KEY = env("DJANGO_SECRET_KEY")
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["https://tele.band"])
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["https://tele.band", "dev-api.musiccpr.org"])

# DATABASES
# ------------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

export DJANGO_READ_DOT_ENV_FILE=True
export DJANGO_SETTINGS_MODULE=config.settings.production

python manage.py collectstatic --noinput
python manage.py migrate --noinput
python -m gunicorn config.asgi:application \
--bind 0.0.0.0:8000 \
--workers 4 \
-k uvicorn.workers.UvicornWorker
Loading