Skip to content

Commit

Permalink
Update deploy_test.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdevcollins authored Dec 6, 2024
1 parent ef6d0f0 commit a664cbb
Showing 1 changed file with 51 additions and 58 deletions.
109 changes: 51 additions & 58 deletions .github/workflows/deploy_test.yml
Original file line number Diff line number Diff line change
@@ -1,67 +1,60 @@
name: 🚀 Deploy Next.js App

on:
push:
branches: [main]

jobs:
build-and-deploy:
name: 🎉 Build and Deploy
runs-on: ubuntu-latest

env:
NODE_VERSION: 20
steps:
- name: 🚚 Checkout Repository
uses: actions/checkout@v4

- name: 🛠 Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: 📂 Deploy to Server
uses: appleboy/ssh-action@v1.0.0
env:
NODE_VERSION: 20
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
envs: NODE_VERSION
script: |
# Ensure required tools are in PATH
export PATH=$HOME/.local/bin:$HOME/.npm-global/bin:$PATH
# Install or update nvm if not present
if [ ! -d "$HOME/.nvm" ]; then
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi
# Install Node.js using nvm
nvm install $NODE_VERSION
nvm use $NODE_VERSION
# Install yarn and pm2 globally
npm install -g yarn pm2
# Navigate to the project directory
cd /home/${{ secrets.SERVER_USER }}/public_html
# Pull the latest code
git pull origin main
# Install dependencies and build
yarn install --frozen-lockfile
yarn build
# Start or restart the Next.js app with PM2
if pm2 describe nextjs-app > /dev/null; then
pm2 reload nextjs-app
else
pm2 start yarn --name "nextjs-app" -- start
fi
# Save the PM2 process list
pm2 save
- name: 🚚 Checkout Repository
uses: actions/checkout@v4

- name: 🛠 Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}

- name: 📦 Install and Build
run: |
npm install
npm run build
- name: 🚀 Deploy to Server
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
set -e
export PATH=$HOME/.local/bin:$HOME/.npm-global/bin:$PATH
if [ ! -d "$HOME/.nvm" ]; then
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi
nvm install ${{ env.NODE_VERSION }}
nvm use ${{ env.NODE_VERSION }}
npm install -g yarn pm2
cd /home/${{ secrets.SERVER_USER }}/public_html
git pull origin main || { echo "Git pull failed"; exit 1; }
npm ci --omit=dev || { echo "Dependency installation failed"; exit 1; }
npm run build || { echo "Build process failed"; exit 1; }
if pm2 describe nextjs-app > /dev/null; then
pm2 reload nextjs-app || { echo "PM2 reload failed"; exit 1; }
else
pm2 start npm --name "nextjs-app" -- run start || { echo "PM2 start failed"; exit 1; }
fi
pm2 save

0 comments on commit a664cbb

Please sign in to comment.