Skip to content

Commit

Permalink
feature(deploy): Implement automatic deployment (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
soohyunme authored Jun 13, 2024
1 parent 8a78948 commit 9d9a884
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/deploy-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy development server

on:
push:
branches:
- dev
workflow_dispatch:

jobs:
build:
runs-on: dev

steps:
- name: Check out the repository
uses: actions/checkout@v2
with:
ref: dev

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

- name: Set up pseudolab config
run: |
sh -c "${{ secrets.RUN_SCRIPT }}"
- name: Shut down Docker Compose
run: |
docker compose -f docker-compose.yaml down
- name: Build and push Docker images
env:
DB_ROOT: ${{ secrets.DB_ROOT }}
DB_ROOT_PASSWORD: ${{ secrets.DB_ROOT_PASSWORD }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
run: |
docker compose -f docker-compose.yaml build
- name: Run Docker Compose
env:
DB_ROOT: ${{ secrets.DB_ROOT }}
DB_ROOT_PASSWORD: ${{ secrets.DB_ROOT_PASSWORD }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
run: |
docker compose -f docker-compose.yaml up -d
48 changes: 48 additions & 0 deletions .github/workflows/deploy-main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy development server

on:
push:
branches:
- main
workflow_dispatch:

jobs:
build:
runs-on: main

steps:
- name: Check out the repository
uses: actions/checkout@v2
with:
ref: main

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

- name: Set up pseudolab config
run: |
sh -c "${{ secrets.RUN_SCRIPT }}"
- name: Shut down Docker Compose
run: |
docker compose -f docker-compose.yaml down
- name: Build and push Docker images
env:
DB_ROOT: ${{ secrets.DB_ROOT }}
DB_ROOT_PASSWORD: ${{ secrets.DB_ROOT_PASSWORD }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
run: |
docker compose -f docker-compose.yaml build
- name: Run Docker Compose
env:
DB_ROOT: ${{ secrets.DB_ROOT }}
DB_ROOT_PASSWORD: ${{ secrets.DB_ROOT_PASSWORD }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
run: |
docker compose -f docker-compose.yaml up -d
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:3.11-slim

WORKDIR /workspace

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install Poetry for managing dependencies
RUN pip install poetry

# Copy only the dependencies specification to the container
COPY pyproject.toml poetry.lock* .

# Install project dependencies but avoid installing the project package itself
RUN poetry config virtualenvs.create false && \
poetry install --no-interaction --no-ansi --no-dev

# Copy the rest of the project files to the container
COPY . .

WORKDIR /workspace/app

# The default command to run the bash
CMD ["bash"]
21 changes: 21 additions & 0 deletions app/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ def run_migrations_online() -> None:
asyncio.run(run_async_migrations())


async def run_async_migrations() -> None:
"""In this scenario we need to create an Engine
and associate a connection with the context.
"""
import os

# Import URL from environment variable.
database_url = os.getenv('DB_URL', config.get_main_option("sqlalchemy.url"))

connectable = async_engine_from_config(
{**config.get_section(config.config_ini_section), "sqlalchemy.url": database_url},
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)

await connectable.dispose()


if context.is_offline_mode():
run_migrations_offline()
else:
Expand Down
38 changes: 38 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
network: host
depends_on:
db:
condition: service_healthy
environment:
DB_URL: mysql+aiomysql://${DB_ROOT}:${DB_ROOT_PASSWORD}@db:3306/${DB_DATABASE}
ports:
- "8000:8000"
command: >
/bin/sh -c "
alembic upgrade head;
python start.py
"
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
volumes:
- mysql-data:/var/lib/mysql
healthcheck:
test: ["CMD-SHELL", "mysql -u ${DB_USER} -p${DB_PASSWORD} -e 'SHOW DATABASES LIKE \"pseudolab\";'"]
interval: 10s
timeout: 5s
retries: 5

volumes:
mysql-data:

0 comments on commit 9d9a884

Please sign in to comment.