Some checks failed
Deploy Nginx Config to Ubuntu Server / deploy (push) Has been cancelled
121 lines
4.2 KiB
YAML
121 lines
4.2 KiB
YAML
# Ubuntu 서버 nginx 설정 자동 배포 워크플로우 (Gitea Actions)
|
|
#
|
|
# 성공 조건:
|
|
# - Gitea Actions 활성화
|
|
# - admin 사용자 sudo NOPASSWD 설정 완료
|
|
# - nginx 서비스 실행 중
|
|
#
|
|
# 실패 조건:
|
|
# - sudo 권한 없음
|
|
# - nginx 설정 문법 오류
|
|
# - 시스템 서비스 오류
|
|
#
|
|
# 주요 작업:
|
|
# 1. 소스 코드 체크아웃
|
|
# 2. nginx 설정 백업
|
|
# 3. 새 설정 적용 (/etc/nginx/sites-available/default)
|
|
# 4. nginx 설정 검증 (nginx -t)
|
|
# 5. nginx 서비스 reload
|
|
# 6. 배포 결과 확인
|
|
|
|
name: Deploy Nginx Config to Ubuntu Server
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
pull_request:
|
|
branches:
|
|
- main
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: self-hosted
|
|
|
|
steps:
|
|
- name: Checkout source code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Backup current nginx config
|
|
run: |
|
|
echo "📋 Creating backup of current nginx configuration..."
|
|
# nginx system config
|
|
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup.$(date +%Y%m%d_%H%M%S)
|
|
# nginx server config
|
|
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup.$(date +%Y%m%d_%H%M%S)
|
|
echo "✅ Backup created successfully"
|
|
|
|
- name: Validate & apply new nginx configuration
|
|
run: |
|
|
echo "🔍 Validating new nginx configuration syntax..."
|
|
# Backup current config and test with new one
|
|
|
|
# nginx system config
|
|
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.temp.backup
|
|
sudo cp nginx.conf /etc/nginx/nginx.conf
|
|
sudo nginx -t || {
|
|
echo "❌ New nginx system configuration has syntax errors!"
|
|
# Restore backup
|
|
sudo cp /etc/nginx/nginx.conf.temp.backup /etc/nginx/nginx.conf
|
|
exit 1
|
|
}
|
|
echo "✅ New configuration syntax is valid"
|
|
|
|
# nginx server config
|
|
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.temp.backup
|
|
sudo cp sites-available/default /etc/nginx/sites-available/default
|
|
sudo nginx -t || {
|
|
echo "❌ New nginx server configuration has syntax errors!"
|
|
# Restore backup
|
|
sudo cp /etc/nginx/sites-available/default.temp.backup /etc/nginx/sites-available/default
|
|
exit 1
|
|
}
|
|
echo "✅ New configuration syntax is valid"
|
|
|
|
- name: Test nginx configuration
|
|
run: |
|
|
echo "🔍 Testing nginx configuration with current setup..."
|
|
sudo nginx -t
|
|
echo "✅ Configuration test passed"
|
|
|
|
- name: Reload nginx service
|
|
run: |
|
|
echo "🔄 Reloading nginx service..."
|
|
sudo systemctl reload nginx
|
|
echo "✅ Nginx service reloaded successfully"
|
|
|
|
- name: Verify deployment
|
|
run: |
|
|
echo "🚀 Verifying deployment status..."
|
|
# Check nginx service status
|
|
sudo systemctl is-active nginx || {
|
|
echo "❌ Nginx service is not running!"
|
|
# Rollback on failure
|
|
echo "🔄 Rolling back to previous configuration..."
|
|
BACKUP_FILE=$(ls -t /etc/nginx/sites-available/default.backup.* 2>/dev/null | head -1)
|
|
if [ -n "$BACKUP_FILE" ]; then
|
|
sudo cp "$BACKUP_FILE" /etc/nginx/sites-available/default
|
|
sudo systemctl reload nginx
|
|
echo "✅ Rollback completed"
|
|
fi
|
|
exit 1
|
|
}
|
|
|
|
# Test HTTP response
|
|
curl -f -s -I http://localhost:80 > /dev/null || {
|
|
echo "⚠️ Warning: HTTP response test failed, but nginx is running"
|
|
}
|
|
|
|
echo "✅ Deployment completed successfully!"
|
|
echo "📊 Deployment Summary:"
|
|
echo " - Configuration: Applied"
|
|
echo " - Service: Running"
|
|
echo " - Status: Success"
|
|
|
|
- name: Cleanup old backups
|
|
run: |
|
|
echo "🧹 Cleaning up old backup files (keeping last 5)..."
|
|
cd /etc/nginx/sites-available/
|
|
sudo ls -t default.backup.* 2>/dev/null | tail -n +6 | sudo xargs rm -f
|
|
echo "✅ Cleanup completed"
|