-
Notifications
You must be signed in to change notification settings - Fork 0
101 lines (85 loc) · 3.1 KB
/
deploy-backend.yml
File metadata and controls
101 lines (85 loc) · 3.1 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
name: 🚀 Deploy Backend to EC2
on:
push:
branches: [ main ]
paths:
- 'src/**'
- 'pom.xml'
- 'task-tracker-api/**'
- '.github/workflows/deploy-backend.yml'
pull_request:
branches: [ main ]
paths:
- 'src/**'
- 'pom.xml'
- 'task-tracker-api/**'
- '.github/workflows/deploy-backend.yml'
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 📦 Checkout code
uses: actions/checkout@v4
- name: ☕ Setup Java 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: maven
- name: 🗃️ Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: 🔨 Build with Maven
run: |
mvn clean package -DskipTests
env:
MAVEN_OPTS: "-Xmx2g"
- name: 🧪 Run Tests
if: github.event_name == 'pull_request'
run: |
cd task-tracker-api
mvn test
- name: 🚀 Deploy to EC2
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
script: |
echo "🔄 Starting deployment..."
APP_DIR=~/task-tracker/task-tracker-api
JAR_NAME=task-tracker-api-0.0.1-SNAPSHOT.jar
echo "🛑 Stopping service..."
sudo systemctl stop task-tracker
echo "💾 Creating backup..."
cd $APP_DIR
if [ -f target/$JAR_NAME ]; then
cp target/$JAR_NAME target/$JAR_NAME.backup
fi
echo "▶️ Starting service..."
sudo systemctl start task-tracker
echo "⏳ Waiting for application to start..."
sleep 15
echo "🏥 Running health check..."
if curl -f http://localhost:8080/actuator/health > /dev/null 2>&1; then
echo "✅ Deployment successful! Application is healthy."
else
echo "❌ Health check failed! Attempting rollback..."
if [ -f target/$JAR_NAME.backup ]; then
cp target/$JAR_NAME.backup target/$JAR_NAME
sudo systemctl restart task-tracker
echo "🔄 Rolled back to previous version."
fi
exit 1
fi
- name: 📢 Deployment Complete
if: success() && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
run: |
echo "✅ Deployment to EC2 completed successfully!"
echo "🌐 Application URL: https://tasktracker.api.devendra.indevs.in"