Skip to content

Commit adf7633

Browse files
authored
Refactor deploy-development.yml and reload.sh for CI/CD performance (#535)
* Rollback deploy file to test with Github Hosted Runner * Update branch name to push * Add step to check default github workspace path * Add path option for actions/checkout * Delete unnecessary code * Use bash instead of sh * Get the latest changes from the branch * Manage branch name with environment variable * Use '&&' to ensure each step runs successfully * Write branch name directly * Fetch all branches from remote repository * Update reload.sh to delete duplicate code and clarify roles * Remove git reset and leave only git pull * Reflect the changes so far in deployment-development.yml * Delete the file for testing using Github Hosted Runner * Delete code for checking github workspace path * Reflect the changes so far in deployment-production-1.yml * Reflect the changes so far in deployment-production-2.yml * Reflect the changes so far in deployment-production.yml
1 parent 966a101 commit adf7633

File tree

5 files changed

+91
-64
lines changed

5 files changed

+91
-64
lines changed

.github/workflows/deploy-development.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ jobs:
3030
key: ${{ secrets.SSH_PRIVATE_KEY }}
3131
port: 22
3232
script: |
33-
cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json
34-
cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env
35-
cd ${{ env.PROJECT_PATH }}
36-
bash script/prebuild.sh
37-
sh script/reload.sh
33+
cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json &&
34+
cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env &&
35+
cd ${{ env.PROJECT_PATH }} &&
36+
git fetch --all &&
37+
git checkout ${{ env.BRANCH_NAME }} &&
38+
git pull origin ${{ env.BRANCH_NAME }} &&
39+
bash script/prebuild.sh &&
40+
bash script/reload.sh
3841
3942
env:
43+
BRANCH_NAME: dev
4044
NODE_ENV: development
4145
SERVER_PROFILE: development
4246
PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/development
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Deploy production-1 server to ec2
52

63
on:
@@ -10,28 +7,41 @@ on:
107
- production-1
118

129
jobs:
13-
build:
14-
runs-on: self-hosted
10+
deploy:
11+
runs-on: ubuntu-latest
12+
1513
strategy:
1614
matrix:
1715
node-version: [16.x]
18-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19-
defaults:
20-
run:
21-
working-directory: ${{ env.SERVER_PROFILE }}
16+
2217
steps:
23-
- uses: actions/checkout@v2
24-
with:
25-
path: ${{ env.SERVER_PROFILE }}
18+
- name: Checkout Repository
19+
uses: actions/checkout@v3
20+
2621
- name: Use Node.js ${{ matrix.node-version }}
27-
uses: actions/setup-node@v2
22+
uses: actions/setup-node@v3
2823
with:
2924
node-version: ${{ matrix.node-version }}
30-
- run: cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json
31-
- run: cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env
32-
- run: bash ${{ env.PROJECT_PATH }}/script/prebuild.sh
33-
- run: sh ${{ env.PROJECT_PATH }}/script/reload.sh
25+
26+
- name: Deploy to EC2 using SSH
27+
uses: appleboy/ssh-action@master
28+
with:
29+
host: ${{ secrets.SERVER_HOST }}
30+
username: ${{ secrets.SSH_USERNAME }}
31+
key: ${{ secrets.SSH_PRIVATE_KEY }}
32+
port: 22
33+
script: |
34+
cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json &&
35+
cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env &&
36+
cd ${{ env.PROJECT_PATH }} &&
37+
git fetch --all &&
38+
git checkout ${{ env.BRANCH_NAME }} &&
39+
git pull origin ${{ env.BRANCH_NAME }} &&
40+
bash script/prebuild.sh &&
41+
bash script/reload.sh
42+
3443
env:
44+
BRANCH_NAME: production-1
3545
NODE_ENV: production
3646
SERVER_PROFILE: production-1
3747
PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/production-1
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Deploy production-2 server to ec2
52

63
on:
@@ -10,28 +7,41 @@ on:
107
- production-2
118

129
jobs:
13-
build:
14-
runs-on: self-hosted
10+
deploy:
11+
runs-on: ubuntu-latest
12+
1513
strategy:
1614
matrix:
1715
node-version: [16.x]
18-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19-
defaults:
20-
run:
21-
working-directory: ${{ env.SERVER_PROFILE }}
16+
2217
steps:
23-
- uses: actions/checkout@v2
24-
with:
25-
path: ${{ env.SERVER_PROFILE }}
18+
- name: Checkout Repository
19+
uses: actions/checkout@v3
20+
2621
- name: Use Node.js ${{ matrix.node-version }}
27-
uses: actions/setup-node@v2
22+
uses: actions/setup-node@v3
2823
with:
2924
node-version: ${{ matrix.node-version }}
30-
- run: cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json
31-
- run: cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env
32-
- run: bash ${{ env.PROJECT_PATH }}/script/prebuild.sh
33-
- run: sh ${{ env.PROJECT_PATH }}/script/reload.sh
25+
26+
- name: Deploy to EC2 using SSH
27+
uses: appleboy/ssh-action@master
28+
with:
29+
host: ${{ secrets.SERVER_HOST }}
30+
username: ${{ secrets.SSH_USERNAME }}
31+
key: ${{ secrets.SSH_PRIVATE_KEY }}
32+
port: 22
33+
script: |
34+
cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json &&
35+
cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env &&
36+
cd ${{ env.PROJECT_PATH }} &&
37+
git fetch --all &&
38+
git checkout ${{ env.BRANCH_NAME }} &&
39+
git pull origin ${{ env.BRANCH_NAME }} &&
40+
bash script/prebuild.sh &&
41+
bash script/reload.sh
42+
3443
env:
44+
BRANCH_NAME: production-2
3545
NODE_ENV: production
3646
SERVER_PROFILE: production-2
3747
PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/production-2
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Deploy production server to ec2
52

63
on:
4+
workflow_dispatch:
75
push:
86
branches:
97
- main
108

119
jobs:
12-
build:
13-
runs-on: self-hosted
10+
deploy:
11+
runs-on: ubuntu-latest
12+
1413
strategy:
1514
matrix:
1615
node-version: [16.x]
17-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
18-
defaults:
19-
run:
20-
working-directory: ${{ env.SERVER_PROFILE }}
16+
2117
steps:
22-
- uses: actions/checkout@v2
23-
with:
24-
path: ${{ env.SERVER_PROFILE }}
18+
- name: Checkout Repository
19+
uses: actions/checkout@v3
20+
2521
- name: Use Node.js ${{ matrix.node-version }}
26-
uses: actions/setup-node@v2
22+
uses: actions/setup-node@v3
2723
with:
2824
node-version: ${{ matrix.node-version }}
29-
- run: cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json
30-
- run: cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env
31-
- run: bash ${{ env.PROJECT_PATH }}/script/prebuild.sh
32-
- run: sh ${{ env.PROJECT_PATH }}/script/reload.sh
25+
26+
- name: Deploy to EC2 using SSH
27+
uses: appleboy/ssh-action@master
28+
with:
29+
host: ${{ secrets.SERVER_HOST }}
30+
username: ${{ secrets.SSH_USERNAME }}
31+
key: ${{ secrets.SSH_PRIVATE_KEY }}
32+
port: 22
33+
script: |
34+
cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json &&
35+
cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env &&
36+
cd ${{ env.PROJECT_PATH }} &&
37+
git fetch --all &&
38+
git checkout ${{ env.BRANCH_NAME }} &&
39+
git pull origin ${{ env.BRANCH_NAME }} &&
40+
bash script/prebuild.sh &&
41+
bash script/reload.sh
42+
3343
env:
44+
BRANCH_NAME: main
3445
NODE_ENV: production
3546
SERVER_PROFILE: production
3647
PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/production

script/reload.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
# 에러 핸들링 추가
44
set -e
55

6-
# ecosystem.json 파일이 존재하면 pm2로 실행 중인 프로세스를 중지
7-
if [[ -f "ecosystem.json" ]]; then
8-
pm2 stop ecosystem.json || true
9-
fi
10-
11-
# npm run build 명령어로 프로젝트 빌드
12-
npm run build || exit 1
13-
146
# ecosystem.json 사용하여 pm2로 프로세스 재시작
157
if [[ -f "ecosystem.json" ]]; then
168
pm2 restart ecosystem.json || exit 1

0 commit comments

Comments
 (0)