Skip to content

Commit 45864fe

Browse files
Merge pull request #5 from JohanCodeForFun/cicd/automatic-deployment
Cicd/automatic deployment
2 parents 2f4a4cb + 89b4e6c commit 45864fe

File tree

5 files changed

+476
-77
lines changed

5 files changed

+476
-77
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Deploy to Elastic Beanstalk
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'server/**'
9+
workflow_dispatch: # Allow manual trigger
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
name: Deploy Backend to AWS Elastic Beanstalk
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up JDK 17
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: '17'
24+
distribution: 'corretto'
25+
26+
- name: Cache Maven packages
27+
uses: actions/cache@v3
28+
with:
29+
path: ~/.m2
30+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
31+
restore-keys: ${{ runner.os }}-m2
32+
33+
- name: Build Spring Boot application
34+
run: |
35+
cd server
36+
./mvnw clean package -DskipTests
37+
38+
- name: Verify build output
39+
run: |
40+
cd server
41+
ls -la target/
42+
if [ ! -f "target/relational-data-access-complete-0.0.1-SNAPSHOT.jar" ]; then
43+
echo "Build failed - JAR file not found!"
44+
exit 1
45+
fi
46+
47+
- name: Prepare deployment package
48+
run: |
49+
cd server
50+
mkdir -p deploy-package
51+
cp target/relational-data-access-complete-0.0.1-SNAPSHOT.jar deploy-package/
52+
cp deploy/Procfile deploy-package/
53+
cp -r deploy/.ebextensions deploy-package/
54+
55+
- name: Create deployment zip
56+
run: |
57+
cd server/deploy-package
58+
zip -r ../deploy-${{ github.sha }}.zip .
59+
60+
- name: Deploy to Elastic Beanstalk
61+
uses: einaregilsson/beanstalk-deploy@v22
62+
with:
63+
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
64+
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
65+
application_name: customer-management-api # Replace with your EB app name
66+
environment_name: customer-management-api-env # Replace with your EB env name
67+
region: eu-north-1 # Replace with your AWS region
68+
version_label: ${{ github.sha }}
69+
deployment_package: server/deploy-${{ github.sha }}.zip
70+
71+
- name: Upload deployment artifact
72+
uses: actions/upload-artifact@v4
73+
if: always()
74+
with:
75+
name: deployment-package-${{ github.sha }}
76+
path: server/deploy-${{ github.sha }}.zip

server/DEPLOYMENT.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# 🚀 Deployment Guide - Customer Management API
2+
3+
This guide covers multiple deployment methods for the Spring Boot backend to AWS Elastic Beanstalk.
4+
5+
## 📋 Prerequisites
6+
7+
### Required Tools
8+
- **Java 17+**: `java -version`
9+
- **Maven**: Already included via `./mvnw`
10+
- **AWS CLI** (for CLI deployment): [Install Guide](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)
11+
- **AWS Account** with Elastic Beanstalk access
12+
13+
### AWS Setup
14+
1. **Create Elastic Beanstalk Application**:
15+
```bash
16+
# Via AWS Console or CLI
17+
aws elasticbeanstalk create-application \
18+
--application-name customer-management-api \
19+
--description "Customer Management REST API"
20+
```
21+
22+
2. **Create Environment**:
23+
```bash
24+
aws elasticbeanstalk create-environment \
25+
--application-name customer-management-api \
26+
--environment-name customer-management-api-env \
27+
--platform-arn "arn:aws:elasticbeanstalk:eu-north-1::platform/Java 17 running on 64bit Amazon Linux 2/3.x.x"
28+
```
29+
30+
## 🎯 Deployment Methods
31+
32+
### Method 1: Manual Build & Deploy (Beginner Friendly)
33+
34+
**Step 1**: Build the deployment package
35+
```bash
36+
cd server
37+
./build-and-deploy.sh
38+
```
39+
40+
**Step 2**: Upload via AWS Console
41+
1. Go to [AWS Elastic Beanstalk Console](https://console.aws.amazon.com/elasticbeanstalk/)
42+
2. Select your application → Environment
43+
3. Click "Upload and Deploy"
44+
4. Upload the generated zip file from `deploy/deploy-YYYYMMDD-HHMMSS.zip`
45+
5. Click "Deploy"
46+
47+
### Method 2: AWS CLI Deployment (Recommended for Development)
48+
49+
**Prerequisites**:
50+
```bash
51+
# Install AWS CLI
52+
aws --version
53+
54+
# Configure credentials
55+
aws configure
56+
```
57+
58+
**Deploy**:
59+
```bash
60+
cd server
61+
./deploy-aws-cli.sh [environment-name]
62+
63+
# Example
64+
./deploy-aws-cli.sh customer-management-api-env
65+
```
66+
67+
### Method 3: GitHub Actions CI/CD (Recommended for Production)
68+
69+
**Step 1**: Set up GitHub Secrets
70+
Go to your repository → Settings → Secrets and variables → Actions
71+
72+
Add these secrets:
73+
- `AWS_ACCESS_KEY_ID`: Your AWS access key
74+
- `AWS_SECRET_ACCESS_KEY`: Your AWS secret key
75+
76+
**Step 2**: Update workflow configuration
77+
Edit `.github/workflows/deploy-backend.yml`:
78+
```yaml
79+
application_name: your-eb-application-name
80+
environment_name: your-eb-environment-name
81+
region: your-aws-region
82+
```
83+
84+
**Step 3**: Deploy
85+
Push changes to `main` branch:
86+
```bash
87+
git add .
88+
git commit -m "Deploy: Update backend"
89+
git push origin main
90+
```
91+
92+
## 🔧 Configuration
93+
94+
### Environment Variables (Set in AWS EB Console)
95+
96+
**Database Configuration**:
97+
```
98+
DB_HOST=your-rds-endpoint.amazonaws.com
99+
DB_PORT=5432
100+
DB_NAME=customerdb
101+
DB_USERNAME=your-username
102+
DB_PASSWORD=your-secure-password
103+
```
104+
105+
**CORS Configuration**:
106+
```
107+
CORS_ALLOWED_ORIGINS=https://main.d123456789.amplifyapp.com,https://your-frontend-domain.com
108+
```
109+
110+
**Application Settings**:
111+
```
112+
SPRING_PROFILES_ACTIVE=production
113+
JPA_DDL_AUTO=validate
114+
LOG_LEVEL=INFO
115+
```
116+
117+
## 🏥 Health Checks
118+
119+
Elastic Beanstalk will use these endpoints:
120+
- **Health Check**: `/api/health`
121+
- **Ping Test**: `/api/ping`
122+
123+
## 📊 Monitoring
124+
125+
### CloudWatch Logs
126+
Logs are automatically streamed to CloudWatch:
127+
```bash
128+
# View logs via CLI
129+
aws logs describe-log-groups --log-group-name-prefix "/aws/elasticbeanstalk"
130+
```
131+
132+
### Application Metrics
133+
- **Health Dashboard**: AWS EB Console → Environment Health
134+
- **Application Logs**: AWS EB Console → Logs
135+
- **Monitoring**: CloudWatch metrics for requests, latency, errors
136+
137+
## 🚨 Troubleshooting
138+
139+
### Common Issues
140+
141+
**1. Health Check Failures**
142+
```bash
143+
# Check if health endpoint responds
144+
curl https://your-app-url.elasticbeanstalk.com/api/health
145+
```
146+
147+
**2. Database Connection Issues**
148+
- Verify RDS security groups allow EB connections
149+
- Check environment variables are set correctly
150+
- Ensure database exists and credentials are valid
151+
152+
**3. CORS Issues**
153+
- Update `CORS_ALLOWED_ORIGINS` with your frontend URL
154+
- Verify frontend is using correct backend URL
155+
156+
### Debugging Commands
157+
```bash
158+
# Check environment status
159+
aws elasticbeanstalk describe-environments \
160+
--application-name customer-management-api
161+
162+
# Download logs
163+
aws elasticbeanstalk request-environment-info \
164+
--environment-name customer-management-api-env \
165+
--info-type tail
166+
167+
# View recent events
168+
aws elasticbeanstalk describe-events \
169+
--environment-name customer-management-api-env
170+
```
171+
172+
## 🔄 Rollback
173+
174+
If deployment fails:
175+
```bash
176+
# List previous versions
177+
aws elasticbeanstalk describe-application-versions \
178+
--application-name customer-management-api
179+
180+
# Rollback to previous version
181+
aws elasticbeanstalk update-environment \
182+
--environment-name customer-management-api-env \
183+
--version-label previous-version-label
184+
```
185+
186+
## 🎯 Best Practices
187+
188+
1. **Test Locally First**: Always test with `./mvnw spring-boot:run`
189+
2. **Database Migrations**: Use `validate` in production, `update` only in development
190+
3. **Environment Variables**: Never commit secrets to git
191+
4. **Monitoring**: Set up CloudWatch alarms for error rates
192+
5. **Blue/Green Deployments**: Use EB's deployment policies for zero-downtime deploys
193+
194+
## 🔗 Useful Links
195+
196+
- [AWS EB Java Guide](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html)
197+
- [Spring Boot EB Guide](https://spring.io/guides/gs/spring-boot-docker/)
198+
- [EB CLI Documentation](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3.html)

server/build-and-deploy.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
# Build and Deploy Script for Elastic Beanstalk
4+
# Usage: ./build-and-deploy.sh
5+
6+
set -e # Exit on any error
7+
8+
echo "🏗️ Building Spring Boot Application..."
9+
10+
# Clean and build the application
11+
echo "Cleaning previous builds..."
12+
./mvnw clean
13+
14+
echo "Building JAR file..."
15+
./mvnw package -DskipTests
16+
17+
# Check if build was successful
18+
if [ ! -f "target/relational-data-access-complete-0.0.1-SNAPSHOT.jar" ]; then
19+
echo "❌ Build failed - JAR file not found!"
20+
exit 1
21+
fi
22+
23+
echo "✅ Build successful!"
24+
25+
# Prepare deployment directory
26+
echo "📦 Preparing deployment package..."
27+
28+
# Clean deploy directory
29+
rm -rf deploy/temp
30+
mkdir -p deploy/temp
31+
32+
# Copy JAR file to deploy directory
33+
cp target/relational-data-access-complete-0.0.1-SNAPSHOT.jar deploy/temp/
34+
35+
# Copy Procfile and .ebextensions
36+
cp deploy/Procfile deploy/temp/
37+
cp -r deploy/.ebextensions deploy/temp/
38+
39+
# Create deployment zip
40+
echo "Creating deployment zip..."
41+
cd deploy/temp
42+
zip -r ../deploy-$(date +%Y%m%d-%H%M%S).zip .
43+
cd ../..
44+
45+
# Clean up temp directory
46+
rm -rf deploy/temp
47+
48+
echo "✅ Deployment package created: deploy/deploy-$(date +%Y%m%d-%H%M%S).zip"
49+
echo ""
50+
echo "📋 Next steps:"
51+
echo "1. Go to AWS Elastic Beanstalk Console"
52+
echo "2. Select your application environment"
53+
echo "3. Click 'Upload and Deploy'"
54+
echo "4. Upload the zip file: deploy/deploy-$(date +%Y%m%d-%H%M%S).zip"
55+
echo "5. Deploy!"

0 commit comments

Comments
 (0)