git pull -> npm run build -> pm2 reload 이렇게 구성되어 있습니다.https://github.com/adnanh/webhook
apt install webhook
npm install pm2 -g

No Expiration으로 설정하고, scope는 repo를 선택합니다.
ssh ubuntu@12.34.56.78 -i your-key.pem
git clone https://<username>:<token>@github.com/<username>/<repository>

deploy 폴더와 run.sh 스크립트는 제가 추가한 것입니다.[
{
"id": "rebuild",
"execute-command": "rebuild.sh",
"command-working-directory": "/home/ubuntu/my-nextjs-project/deploy",
"response-message": "push event received",
"trigger-rule": {
"match": {
"type": "value",
"value": "refs/heads/master",
"parameter": {
"source": "payload",
"name": "ref"
}
}
}
}
]
git pull -> npm run build -> pm2 reload 순서로 실행됩니다.#!/bin/bash
cd /home/ubuntu/my-nextjs-project
git pull
chmod +x ./deploy/rebuild.sh
npm run build
pm2 reload deploy/ecosystem.config.js
module.exports = {
apps: [
{
name: "my-nextjs-project",
cwd: "/home/ubuntu/my-nextjs-project",
script: "npm",
args: "start",
instances: 0,
exec_mode: "cluster",
},
],
};
pm2와 webhook을 실행합니다.&를 붙여 백그라운드에서 실행합니다.#!/bin/bash
cd /home/ubuntu/my-nextjs-project/deploy
pm2 start ecosystem.config.js &
webhook -hooks hooks.json -port 12345 &
chmod +x ./run.sh
./run.sh
AWS 메뉴 진입: EC2 -> 보안 그룹 -> 보안 그룹 선택 -> 인바운드 규칙 편집
원하는 포트를 추가합니다. 저는 12345번 포트를 추가했습니다.

13.209.1.56/29 부분은 AWS 서울 리전의 Instance Connect IP 대역입니다.GitHub 메뉴 진입: Repository -> Settings -> Webhooks -> Add webhook
사진과 같이 설정합니다.

Add webhook 버튼을 누르면 웹훅이 등록됩니다.
이후 main 브랜치에서 push 이벤트가 발생하면 EC2 인스턴스로 다음과 같은 본문과 함께 POST 요청이 전송됩니다.
{
"event": "push",
"payload": {
"ref": "refs/heads/main",
"before": "1214900eca16aa54d97d062e7b72261616fd53aa",
"after": "40a717b3644e2ddec52cf6c8bfa436767bf0704e",
"repository": {
"id": 17892893,
rebuild.sh 스크립트를 실행합니다.