Create deploy.yml #1
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 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '18' | |
| - name: Install dependencies | |
| run: npm install | |
| - name: Deploy via SSH and PM2 restart | |
| uses: appleboy/ssh-action@v0.1.1 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| # EC2 서버에서 실행할 배포 스크립트 | |
| DEPLOY_PATH=/home/ec2-user/app | |
| # 최초 배포 및 이후 배포 로직 | |
| if [ ! -d $DEPLOY_PATH/.git ]; then | |
| git clone https://github.com/${{ github.repository }}.git $DEPLOY_PATH | |
| cd $DEPLOY_PATH | |
| npm install | |
| pm2 start npm --name "node-app" -- start | |
| else | |
| cd $DEPLOY_PATH | |
| git pull origin master | |
| npm install --production | |
| pm2 reload node-app | |
| fi |