Update deploy_test.yml #37
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 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: ${{ 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 # Exit immediately if a command exits with a non-zero status | |
# 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 ${{ env.NODE_VERSION }} | |
nvm use ${{ env.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 || { echo "Git pull failed"; exit 1; } | |
# Install dependencies and build | |
yarn install --frozen-lockfile || { echo "Dependency installation failed"; exit 1; } | |
yarn build || { echo "Build process failed"; exit 1; } | |
# Start or restart the Next.js app with PM2 | |
if pm2 describe nextjs-app > /dev/null; then | |
pm2 reload nextjs-app || { echo "PM2 reload failed"; exit 1; } | |
else | |
pm2 start yarn --name "nextjs-app" -- start || { echo "PM2 start failed"; exit 1; } | |
fi | |
# Save the PM2 process list | |
pm2 save |