-
Notifications
You must be signed in to change notification settings - Fork 0
100 lines (85 loc) · 3.39 KB
/
Copy pathcd.yml
File metadata and controls
100 lines (85 loc) · 3.39 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
name: CD - Docker Hub Push on Main
on:
push:
branches:
- main
permissions:
id-token: write # OIDC 토큰 발급
contents: read # checkout이 코드 읽을 권한
jobs:
build-and-push:
name: Build & Push Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Cache Gradle packages
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build & Push BE image
uses: docker/build-push-action@v5
with:
context: .
dockerfile: Dockerfile
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/rootin-be:latest
${{ secrets.DOCKERHUB_USERNAME }}/rootin-be:${{ github.sha }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Deploy to EC2 via SSM
env:
INSTANCE_ID: ${{ secrets.EC2_ID }}
run: |
set -euo pipefail
CMD_ID=$(aws ssm send-command \
--instance-ids "$INSTANCE_ID" \
--document-name "AWS-RunShellScript" \
--comment "Deploy from ${{ github.repository }}" \
--parameters 'commands=["cd /home/ubuntu/rootin && docker compose -f docker-compose.yml pull be && docker compose -f docker-compose.yml up -d --no-deps be && docker image prune -f"]' \
--query "Command.CommandId" --output text)
echo "Command ID: $CMD_ID"
sleep 5
# 종료 상태가 될 때까지 직접 폴링 (긴 배포도 끝까지 기다림)
while true; do
STATUS=$(aws ssm get-command-invocation \
--command-id "$CMD_ID" --instance-id "$INSTANCE_ID" \
--query "Status" --output text 2>/dev/null || echo "Pending")
echo "Current status: $STATUS"
case "$STATUS" in
Success|Failed|Cancelled|TimedOut) break ;;
esac
sleep 5
done
# 실행 로그 출력 (성공/실패 무관하게 항상)
echo "----- stdout -----"
aws ssm get-command-invocation --command-id "$CMD_ID" --instance-id "$INSTANCE_ID" --query "StandardOutputContent" --output text
echo "----- stderr -----"
aws ssm get-command-invocation --command-id "$CMD_ID" --instance-id "$INSTANCE_ID" --query "StandardErrorContent" --output text
# Success가 아니면 워크플로우 실패 처리
if [ "$STATUS" != "Success" ]; then
echo "::error::Deployment failed with status: $STATUS"
exit 1
fi
echo "Deployment succeeded ✅"