Skip to content

Commit

Permalink
feat(ci): add workflow for deploying to DigitalOcean Kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriana618 committed Jul 14, 2024
1 parent fab08d2 commit 8b83c8e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/deploy-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy API

on:
push:

jobs:
build:
runs-on: ubuntu-latest
name: build-main-api
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Login to Docker Hub
run: echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin

- name: Build, tag, and push image to Docker Hub
run: |
docker build .. --file docker-conf/Dockerfile-django-api --tag ${{ secrets.DOCKER_REGISTRY }}/algova-django-api:${GITHUB_SHA::7}
docker push ${{ secrets.DOCKER_REGISTRY }}/algova-django-api:latest
deploy:
needs: [build]
runs-on: ubuntu-latest
name: deploy-main-api
steps:
- name: Checkout main
uses: actions/checkout@v2

- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

- name: Deploy to DigitalOcean Kubernetes
run: |
kubectl apply -f kubernetes/api-deployment.yaml --namespace=default
kubectl set image deployment/algova-django-api algova-django-api=${{ secrets.DOCKER_REGISTRY }}/algova-django-api:${GITHUB_SHA::7} --namespace=default
kubectl rollout status deployment/algova-django-api --namespace=default
kubectl wait --for=condition=available --timeout=300s --namespace=default deployment/algova-django-api
kubectl get pods -l app=algova-django-api --namespace=default
sleep 10 # Wait for previous pod to terminate
kubectl exec --namespace=default \
$(kubectl get pod -l app=algova-django-api --field-selector=status.phase==Running -o jsonpath="{.items[0].metadata.name}" --namespace=default) \
-- python manage.py makemigrations
kubectl exec --namespace=default \
$(kubectl get pod -l app=algova-django-api --field-selector=status.phase==Running -o jsonpath="{.items[0].metadata.name}" --namespace=default) \
-- python manage.py migrate
2 changes: 1 addition & 1 deletion demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["0.0.0.0","127.0.0.1"]


# Application definition
Expand Down
44 changes: 44 additions & 0 deletions docker-conf/Dockerfile-django-api
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Build Stage
FROM python:3.9-slim as builder

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

WORKDIR /build

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
build-essential \
unixodbc-dev \
default-libmysqlclient-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

COPY requirements ./requirements

RUN pip install --no-cache-dir --target=/build/deps -r requirements/prod.txt


# Final Stage
FROM python:3.9-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

WORKDIR /home/algova

# Copy installed dependencies from the builder stage
COPY --from=builder /build/deps /usr/local/lib/python3.9/site-packages
COPY --from=builder /build/deps/bin /usr/local/bin

COPY . .

# Runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
unixodbc-dev \
default-libmysqlclient-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

EXPOSE 8000
26 changes: 26 additions & 0 deletions kubernetes/api-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: default
name: algova-django-api
spec:
selector:
matchLabels:
app: algova-django-api
template:
metadata:
labels:
app: algova-django-api
spec:
containers:
- name: algova-django-api
image: algova/algova-django-api:latest
imagePullPolicy: Always
command: ['gunicorn', 'config.wsgi', '--bind=0.0.0.0:8000']
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

4 changes: 4 additions & 0 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
django==4.2
django-environ==0.10.0
djangorestframework==3.14.0
gunicorn

0 comments on commit 8b83c8e

Please sign in to comment.