fix: main.yml 수정 #8
This file contains 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 to EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "21.7.3" | |
- name: Cache Node.js modules | |
uses: actions/cache@v2 | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Install dependencies | |
run: npm ci | |
- name: Create .env file | |
run: echo "${{ secrets.ENV }}" >> .env | |
# Build step removed since there is no build script | |
- name: Copy files via SSH | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.EC2_DNS }} | |
username: ${{ secrets.EC2_USER_NAME }} | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
port: 22 | |
source: "." | |
target: "/home/ubuntu/wething-backend" | |
- name: Execute remote commands via SSH | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_DNS }} | |
username: ${{ secrets.EC2_USER_NAME }} | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
port: 22 | |
script: | | |
# Install Node.js and pm2 on Ubuntu | |
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash | |
sudo apt-get update | |
curl -sL https://rpm.nodesource.com/setup_21.x | sudo bash - | |
# sudo nvm install 21.7.3 | |
sudo apt-get install -y node.js | |
npm install -g pm2 | |
# Navigate to the application directory | |
cd /home/ubuntu/wething-backend | |
# Install project dependencies | |
npm install --production | |
# Restart or start the application with pm2 | |
pm2 stop all | |
pm2 delete all | |
pm2 start bin/www |