-
Notifications
You must be signed in to change notification settings - Fork 0
151 lines (123 loc) · 4.45 KB
/
Copy pathjwt-actions.yml
File metadata and controls
151 lines (123 loc) · 4.45 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
name: Writon Nodejs(TypeScript) CI with NPM and CD with Docker & AWS EC2
on:
push:
branches: ['master']
pull_request:
branches: ['master']
permissions:
contents: read
jobs:
CI:
runs-on: ubuntu-latest
steps:
# 소스코드 다운로드
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '23' # 또는 프로젝트에 맞는 Node.js 버전
# NPM 종속성 설치
- name: Install dependencies
run: npm install
# FIREBASE SDK 세팅
- name: create-json
id: create-json
uses: jsdaniell/create-json@1.1.2
with:
name: 'writon-firebase-admin.json'
json: ${{ secrets.FIREBASE }}
# 빌드
- name: Build
run: npm run build
# 린팅
# - name: Run Lint
# run: npm run lint # package.json에 정의된 lint 스크립트 실행
# 보안 검사 (예: npm audit)
# - name: Run Security Audit with audit-ci
# run: npx audit-ci --high --production --config ./audit-ci.json
CD:
needs: CI
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
steps:
# 소스코드 다운로드
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16' # 또는 프로젝트에 맞는 Node.js 버전
# ENV SDK 세팅
- name: Setting env file
run: |
touch .env
echo "${{ secrets.ENV }}" >> .env
shell: bash
# FIREBASE SDK 세팅
- name: create-json
id: create-json
uses: jsdaniell/create-json@1.1.2
with:
name: 'writon-firebase-admin.json'
json: ${{ secrets.FIREBASE }}
- name: Log in to GHCR with PAT
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_PAT }}
- name: Build Docker Image For Node.js
run: |
docker build -t ghcr.io/${{ github.repository_owner }}/blue:1.0 -f blue.Dockerfile .
docker push ghcr.io/${{ github.repository_owner }}/blue:1.0
docker build -t ghcr.io/${{ github.repository_owner }}/green:1.0 -f green.Dockerfile .
docker push ghcr.io/${{ github.repository_owner }}/green:1.0
# 서버에서 Docker 이미지 실행
- name: EC2 Docker Run
uses: appleboy/ssh-action@v0.1.6
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
echo ${{ secrets.GHCR_PAT }} | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
docker pull ghcr.io/${{ github.repository_owner }}/green:1.0
docker run -d -p 3001:3000 --name green ghcr.io/${{ github.repository_owner }}/green:1.0
for i in {1..60}; do
if curl -sf http://localhost:3001/health > /dev/null; then
echo "Green container is ready"
break
fi
echo "Waiting for green container (attempt $i)..."
sleep 1
if [ $i -eq 60]; then
echo "Green container failed to start in time"
exit 1
fi
done
sudo touch /etc/nginx/green.flag
sudo rm /etc/nginx/blue.flag
docker stop blue
docker rm blue
docker rmi ghcr.io/${{ github.repository_owner }}/blue:1.0 || true
docker pull ghcr.io/${{ github.repository_owner }}/blue:1.0
docker run -d -p 3002:3000 --name blue ghcr.io/${{ github.repository_owner }}/blue:1.0
sleep 10
for i in {1..60}; do
if curl -sf http://localhost:3002/health > /dev/null; then
echo "Blue container is ready!"
break
fi
echo "Waiting for blue container (attempt $i)..."
sleep 1
if [ $i -eq 60]; then
echo "Blue container failed to start in time."
exit 1
fi
done
sudo touch /etc/nginx/blue.flag
sudo rm /etc/nginx/green.flag
docker stop green
docker rm green
docker rmi ghcr.io/${{ github.repository_owner }}/green:1.0 || true