forked from prudhvisurya996/JavaWebCalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy
More file actions
79 lines (62 loc) · 2.34 KB
/
Copy pathdeploy
File metadata and controls
79 lines (62 loc) · 2.34 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
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: self-hosted
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. Show image URI
- name: Image URI
run: echo "${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}"