Skip to content

Commit 4a61b19

Browse files
committed
docs: 배포 자동화 ci/cd 문서화
1 parent f2b95ea commit 4a61b19

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

.documents/04-project-build-run.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ cp -rf dist/* ../backend/public
3333
```bash
3434
cd ../backend/
3535
npm i
36-
pm2 start bin/www --name web
36+
pm2 start bin/www --name react-api-cicd
3737
pm2 list
3838
```

.documents/06-ci-cd-setup.md

+70-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,66 @@
11
## 배포 자동화하기 CI/CD
22

3-
- `CI (Continuous Integration)`: 영역별로 나눠서 개발할 때, 인터페이스 등의 충돌을 미리 발견하기 위해 매일 또는 매시간 저장소를 자동으로 필드하는 개념
4-
- `CD (Continuous Delivery)`: 저장소 코드의 변경이 발생하면 개발 서버 또는 운영계에 자동으로 배포하는 작업
3+
- `CI (Continuous Integration)`: 개발 과정에서 코드 충돌을 예방하기 위해 주기적으로 저장소를 자동 빌드 및 테스트
4+
- `CD (Continuous Delivery)`: 저장소의 코드 변경 시 자동으로 서버에 배포
55

66
<br />
77

8-
### CI/CD 구성하기
8+
### 배포 스크립트 작성하기
9+
10+
프로젝트 루트 경로에 **scripts/deploy.sh** 파일을 생성한다.
11+
<br />
12+
이 스크립트는 `GitHub Actions`로 main 브랜치 변경 시 최신 코드를 자동으로 서버에 배포, 빌드, 실행한다.
13+
14+
scripts/deploy.sh
15+
16+
```bash
17+
#!/bin/bash
18+
19+
npm -g install yarn
20+
21+
source ~/.bash_profile
22+
cd ~/git/react-api-cicd/
23+
git pull origin main
24+
25+
# front-end
26+
cd frontend/
27+
yarn install
28+
yarn build
29+
cp -rf dist/* ../backend/public
30+
31+
# back-end
32+
cd ../backend/
33+
yarn install
34+
35+
# pm2
36+
pm2 stop react-api-cicd
37+
pm2 start bin/www --name react-api-cicd --update-env
38+
sleep 2
39+
pm2 list
40+
```
41+
42+
<br />
43+
44+
실행 권한 부여
45+
46+
```bash
47+
chmod ./scripts/deploy.sh
48+
```
49+
50+
<br />
51+
52+
코드를 Github에 Push한 후, SSH로 접속한 서버에서 **git pull origin main** 명령어를 실행하여 변경 사항을 반영한다.
53+
54+
<br />
55+
56+
### GitHub Actions 구성하기
957

1058
`CI/CD` 도구로 **Jenkins**가 가장 유명하지만, 이번에는 **GitHub Actions**를 통해서 구성해 본다.
1159
<br />
12-
프로젝트 저장소 root에 **.github/workflows** 폴더를 만들고, 다음 **deploy-main.yml** 파일을 추가한다.
60+
프로젝트 루트 경로에 **.github/workflows/deploy-main.yml** 파일을 만들고 아래 내용을 추가한다.
1361

1462
```yml
15-
name: remote ssh command for deploy
63+
name: Deploy on push to main
1664

1765
on:
1866
push:
@@ -23,6 +71,11 @@ jobs:
2371
name: Build
2472
runs-on: ubuntu-latest
2573
steps:
74+
# 레포지토리 체크아웃
75+
- name: Checkout repository
76+
uses: actions/checkout@v2
77+
78+
# SSH를 사용하여 스크립트 실행
2679
- name: executing remote ssh commands using key
2780
uses: appleboy/ssh-action@master
2881
with:
@@ -31,5 +84,16 @@ jobs:
3184
key: ${{ secrets.KEY }}
3285
port: ${{ secrets.PORT }}
3386
script: |
34-
./deploy.sh
87+
./git/react-api-cicd/scripts/deploy.sh
3588
```
89+
90+
<br />
91+
92+
### Secrets 설정
93+
94+
GitHub에서 프로젝트 레포지토리의 **Settings > Secrets > Actions**로 이동하여 다음 항목을 추가한다.
95+
96+
- `HOST`: 서버 도메인
97+
- `USERNAME`: ec2-user
98+
- `KEY`: react-api-cicd-kp.pem 파일 내용
99+
- `PORT`: 22

0 commit comments

Comments
 (0)