Update deploy_test.yml #14
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 | |
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 |