-
Notifications
You must be signed in to change notification settings - Fork 1
134 lines (115 loc) · 4.98 KB
/
Copy pathcd-dev.yml
File metadata and controls
134 lines (115 loc) · 4.98 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
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
name: CD Dev
on:
push:
branches:
- release
- main
permissions:
id-token: write
contents: read
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: cherrish-server
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: 'gradle'
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/cherrish-github-actions-role
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build and push image with Jib
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}
run: |
./gradlew jib \
-Djib.to.image=${ECR_REGISTRY} \
-Djib.to.tags=latest,${{ github.sha }}
- name: Deploy to EC2 via SSM
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
COMMAND_ID=$(aws ssm send-command \
--instance-ids "${{ secrets.EC2_INSTANCE_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters "commands=[
'cd /home/ec2-user/cherrish',
'curl -fsSL https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}/scripts/deploy-dev.sh -o deploy-dev.sh',
'chmod +x deploy-dev.sh',
'export AWS_REGION=${{ env.AWS_REGION }}',
'export ECR_REGISTRY=${ECR_REGISTRY}',
'export IMAGE_TAG=${IMAGE_TAG}',
'bash deploy-dev.sh'
]" \
--timeout-seconds 300 \
--query "Command.CommandId" \
--output text)
echo "Command ID: ${COMMAND_ID}"
# Wait for command to complete (poll until done)
echo "Waiting for deployment to complete..."
for i in {1..60}; do
STATUS=$(aws ssm get-command-invocation \
--command-id "${COMMAND_ID}" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query "Status" \
--output text 2>/dev/null || echo "Pending")
echo "Status: ${STATUS}"
if [ "${STATUS}" = "Success" ]; then
echo "Deployment successful!"
aws ssm get-command-invocation \
--command-id "${COMMAND_ID}" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query "StandardOutputContent" \
--output text
exit 0
elif [ "${STATUS}" = "Failed" ] || [ "${STATUS}" = "Cancelled" ] || [ "${STATUS}" = "TimedOut" ]; then
echo "Deployment failed with status: ${STATUS}"
echo "=== STDOUT ==="
aws ssm get-command-invocation \
--command-id "${COMMAND_ID}" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query "StandardOutputContent" \
--output text
echo "=== STDERR ==="
aws ssm get-command-invocation \
--command-id "${COMMAND_ID}" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query "StandardErrorContent" \
--output text
exit 1
fi
sleep 5
done
echo "Deployment timed out"
exit 1
- name: Send Discord notification on success
if: success()
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
curl -H "Content-Type: application/json" \
-d "{\"embeds\": [{\"title\": \"🚀 Deploy Success\", \"description\": \"Cherrish Server deployed to dev\", \"color\": 3066993, \"fields\": [{\"name\": \"Branch\", \"value\": \"${{ github.ref_name }}\", \"inline\": true}, {\"name\": \"Commit\", \"value\": \"\`${GITHUB_SHA:0:7}\`\", \"inline\": true}], \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)\"}]}" \
$DISCORD_WEBHOOK || true
- name: Send Discord notification on failure
if: failure()
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
curl -H "Content-Type: application/json" \
-d "{\"embeds\": [{\"title\": \"❌ Deploy Failed\", \"description\": \"Cherrish Server dev deployment failed\", \"color\": 15158332, \"fields\": [{\"name\": \"Branch\", \"value\": \"${{ github.ref_name }}\", \"inline\": true}, {\"name\": \"Commit\", \"value\": \"\`${GITHUB_SHA:0:7}\`\", \"inline\": true}], \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)\"}]}" \
$DISCORD_WEBHOOK || true