1+ name : Django CI/CD
2+
3+ on :
4+ push :
5+ branches : [ "main" ]
6+ pull_request :
7+ branches : [ "main" ]
8+
9+ jobs :
10+ django_ci :
11+ runs-on : ubuntu-latest
12+ strategy :
13+ matrix :
14+ python-version : [3.12]
15+
16+ steps :
17+ - uses : actions/checkout@v4
18+ - name : Set up Python ${{ matrix.python-version }}
19+ uses : actions/setup-python@v4
20+ with :
21+ python-version : ${{ matrix.python-version }}
22+
23+ - name : Install Dependencies
24+ run : |
25+ python -m pip install --upgrade pip
26+ pip install -r requirements.txt
27+
28+ - name : Run Tests
29+ run : |
30+ python manage.py test
31+
32+ deploy_django :
33+ runs-on : ubuntu-latest
34+ needs : django_ci
35+
36+ steps :
37+ - name : Checkout repository
38+ uses : actions/checkout@v4
39+
40+ - name : Deploy Django via SSH
41+ uses : appleboy/ssh-action@v0.1.8
42+ with :
43+ host : ${{ secrets.SERVER_HOST }}
44+ username : ${{ secrets.SERVER_USER }}
45+ key : ${{ secrets.SERVER_SSH_KEY }}
46+ script : |
47+ set -e
48+
49+ REPO_DIR="CerifyNow"
50+
51+ # Clone or update the repository
52+ if [ ! -d "$REPO_DIR" ]; then
53+ echo "Cloning repository into $REPO_DIR..."
54+ git clone https://github.com/CerifyNow/CerifyNow-API.git $REPO_DIR
55+ else
56+ echo "Repository already exists, pulling the latest changes..."
57+ cd $REPO_DIR
58+ git pull origin main
59+ cd ..
60+ fi
61+
62+ # Check if the repo directory exists after clone/pull
63+ if [ ! -d "$REPO_DIR" ]; then
64+ echo "Error: The directory '$REPO_DIR' does not exist after cloning or pulling the repository."
65+ exit 1
66+ fi
67+
68+ # Navigate into the repository directory
69+ echo "Navigating into $REPO_DIR directory..."
70+ cd $REPO_DIR
71+
72+ # Create virtual environment if it doesn't exist
73+ if [ ! -d "venv" ]; then
74+ echo "Creating virtual environment..."
75+ python3 -m venv venv
76+ fi
77+
78+ # Activate the virtual environment
79+ source venv/bin/activate
80+
81+ # Install dependencies
82+ echo "Installing dependencies..."
83+ pip install --upgrade pip
84+ pip install -r requirements.txt
85+
86+ # Run Django migrations
87+ echo "Running migrations..."
88+ python3 manage.py makemigrations institutions
89+ python3 manage.py makemigrations users
90+ python3 manage.py migrate
91+
92+ # Collect static files
93+ echo "Collecting static files..."
94+ python manage.py collectstatic --noinput
95+
96+ # Restart Django via systemd
97+ echo "Restarting Django service..."
98+ sudo systemctl restart django.service
99+
100+ echo "Successfully deployed!"
0 commit comments