Skip to content

Workflow file for this run

name: Deploy to AWS EC2
on:
push:
branches:
- pipeline
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "lts/*"
cache: "npm"
- name: Install Dependencies
run: |
npm install
- name: Build Next.js App
run: npm run build
- name: Set up SSH Key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
- name: Copy Files to EC2
env:
EC2_HOST: [email protected]
run: |
mkdir -p deploy
tar -czf deploy/app.tar.gz .next package.json package-lock.json
scp -i ~/.ssh/deploy_key deploy/app.tar.gz $EC2_HOST:~/app.tar.gz
- name: Deploy on EC2
env:
EC2_HOST: [email protected]
run: |
ssh -i ~/.ssh/deploy_key $EC2_HOST << EOF
mkdir -p ~/app && tar -xzf ~/app.tar.gz -C ~/app && cd ~/app
npm install --production
pm2 stop all || true
pm2 start npm --name "nextjs-app" -- start
EOF