pipeline {
    agent any
    environment {
        DEPLOY_FRONTEND = '/mnt/h/bitforum/sources'
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm ci'
                sh 'npm run build'
            }
        }
        stage('Build & Deploy') {
            agent { label 'built-in' }
            steps {
                sh 'npm ci'
                sh 'npm run build'
                sh 'rsync -av --delete --exclude="web.config" --exclude="ecosystem.config.js" --exclude="restart.ps1" .next/standalone/ ${DEPLOY_FRONTEND}/'
                sh 'rsync -av --delete .next/static/ ${DEPLOY_FRONTEND}/.next/static/'
                sh 'rsync -av --delete public/ ${DEPLOY_FRONTEND}/public/'
            }
        }
        stage('Restart Frontend') {
            agent { label 'windows-host' }
            steps {
                bat 'chcp 65001 && powershell.exe -ExecutionPolicy Bypass -File H:\\IIS\\bitforum\\restart.ps1'
            }
        }
    }
    post {
        success { echo 'Frontend deploy success!' }
        failure { echo 'Frontend deploy failed!' }
    }
}
