-
Notifications
You must be signed in to change notification settings - Fork 0
134 lines (107 loc) · 4.48 KB
/
cicd.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
name: CI/CD
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
default: 'main'
environment:
description: 'Environment to deploy (dev or prod)'
required: true
default: 'dev'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.branch }}
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Generate application.yml
run: |
mkdir -p ./src/main/resources
echo "${{ secrets.CD_APPLICATION }}" > ./src/main/resources/application.yml
- name: Generate application-aws.yml
run: echo "${{ secrets.CD_APPLICATION_AWS }}" > ./src/main/resources/application-aws.yml
- name: Generate application-naver.yml
run: echo "${{ secrets.CD_APPLICATION_NAVER }}" > ./src/main/resources/application-naver.yml
- name: Generate application-oath.yml
run: echo "${{ secrets.CD_APPLICATION_OATH }}" > ./src/main/resources/application-oath.yml
- name: Build Project
run: ./gradlew clean build -x test
- name: Login to Docker Hub
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker Image
run: docker build -t yh0872/spoony-dev:latest .
- name: Publish Image to Docker Hub
run: docker push yh0872/spoony-dev:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Install SSH Client
run: sudo apt-get update && sudo apt-get install -y openssh-client
- name: Create SSH Key File
run: |
if [ "${{ github.event.inputs.environment }}" == "dev" ]; then
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ssh_key.pem
echo "${{ secrets.DEV_EC2_PUBLIC_IP }}" > ec2_public_ip.txt
else
echo "${{ secrets.PROD_EC2_SSH_KEY }}" > ssh_key.pem
echo "${{ secrets.PROD_EC2_PUBLIC_IP }}" > ec2_public_ip.txt
fi
chmod 600 ssh_key.pem
- name: SSH into EC2 and Deploy
run: |
EC2_PUBLIC_IP=$(cat ec2_public_ip.txt)
ssh -o StrictHostKeyChecking=no -i ssh_key.pem ubuntu@$EC2_PUBLIC_IP << EOF
#!/bin/bash
echo "🔍 현재 실행 중인 포트 확인"
ACTIVE_PORT=\$(sudo docker ps --format "{{.Ports}}" | grep -oE '0.0.0.0:808[12]' | cut -d':' -f2 | cut -d'-' -f1)
if [[ -z "\$ACTIVE_PORT" ]]; then
echo "⚠️ 현재 실행 중인 포트를 찾을 수 없습니다. 기본값(8081)으로 설정합니다."
ACTIVE_PORT="8081"
fi
if [[ "\$ACTIVE_PORT" == "8081" ]]; then
NEW_PORT="8082"
else
NEW_PORT="8081"
fi
echo "🔄 새로운 컨테이너를 \$NEW_PORT 포트에서 실행"
echo "🚀 최신 이미지 가져오기"
sudo docker pull yh0872/spoony-dev:latest
echo "🔧 새로운 컨테이너 실행 (포트 \$NEW_PORT)"
sudo docker run -d -p \$NEW_PORT:8080 --name spoony-dev-\$NEW_PORT yh0872/spoony-dev
echo "⌛️ 새로운 컨테이너가 정상적으로 실행될 때까지 대기..."
sleep 10
echo "🔁 Nginx 설정 업데이트"
sudo bash -c 'cat > /etc/nginx/sites-available/default <<EOL
server {
listen 80;
server_name www.spoony.o-r.kr;
location / {
proxy_pass http://127.0.0.1:$NEW_PORT;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
}
EOL'
echo "🔁 Nginx 재시작하여 트래픽 변경"
sudo nginx -t && sudo nginx -s reload
echo "🗑️ 이전 컨테이너 제거"
if [[ "\$ACTIVE_PORT" == "8081" || "\$ACTIVE_PORT" == "8082" ]]; then
echo "이전 컨테이너 spoony-dev-\$ACTIVE_PORT 종료 및 삭제"
sudo docker stop spoony-dev-\$ACTIVE_PORT
sudo docker rm spoony-dev-\$ACTIVE_PORT
fi
echo "✅ 배포 완료. 현재 활성 컨테이너: spoony-dev-\$NEW_PORT"
EOF