Update deploy_test.yml #23
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: | | |
yarn install | |
yarn build | |
- name: Set up SSH | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts | |
- name: Deploy via SSH | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} << 'EOF' | |
set -e | |
echo "Checking disk space..." | |
df -h | |
echo "Checking if Yarn is installed..." | |
if ! command -v yarn > /dev/null; then | |
echo "Yarn is not installed. Installing Yarn locally..." | |
npm install --prefix ~/.local yarn || { echo "Yarn installation failed"; exit 1; } | |
export PATH=$HOME/.local/bin:$PATH | |
else | |
echo "Yarn is already installed." | |
fi | |
echo "Node.js version installed:" | |
node -v | |
echo "Yarn version installed:" | |
yarn -v | |
echo "Installing global dependencies..." | |
yarn global add pm2 || { echo "Yarn global add pm2 failed"; exit 1; } | |
echo "Deploying the application..." | |
cd /home/${{ secrets.SERVER_USER }}/public_html | |
git pull origin main || { echo "Git pull failed"; exit 1; } | |
yarn install --frozen-lockfile || { echo "Dependency installation failed"; exit 1; } | |
yarn 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 yarn --name 'nextjs-app' -- run start || { echo "PM2 start failed"; exit 1; } | |
fi | |
pm2 save | |
EOF |