Update deploy.yml #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy Node App to EC2 | |
| on: | |
| push: | |
| branches: | |
| - master # 마스터 브랜치에 푸시될 때마다 워크플로 실행 | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 # 1. GitHub에서 코드 체크아웃 (Actions Runner로 가져옴) | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '18' # Node.js 버전 설정 | |
| - name: Install dependencies | |
| run: npm install # 2. 의존성 설치 | |
| - name: Deploy via SSH and PM2 restart | |
| uses: appleboy/ssh-action@v0.1.11 # 3. SSH 접속을 위한 외부 액션 사용 (안정적인 버전 권장) | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| # EC2 서버에서 실행할 배포 스크립트 | |
| DEPLOY_PATH=/home/ec2-user/app | |
| # 최초 배포 및 이후 배포 로직 (Git Clone vs Pull) | |
| if [ ! -d $DEPLOY_PATH/.git ]; then | |
| # 최초 배포: 저장소 클론, npm install, PM2로 앱 시작 | |
| git clone https://github.com/${{ github.repository }}.git $DEPLOY_PATH | |
| cd $DEPLOY_PATH | |
| npm install | |
| pm2 start npm --name "node-app" -- start | |
| else | |
| # 이후 배포: 최신 코드 Pull, 앱 재시작 | |
| cd $DEPLOY_PATH | |
| git pull origin master | |
| npm install --production | |
| pm2 reload node-app | |
| fi |