Skip to content

Commit a2f3210

Browse files
committed
added docker based deployment
1 parent 4c71adc commit a2f3210

File tree

3 files changed

+157
-28
lines changed

3 files changed

+157
-28
lines changed

.github/workflows/deploy.yml

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
name: Deploy to server
1+
# name: Deploy to server
22

3-
on:
4-
push:
5-
branches:
6-
- master # Trigger the workflow on push or pull request to the master branch
3+
# on:
4+
# push:
5+
# branches:
6+
# - master # Trigger the workflow on push or pull request to the master branch
77

8-
jobs:
9-
deploy:
10-
runs-on: ubuntu-latest
8+
# jobs:
9+
# deploy:
10+
# runs-on: ubuntu-latest
1111

12-
steps:
13-
- name: Checkout code
14-
uses: actions/checkout@v2
12+
# steps:
13+
# - name: Checkout code
14+
# uses: actions/checkout@v2
1515

16-
- name: Execute remote ssh commands to deploy
17-
uses: appleboy/ssh-action@master
18-
with:
19-
host: ${{ secrets.SERVER_HOST }}
20-
username: ${{ secrets.SERVER_USERNAME }}
21-
key: ${{ secrets.SSH_KEY }}
22-
script: |
23-
cd LinkedinAI
24-
git reset --hard
25-
git pull
26-
sed -i "s/SECRET_KEY = .*/SECRET_KEY = '${{ secrets.SECRET_KEY }}'/g" LinkedInAi/settings.py
27-
sed -i "s/DEBUG = .*/DEBUG = False/g" LinkedInAi/settings.py
28-
sed -i "s/ALLOWED_HOSTS = .*/ALLOWED_HOSTS = ['linkedinai.pratikpathak.com']/g" LinkedInAi/settings.py
29-
.venv/bin/python manage.py makemigrations # create new migrations based on the changes you made to your models
30-
.venv/bin/python manage.py migrate # apply and unapply migrations
31-
.venv/bin/python manage.py collectstatic --noinput
32-
sudo systemctl restart django
16+
# - name: Execute remote ssh commands to deploy
17+
# uses: appleboy/ssh-action@master
18+
# with:
19+
# host: ${{ secrets.SERVER_HOST }}
20+
# username: ${{ secrets.SERVER_USERNAME }}
21+
# key: ${{ secrets.SSH_KEY }}
22+
# script: |
23+
# cd LinkedinAI
24+
# git reset --hard
25+
# git pull
26+
# sed -i "s/SECRET_KEY = .*/SECRET_KEY = '${{ secrets.SECRET_KEY }}'/g" LinkedInAi/settings.py
27+
# sed -i "s/DEBUG = .*/DEBUG = False/g" LinkedInAi/settings.py
28+
# sed -i "s/ALLOWED_HOSTS = .*/ALLOWED_HOSTS = ['linkedinai.pratikpathak.com']/g" LinkedInAi/settings.py
29+
# .venv/bin/python manage.py makemigrations # create new migrations based on the changes you made to your models
30+
# .venv/bin/python manage.py migrate # apply and unapply migrations
31+
# .venv/bin/python manage.py collectstatic --noinput
32+
# sudo systemctl restart django

.github/workflows/dockerDeploy.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Deploy Docker Container
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build-and-deploy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: Modify settings.py
16+
run: |
17+
sed -i "s/SECRET_KEY = .*/SECRET_KEY = '${{ secrets.SECRET_KEY }}'/g" LinkedInAi/settings.py
18+
sed -i "s/DEBUG = .*/DEBUG = False/g" LinkedInAi/settings.py
19+
sed -i "s/ALLOWED_HOSTS = .*/ALLOWED_HOSTS = ['linkedinai.pratikpathak.com']/g" LinkedInAi/settings.py
20+
21+
- name: Create .env file
22+
run: echo "${{ secrets.ENVIRONMENTAL_VARIABLES }}" > .env
23+
24+
- name: Build Docker image
25+
run: docker build . -t linkedrite:latest
26+
27+
- name: Save Docker image
28+
run: docker save linkedrite:latest > linkedrite.tar
29+
30+
- name: Remove existing Docker image file on server
31+
uses: appleboy/ssh-action@master
32+
with:
33+
host: ${{ secrets.SERVER_HOST }}
34+
username: ${{ secrets.SERVER_USERNAME }}
35+
key: ${{ secrets.SSH_KEY }}
36+
port: ${{ secrets.SSH_PORT }}
37+
script: |
38+
rm -f ~/home/linkedrite.tar
39+
40+
- name: Copy Docker image to server
41+
uses: appleboy/scp-action@master
42+
with:
43+
host: ${{ secrets.SERVER_HOST }}
44+
username: ${{ secrets.SERVER_USERNAME }}
45+
key: ${{ secrets.SSH_KEY }}
46+
port: ${{ secrets.SSH_PORT }}
47+
source: "linkedrite.tar"
48+
target: "~/home/"
49+
50+
- name: Load Docker image and run container on server
51+
uses: appleboy/ssh-action@master
52+
with:
53+
host: ${{ secrets.SERVER_HOST }}
54+
username: ${{ secrets.SERVER_USERNAME }}
55+
key: ${{ secrets.SSH_KEY }}
56+
port: ${{ secrets.SSH_PORT }}
57+
script: |
58+
# Load the Docker image, overwriting any existing image with the same name
59+
docker load < ~/home/linkedrite.tar
60+
61+
# Check if the container is running, stop and remove it if it is
62+
if [ $(docker ps -q -f name=linkedrite) ]; then
63+
docker stop linkedrite
64+
docker rm linkedrite
65+
# Check if the container exists (but is stopped), remove it if it does
66+
elif [ $(docker ps -aq -f status=exited -f name=linkedrite) ]; then
67+
docker rm linkedrite
68+
fi
69+
70+
# Run the new container
71+
docker run -d --name linkedrite -p 8000:8000 linkedrite:latest

Dockerfile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.8-slim as builder
3+
4+
# Set environment variables:
5+
# - PYTHONDONTWRITEBYTECODE: Prevents Python from writing pyc files to disk (equivalent to python -B option)
6+
# - PYTHONUNBUFFERED: Prevents Python from buffering stdout and stderr
7+
ENV PYTHONDONTWRITEBYTECODE 1
8+
ENV PYTHONUNBUFFERED 1
9+
10+
# Install system dependencies required for Python packages with native extensions
11+
RUN apt-get update && apt-get install -y --no-install-recommends \
12+
gcc \
13+
libc-dev \
14+
libffi-dev \
15+
libssl-dev \
16+
cargo \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
# Install Poetry for Python package management
20+
RUN pip install --upgrade pip && pip install poetry
21+
22+
# Set the working directory in the container
23+
WORKDIR /app
24+
25+
# Copy the Python dependency files to the container
26+
COPY pyproject.toml poetry.lock* /app/
27+
28+
# Configure Poetry:
29+
# - virtualenvs.create false: Prevents Poetry from creating a virtual environment inside the Docker container
30+
# Install runtime dependencies only (no development dependencies)
31+
RUN poetry config virtualenvs.create false \
32+
&& poetry install --no-interaction --no-ansi --no-dev --no-root
33+
34+
# Start a new build stage for the final image
35+
FROM python:3.8-slim as final
36+
37+
# Copy installed dependencies from the builder stage
38+
COPY --from=builder /usr/local/lib/python3.8/site-packages /usr/local/lib/python3.8/site-packages
39+
COPY --from=builder /usr/local/bin /usr/local/bin
40+
41+
# Set the working directory in the container
42+
WORKDIR /app
43+
44+
# Copy the Django project files to the container
45+
COPY . /app/
46+
47+
RUN python manage.py makemigrations
48+
RUN python manage.py migrate
49+
50+
# Collect static files
51+
RUN python manage.py collectstatic --noinput
52+
53+
54+
# Expose the port the app runs on
55+
EXPOSE 8000
56+
57+
# Use gunicorn as the entry point to serve the app. Adjust the number of workers as necessary.
58+
CMD ["gunicorn", "LinkedInAi.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"]

0 commit comments

Comments
 (0)