-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ci): add workflow for deploying to DigitalOcean Kubernetes
- Loading branch information
1 parent
fab08d2
commit 8b83c8e
Showing
6 changed files
with
123 additions
and
4 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,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 |
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
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 @@ | ||
# 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 |
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,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" |
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
django==4.2 | ||
django-environ==0.10.0 | ||
djangorestframework==3.14.0 | ||
gunicorn |