-
Notifications
You must be signed in to change notification settings - Fork 0
222 lines (192 loc) Β· 7.29 KB
/
deploy.yml
File metadata and controls
222 lines (192 loc) Β· 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
name: Deploy to Server
on:
push:
branches: [main]
workflow_dispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'production'
type: choice
options:
- production
- staging
force:
description: 'Force deployment even if checks fail'
required: false
default: false
type: boolean
jobs:
pre-deploy-checks:
runs-on: ubuntu-latest
outputs:
should_deploy: ${{ steps.check.outputs.should_deploy }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run basic validation
id: check
run: |
echo "Running pre-deployment checks..."
# Check if this is a valid deployment commit
if git log -1 --pretty=%B | grep -i "\[skip ci\]\|\[skip deploy\]"; then
echo "π Deployment skipped via commit message"
echo "should_deploy=false" >> $GITHUB_OUTPUT
exit 0
fi
# Check for critical files
required_files=("manage.py" "requirements.txt" "deploy.sh")
missing_files=0
for file in "${required_files[@]}"; do
if [ ! -f "$file" ]; then
echo "β Missing required file: $file"
missing_files=$((missing_files + 1))
fi
done
if [ $missing_files -gt 0 ]; then
echo "β Missing $missing_files required files"
if [ "${{ github.event.inputs.force }}" = "true" ]; then
echo "β οΈ Force deployment enabled, continuing despite missing files"
echo "should_deploy=true" >> $GITHUB_OUTPUT
else
echo "should_deploy=false" >> $GITHUB_OUTPUT
exit 1
fi
else
echo "β
All required files present"
echo "should_deploy=true" >> $GITHUB_OUTPUT
fi
deploy:
runs-on: ubuntu-latest
needs: pre-deploy-checks
if: needs.pre-deploy-checks.outputs.should_deploy == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Determine deployment environment
id: env
run: |
if [ "${{ github.event.inputs.environment }}" != "" ]; then
ENV="${{ github.event.inputs.environment }}"
elif [[ "${{ github.ref }}" == *"develop"* ]]; then
ENV="staging"
else
ENV="production"
fi
echo "ENVIRONMENT=$ENV" >> $GITHUB_OUTPUT
echo "Deploying to $ENV environment"
- name: Setup deployment variables
id: vars
run: |
if [ "${{ steps.env.outputs.ENVIRONMENT }}" = "staging" ]; then
echo "SERVER_HOST=${{ secrets.STAGING_SERVER_IP || secrets.SERVER_IP }}" >> $GITHUB_OUTPUT
echo "DEPLOY_BRANCH=develop" >> $GITHUB_OUTPUT
else
echo "SERVER_HOST=${{ secrets.SERVER_IP }}" >> $GITHUB_OUTPUT
echo "DEPLOY_BRANCH=main" >> $GITHUB_OUTPUT
fi
- name: Deploy via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ steps.vars.outputs.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 2111
script: |
set -e
echo "π Starting deployment to ${{ steps.env.outputs.ENVIRONMENT }}"
echo "Deployment triggered by: ${{ github.actor }}"
echo "Commit: ${{ github.sha }}"
echo "Branch: ${{ steps.vars.outputs.DEPLOY_BRANCH }}"
cd /home/cmsuser/cms_project/
# Create deployment log
DEPLOY_LOG="logs/deploy_$(date +%Y%m%d_%H%M%S).log"
mkdir -p logs
exec > >(tee -a "$DEPLOY_LOG") 2>&1
# Backup current deployment
echo "π¦ Creating backup..."
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/backup.tar.gz" \
--exclude=venv \
--exclude=*.pyc \
--exclude=__pycache__ \
--exclude=backups \
--exclude=logs \
--exclude=.git \
.
# Update code
echo "π Updating code from ${{ steps.vars.outputs.DEPLOY_BRANCH }} branch..."
git fetch origin
git checkout ${{ steps.vars.outputs.DEPLOY_BRANCH }}
git reset --hard origin/${{ steps.vars.outputs.DEPLOY_BRANCH }}
# Update dependencies
echo "π Updating Python dependencies..."
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
# Database migrations
echo "ποΈ Running database migrations..."
python manage.py migrate --noinput
# Static files
echo "π Collecting static files..."
python manage.py collectstatic --noinput --clear
# Security checks
echo "π Running security checks..."
python manage.py check --deploy 2>/dev/null || echo "Security check completed"
# Restart services
echo "β‘ Restarting services..."
if [ -f cms.service ]; then
sudo cp cms.service /etc/systemd/system/cms.service
fi
sudo systemctl daemon-reload
sudo systemctl restart cms
sudo systemctl reload nginx
# Health check
echo "π₯ Performing health check..."
sleep 5
if curl --unix-socket /run/cms/cms.sock -f http://localhost/health/; then
echo "β
Deployment completed successfully!"
echo "Health check passed"
else
echo "β οΈ Deployment completed but health check failed"
fi
# Cleanup old backups (keep last 10)
echo "π§Ή Cleaning up old backups..."
ls -dt backups/* | tail -n +11 | xargs rm -rf 2>/dev/null || true
echo "π Deployment process completed at $(date)"
- name: Send deployment notification
if: always()
run: |
DEPLOYMENT_STATUS="${{ job.status }}"
ENVIRONMENT="${{ steps.env.outputs.ENVIRONMENT }}"
COMMIT_SHA="${{ github.sha }}"
COMMIT_MESSAGE=$(git log -1 --pretty=%B | head -1)
echo "Deployment $DEPLOYMENT_STATUS for $ENVIRONMENT"
echo "Commit: $COMMIT_SHA"
echo "Message: $COMMIT_MESSAGE"
# This would typically send to Slack/Teams/etc
# For now, just log it
echo "π’ Notification: Deployment to $ENVIRONMENT $DEPLOYMENT_STATUS"
post-deploy:
runs-on: ubuntu-latest
needs: deploy
if: always()
steps:
- name: Generate deployment report
run: |
echo "π Deployment Summary"
echo "===================="
echo "Repository: ${{ github.repository }}"
echo "Environment: ${{ steps.env.outputs.ENVIRONMENT }}"
echo "Status: ${{ needs.deploy.result }}"
echo "Triggered by: ${{ github.actor }}"
echo "Commit: ${{ github.sha }}"
echo "Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC")"
if [ "${{ needs.deploy.result }}" = "success" ]; then
echo "β
Deployment successful"
else
echo "β Deployment failed"
echo "Check the deploy job logs for details"
fi