Vite+React zero downtime deployment

Vite+React zero downtime deployment

Mukhammad Irmatov

name: Deploy Vite + React to Nginx


on:

 push:

  branches:

   - main


jobs:

 deploy:

  runs-on: ubuntu-latest


  steps:

   # 1️⃣ Checkout the Repository

   - name: Checkout Repository

    uses: actions/checkout@v2


   # 2️⃣ Set Up Node.js

   - name: Set up Node.js

    uses: actions/setup-node@v3

    with:

     node-version: 20.11.1

     cache: "npm"


   # 3️⃣ Install Dependencies

   - name: Install Dependencies

    run: yarn install --frozen-lockfile


   # 4️⃣ Build Vite Project

   - name: Build Vite Project

    run: yarn build


   # 5️⃣ Define Environment Variables

   - name: Set Deployment Timestamp

    run: echo "DEPLOY_TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV


   # 6️⃣ Transfer Build Files to Server via SSH (Backup & Deploy)

   - name: Transfer Build Files to Server

    uses: appleboy/scp-action@master

    with:

     host: ${{ secrets.SSH_HOST }}

     username: ${{ secrets.SSH_USERNAME }}

     key: ${{ secrets.SSH_PRIVATE_KEY }}

     source: "dist/*"

     target: "/var/www/cafesta-landing/releases/${{ env.DEPLOY_TIMESTAMP }}"


   # 7️⃣ SSH into Server, Symlink New Release & Restart Nginx

   - name: SSH Deploy & Rollback on Failure

    uses: appleboy/ssh-action@master

    with:

     host: ${{ secrets.SSH_HOST }}

     username: ${{ secrets.SSH_USERNAME }}

     key: ${{ secrets.SSH_PRIVATE_KEY }}

     script: |

      DEPLOY_PATH="/var/www/cafesta-landing"

      RELEASES_PATH="$DEPLOY_PATH/releases"

      NEW_RELEASE="$RELEASES_PATH/${{ env.DEPLOY_TIMESTAMP }}"

      CURRENT_RELEASE="$DEPLOY_PATH/current"


      # Backup old deployment (optional)

      mkdir -p "$DEPLOY_PATH/backups"

      if [ -L "$CURRENT_RELEASE" ]; then

       PREV_RELEASE=$(readlink -f $CURRENT_RELEASE)

       tar -czf "$DEPLOY_PATH/backups/backup_$(date +%Y%m%d%H%M%S).tar.gz" -C "$PREV_RELEASE" .

      fi


      # Update symlink to new release

      ln -sfn $NEW_RELEASE $CURRENT_RELEASE


      # Restart Nginx and handle failure

      sudo service nginx reload || {

       echo "Nginx reload failed! Rolling back..."

       ln -sfn $(ls -dt $RELEASES_PATH/* | head -n 2 | tail -n 1) $CURRENT_RELEASE

       sudo service nginx reload

      }


   # 8️⃣ Send Deployment Notification (Optional)

   - name: Send Telegram Notification

    uses: appleboy/telegram-action@master

    with:

     to: ${{ secrets.TELEGRAM_CHAT_ID }}

     token: ${{ secrets.TELEGRAM_BOT_TOKEN }}

     message: "🚀 Tabriklayman, deploymentni eplading"


Report Page