forked from sukreethi/JavaWebCalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
101 lines (82 loc) · 3.27 KB
/
Copy pathdeploy.yml
File metadata and controls
101 lines (82 loc) · 3.27 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: Build Maven Artifact, Push Docker Image to ECR, and Deploy to EC2
on:
push:
branches:
- master # Run only when code is pushed to master branch
jobs:
build:
runs-on: ubuntu-latest
steps:
# 1. Checkout repo
- name: Checkout repository
uses: actions/checkout@v3
# 2. Set up Java (for Maven)
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
# 3. Build artifact with Maven (WAR or JAR)
- name: Build with Maven
run: mvn clean package -DskipTests
# 4. Find and rename artifact (supports both .jar and .war)
- name: Rename artifact
run: |
echo "Listing target/ folder..."
ls -lh target/
ARTIFACT_FILE=$(ls target/*.jar 2>/dev/null | grep -v 'original' | head -n 1 || true)
if [ -z "$ARTIFACT_FILE" ]; then
ARTIFACT_FILE=$(ls target/*.war | head -n 1)
fi
echo "Found artifact: $ARTIFACT_FILE"
if [ -z "$ARTIFACT_FILE" ]; then
echo "❌ No jar/war file found. Failing build."
exit 1
fi
cp "$ARTIFACT_FILE" target/app.jar
ls -lh target/
# 5. Configure AWS credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# 6. Login to Amazon ECR
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
# 7. Build, Tag, and Push Docker image
- name: Build, Tag, and Push image to ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
echo "Building Docker image with artifact..."
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
echo "Pushing image to ECR..."
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
# 8. Construct and export image URI
- name: Set IMAGE_URI
id: image_uri
run: echo "IMAGE_URI=${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_ENV
# 9. Update kubeconfig for EKS cluster
- name: Update kubeconfig for EKS cluster
run: |
aws eks update-kubeconfig \
--region ${{ secrets.AWS_REGION }} \
--name ${{ secrets.EKS_CLUSTER_NAME }}
# 10. Deploy to EKS (Update Image in Deployment)
- name: Deploy to EKS (Update Image in Deployment)
run: |
echo "Deploying image: $IMAGE_URI"
if kubectl get deployment my-deployment >/dev/null 2>&1; then
echo "Updating existing deployment..."
kubectl set image deployment/my-deployment my-container=$IMAGE_URI --record
else
echo "Deployment not found. Creating..."
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
fi
kubectl rollout status deployment/my-deployment