-
Notifications
You must be signed in to change notification settings - Fork 1
177 lines (153 loc) · 6.21 KB
/
deploy-production.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
name: deploy
on:
push:
branches: [dev]
pull_request:
branches: [dev]
env:
DOCKER_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/juga-docker
DOCKER_TAG: ${{ github.sha }}
jobs:
build-and-deploy:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
app:
[
{
name: 'be-1',
dir: 'BE',
port: 3000,
container: 'juga-docker-be-1',
env: 'ENV_BE_1',
},
{
name: 'be-2',
dir: 'BE',
port: 3001,
container: 'juga-docker-be-2',
env: 'ENV_BE_2',
},
{
name: 'be-3',
dir: 'BE',
port: 3002,
container: 'juga-docker-be-3',
env: 'ENV_BE_3',
},
{
name: 'fe',
dir: 'FE',
port: 5173,
container: 'juga-docker-fe',
env: 'ENV_FE',
},
]
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: ./${{matrix.app.dir}}/package-lock.json
- name: Create .env file
run: |
touch ./${{ matrix.app.dir }}/.env
echo "${{ secrets[matrix.app.env] }}" > ./${{matrix.app.dir}}/.env
- name: Install dependencies
working-directory: ./${{matrix.app.dir}}
continue-on-error: true
run: npm ci
- name: Run tests
if: ${{ contains(matrix.app.name, 'be') }}
working-directory: ./${{matrix.app.dir}}
run: npm test
env:
CI: true
- name: Run linter
working-directory: ./${{matrix.app.dir}}
run: npm run lint
- name: Build application
working-directory: ./${{matrix.app.dir}}
run: npm run build
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and Push Docker Image
working-directory: ./${{ matrix.app.dir }}
env:
NCP_ACCESS_KEY: ${{ secrets.NCP_ACCESS_KEY }}
NCP_SECRET_KEY: ${{ secrets.NCP_SECRET_KEY }}
run: |
if [ "${{ matrix.app.name }}" = "fe" ]; then
docker build \
--build-arg VITE_SOCKET_URL=${{ secrets.VITE_SOCKET_URL }} \
--build-arg VITE_API_URL=${{ secrets.VITE_API_URL }} \
--build-arg VITE_API_KAKAO_URL=${{ secrets.VITE_API_KAKAO_URL }} \
--build-arg VITE_STORAGE_KEY=${{ secrets.VITE_STORAGE_KEY }} \
--build-arg VITE_MAX_HISTORY_ITEMS=${{ secrets.VITE_MAX_HISTORY_ITEMS }} \
-t ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} .
else
docker build -t ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} .
fi
docker tag ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
- name: Get Github Actions IP
id: ip
uses: haythem/[email protected]
- name: Setting NCP CLI & Credentials
run: |
cd ~
wget https://www.ncloud.com/api/support/download/5/65
unzip 65
mkdir ~/.ncloud
echo -e "[DEFAULT]\nncloud_access_key_id = ${{ secrets.NCP_ACCESS_KEY }}\nncloud_secret_access_key = ${{ secrets.NCP_SECRET_KEY }}\nncloud_api_url = ${{ secrets.NCP_API_URI }}" >> ~/.ncloud/configure
- name: Add Github Action Ip to Security group
run: |
cd ~
ls -la
chmod -R 777 ~/cli_linux
cd ~/cli_linux
./ncloud vserver addAccessControlGroupInboundRule --regionCode KR --vpcNo ${{ secrets.NCP_VPC_ID }} --accessControlGroupNo ${{ secrets.NCP_ACG_ID }} --accessControlGroupRuleList "protocolTypeCode='TCP', ipBlock='${{ steps.ip.outputs.ipv4 }}/32', portRange='${{ secrets.SSH_PORT }}'"
- name: Deploy to NCP Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NCP_SERVER_HOST }}
username: ${{ secrets.NCP_SERVER_USERNAME }}
key: ${{ secrets.NCP_SERVER_SSH_KEY }}
port: 22
script: |
docker system prune -af
echo "${{ secrets[matrix.app.env] }}" > ${{matrix.app.name}}.env
docker network create juga-network || true
if [ "${{ matrix.app.name }}" = "be-1" ]; then
docker run -d \
--name redis \
--network juga-network \
-p 6379:6379 \
-v redis_data:/data \
redis:latest redis-server --appendonly yes
docker exec redis redis-cli EVAL "local keys = redis.call('keys', 'connections:*') for i=1,#keys do redis.call('del', keys[i]) end" 0
fi
docker pull ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker stop ${{ matrix.app.container }} || true
docker rm ${{ matrix.app.container }} || true
docker run -d \
--name ${{ matrix.app.container }} \
--network juga-network \
-p ${{ matrix.app.port }}:3000 \
--env-file ${{matrix.app.name}}.env \
${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker ps | grep ${{ matrix.app.container }}
docker logs ${{ matrix.app.container }}
rm ${{matrix.app.name}}.env
- name: Remove Github Action Ip to Security group
run: |
chmod -R 777 ~/cli_linux
cd ~/cli_linux
./ncloud vserver removeAccessControlGroupInboundRule --regionCode KR --vpcNo ${{ secrets.NCP_VPC_ID }} --accessControlGroupNo ${{ secrets.NCP_ACG_ID }} --accessControlGroupRuleList "protocolTypeCode='TCP', ipBlock='${{ steps.ip.outputs.ipv4 }}/32', portRange='${{ secrets.SSH_PORT }}'"