From ef8bf575621ae0b463e0a2a3cc3dbfb7c1622674 Mon Sep 17 00:00:00 2001 From: Pooria Setayesh Date: Sat, 14 Dec 2024 23:24:32 +0330 Subject: [PATCH] ci: update workflow --- .github/workflows/ci.yml | 95 +++++++++++++++++++++++++++++++++++----- 1 file changed, 83 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33f8c51..409822e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,19 +1,90 @@ -name: Jest Tests +name: Deploy NextJs-Woo App on: - pull_request: - workflow_dispatch: push: - branches: [main] + branches: + - main # Change this to your default branch if it's different jobs: - unit-test: - name: Run Tests + build: runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '20.12.2' # Specify the Node.js version you want to use + + - name: Install dependencies + run: | + yarn install --prod + + - name: Build Next.js app + run: | + yarn build + + - name: Prepare assets for standalone build + run: | + # Copy public assets and static files to the standalone directory + cp -r public .next/standalone/ + cp -r .next/static .next/standalone/.next/ + + - name: Zip the build folder + run: | + zip -r nextjs-app.zip .next/standalone + + - name: Upload zip file + uses: actions/upload-artifact@v2 + with: + name: nextjs-app + path: nextjs-app.zip + + deploy: + runs-on: ubuntu-latest + needs: build # Ensure this job runs after the build job steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: '20.11.0' - - run: yarn - - run: yarn test + - name: Download zip file + uses: actions/download-artifact@v2 + with: + name: nextjs-app + + - name: Install sshpass + run: | + sudo apt-get update + sudo apt-get install -y sshpass + + - name: Deploy to server + env: + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + SERVER_IP: ${{ secrets.SERVER_IP }} + USERNAME: ${{ secrets.USERNAME }} + run: | + # Create the SSH directory + mkdir -p ~/.ssh + + # Add the server's host key to known hosts + ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts + + # Copy the zip file to the server using SSH with password + echo "$SSH_PASSWORD" | sshpass scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null nextjs-app.zip $USERNAME@$SERVER_IP:/path/on/server + + # Connect to the server and deploy + echo "$SSH_PASSWORD" | sshpass ssh $USERNAME@$SERVER_IP << 'EOF' + # Unzip the application + cd /path/on/server + unzip -o nextjs-app.zip + + # Install PM2 if not installed + if ! command -v pm2 &> /dev/null + then + npm install -g pm2 + fi + + # Start the application with PM2, setting NODE_PORT + NODE_PORT=3000 pm2 start .next/standalone/server.js --name nextjs-app --watch + pm2 save + pm2 startup +EOF \ No newline at end of file