-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (132 loc) · 5.02 KB
/
dev-batch-deploy.yml
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
name: CI/CD Pipeline for Dev Batch
on:
push:
branches:
- develop
paths-ignore:
- 'application/**'
- '.github/**'
- 'scheduler/cron.Dockerfile'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
env:
MODULE_NAME: scheduler
steps:
- name: Parse combined secrets
id: parse_secrets
run: |
echo "Extracting secrets..."
echo '${{ secrets.DEV_BATCH_META_DATA }}' | jq -r 'to_entries | .[] | "echo \(.key)=\(.value) >> $GITHUB_ENV"' | bash
- name: Checkout source code
uses: actions/checkout@v3
with:
token: ${{ env.TOKEN_GITHUB }}
submodules: true
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Grant execute permission for gradlew
run: chmod +x gradlew
shell: bash
- name: Build with Gradle
run: SPRING_PROFILES_ACTIVE=test ./gradlew :${{ env.MODULE_NAME }}:clean :${{ env.MODULE_NAME }}:build
shell: bash
- name: Debug working directory
run: pwd && ls -al ./${{ env.MODULE_NAME }}/src/main/resources/
- name: Upload build artifact (JAR and Dockerfile)
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: |
./${{ env.MODULE_NAME }}/build/libs/*.jar
./${{ env.MODULE_NAME }}/build/resources/main/logback-spring.xml
./${{ env.MODULE_NAME }}/*.Dockerfile
upload-docker-image:
runs-on: ubuntu-latest
needs: build
steps:
- name: Parse combined secrets
id: parse_secrets
run: |
echo "Extracting secrets..."
echo '${{ secrets.DEV_BATCH_META_DATA }}' | jq -r 'to_entries | .[] | "echo \(.key)=\(.value) >> $GITHUB_ENV"' | bash
- name: Download build artifact (JAR and Dockerfile)
uses: actions/download-artifact@v4
with:
name: build-artifacts
- name: Log in to Amazon ECR Public
run: |
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ env.ECR_REPOSITORY_URI }}
- name: Build Spring container image
run: |
docker build --build-arg PROFILE=${{ env.ENVIRONMENT }} -t ${{ env.ECR_REPOSITORY_URI }}/app:latest -f app.Dockerfile .
docker push ${{ env.ECR_REPOSITORY_URI }}/app:latest
deploy:
runs-on: ubuntu-latest
needs: upload-docker-image
steps:
- name: Parse combined secrets
id: parse_secrets
run: |
echo "Extracting secrets..."
echo '${{ secrets.DEV_BATCH_META_DATA }}' | jq -r 'to_entries | .[] | "echo \(.key)=\(.value) >> $GITHUB_ENV"' | bash
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Get EC2 instance ID
id: get-instance-id
run: |
INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Type,Values=${{ env.EC2_TAG_NAME }}" "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].InstanceId" \
--output text \
--region ${{ env.AWS_REGION }})
if [ -z "$INSTANCE_ID" ]; then
echo "No running instance found with tag Type=batch"
exit 1
fi
echo "Instance ID: $INSTANCE_ID"
echo "::set-output name=instance_id::$INSTANCE_ID"
- name: Deploy to EC2 using SSM
id: deploy-ssm
run: |
COMMAND_ID=$(aws ssm send-command \
--document-name "AWS-RunShellScript" \
--targets Key=InstanceIds,Values=${{ steps.get-instance-id.outputs.instance_id }} \
--parameters commands=["${{ env.DEPLOY_COMMAND }}"] \
--comment "Deploy new Docker container" \
--query "Command.CommandId" \
--output text \
--region ${{ env.AWS_REGION }})
if [ -z "$COMMAND_ID" ]; then
echo "Failed to send command"
exit 1
fi
echo "SSM Command ID: $COMMAND_ID"
echo "::set-output name=command_id::$COMMAND_ID"
- name: Monitor SSM Command
run: |
STATUS="InProgress"
while [ "$STATUS" = "InProgress" ] || [ "$STATUS" = "Pending" ]; do
STATUS=$(aws ssm list-command-invocations \
--command-id ${{ steps.deploy-ssm.outputs.command_id }} \
--details \
--query "CommandInvocations[0].Status" \
--output text \
--region ${{ env.AWS_REGION }})
echo "Current status: $STATUS"
sleep 5
done
if [ "$STATUS" = "Success" ]; then
echo "Deployment succeeded!"
exit 0
else
echo "Deployment failed with status: $STATUS"
exit 1
fi