Skip to content

Commit a0edb87

Browse files
committed
added cd.yml, nginx.conf and updated dockerfiles
1 parent 725b17c commit a0edb87

File tree

4 files changed

+197
-4
lines changed

4 files changed

+197
-4
lines changed

.github/workflows/cd.yml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: CD - Deploy to AWS EC2
2+
3+
on:
4+
workflow_run:
5+
workflows: ["CI - Build and Push Docker Images"]
6+
types:
7+
- completed
8+
branches: [ main ]
9+
workflow_dispatch:
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
20+
- name: Configure AWS credentials
21+
uses: aws-actions/configure-aws-credentials@v1
22+
with:
23+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
24+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
25+
aws-region: ${{ secrets.AWS_REGION }}
26+
27+
- name: Setup SSH to EC2 instance
28+
uses: appleboy/ssh-action@master
29+
with:
30+
host: ${{ secrets.EC2_HOST }}
31+
username: ${{ secrets.EC2_USERNAME }}
32+
key: ${{ secrets.EC2_SSH_KEY }}
33+
script: |
34+
# Create docker-compose file if not exists
35+
mkdir -p ~/app
36+
37+
# Create or update docker-compose.yml
38+
cat > ~/app/docker-compose.yml << 'EOL'
39+
version: '3.8'
40+
41+
services:
42+
postgres:
43+
image: postgres:13
44+
container_name: postgres
45+
restart: always
46+
environment:
47+
POSTGRES_USER: postgres
48+
POSTGRES_PASSWORD: postgres
49+
POSTGRES_DB: orderdb
50+
volumes:
51+
- postgres-data:/var/lib/postgresql/data
52+
networks:
53+
- app-network
54+
55+
order-api:
56+
image: ${{ secrets.DOCKERHUB_USERNAME }}/order-api:latest
57+
container_name: order-api
58+
restart: always
59+
depends_on:
60+
- postgres
61+
environment:
62+
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/orderdb
63+
SPRING_DATASOURCE_USERNAME: postgres
64+
SPRING_DATASOURCE_PASSWORD: postgres
65+
ports:
66+
- "8080:8080"
67+
networks:
68+
- app-network
69+
70+
order-ui:
71+
image: ${{ secrets.DOCKERHUB_USERNAME }}/order-ui:latest
72+
container_name: order-ui
73+
restart: always
74+
depends_on:
75+
- order-api
76+
environment:
77+
REACT_APP_API_URL: http://${{ secrets.EC2_HOST }}:8080
78+
ports:
79+
- "80:80"
80+
networks:
81+
- app-network
82+
83+
prometheus:
84+
image: prom/prometheus:v2.40.0
85+
container_name: prometheus
86+
restart: always
87+
volumes:
88+
- ~/app/prometheus.yml:/etc/prometheus/prometheus.yml
89+
- prometheus-data:/prometheus
90+
ports:
91+
- "9090:9090"
92+
networks:
93+
- app-network
94+
95+
grafana:
96+
image: grafana/grafana:9.2.4
97+
container_name: grafana
98+
restart: always
99+
depends_on:
100+
- prometheus
101+
volumes:
102+
- grafana-data:/var/lib/grafana
103+
ports:
104+
- "3000:3000"
105+
networks:
106+
- app-network
107+
108+
volumes:
109+
postgres-data:
110+
prometheus-data:
111+
grafana-data:
112+
113+
networks:
114+
app-network:
115+
driver: bridge
116+
EOL
117+
118+
# Create prometheus.yml configuration
119+
cat > ~/app/prometheus.yml << 'EOL'
120+
global:
121+
scrape_interval: 15s
122+
123+
scrape_configs:
124+
- job_name: 'prometheus'
125+
static_configs:
126+
- targets: ['localhost:9090']
127+
128+
- job_name: 'spring-actuator'
129+
metrics_path: '/actuator/prometheus'
130+
static_configs:
131+
- targets: ['order-api:8080']
132+
EOL
133+
134+
# Pull the latest images
135+
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/order-api:latest
136+
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/order-ui:latest
137+
138+
# Stop and remove existing containers
139+
cd ~/app
140+
docker-compose down || true
141+
142+
# Start new containers
143+
docker-compose up -d
144+
145+
# Clean up unused images
146+
docker image prune -af

order-api/Dockerfile

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
FROM maven:3.8-openjdk-17-slim AS build
2+
WORKDIR /app
3+
COPY pom.xml .
4+
COPY .mvn/ .mvn/
5+
COPY mvnw .
6+
RUN chmod +x mvnw
7+
# Download dependencies first (for better caching)
8+
RUN ./mvnw dependency:go-offline -B
9+
10+
# Copy source and build
11+
COPY src/ src/
12+
RUN ./mvnw package -DskipTests
13+
14+
################################################################################3
15+
116
# Use a lightweight JRE for runtime
217
FROM eclipse-temurin:17-jre-alpine
318

@@ -7,11 +22,19 @@ WORKDIR /app
722
# Install jq
823
RUN apk --no-cache add jq
924

10-
# Copy the built jar file
11-
COPY target/order-api-0.0.1-SNAPSHOT.jar app.jar
25+
# Create a non-root user
26+
RUN addgroup -S spring && adduser -S spring -G spring
27+
USER spring:spring
28+
29+
# Copy the jar file from the build stage
30+
COPY --from=build /app/target/*.jar app.jar
1231

1332
# Expose the application port
1433
EXPOSE 8080
1534

35+
# Set healthcheck
36+
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:8080/actuator/health || exit 1
37+
38+
1639
# Run the application
17-
ENTRYPOINT ["java", "-jar", "app.jar"]
40+
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

order-ui/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ WORKDIR /app
88
COPY package.json package-lock.json ./
99

1010
# Install dependencies
11-
RUN npm install
11+
RUN npm ci
1212

1313
# Copy the entire project
1414
COPY . .
@@ -21,9 +21,13 @@ FROM nginx:alpine
2121

2222
# Copy the built files to Nginx's HTML directory
2323
COPY --from=builder /app/build /usr/share/nginx/html
24+
COPY --from=builder /app/nginx.conf /etc/nginx/conf.d/default.conf
2425

2526
# Expose Nginx default port
2627
EXPOSE 80
2728

29+
# Health check
30+
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:80 || exit 1
31+
2832
# Start Nginx
2933
CMD ["nginx", "-g", "daemon off;"]

order-ui/nginx.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
location / {
6+
root /usr/share/nginx/html;
7+
index index.html;
8+
try_files $uri $uri/ /index.html;
9+
}
10+
11+
# Proxy API requests to the backend
12+
location /api/ {
13+
proxy_pass http://order-api:8080/;
14+
proxy_http_version 1.1;
15+
proxy_set_header Upgrade $http_upgrade;
16+
proxy_set_header Connection 'upgrade';
17+
proxy_set_header Host $host;
18+
proxy_cache_bypass $http_upgrade;
19+
}
20+
}

0 commit comments

Comments
 (0)