pipeline {
    agent any

    tools {
        nodejs 'nodejs'
    }

    options {
        timestamps()
        disableConcurrentBuilds()
        timeout(time: 30, unit: 'MINUTES')
    }

    environment {
        NODE_ENV = 'production'
        CI = 'true'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Install Dependencies') {
            steps {
                sh 'npm ci --include=dev'
            }
        }

        stage('Lint') {
            steps {
                sh 'npm run lint'
            }
        }

        stage('Build') {
            steps {
                sh 'npm run build'
                archiveArtifacts artifacts: 'dist/**', fingerprint: true, allowEmptyArchive: false
            }
        }

        stage('Deploy') {
            steps {
                sh 'sudo /usr/bin/rsync -a --delete dist/ /opt/1panel/www/sites/workstation.xiaozhou233.cn/index/'
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}
