pipeline {
    agent any

    environment {
        APP_NAME="ocr-server-admin"
        K8S_FILE="builds/ingress.yaml"
        K8S_NAMESPACE="kube-qa"
    }


    stages {

        stage('compile'){
            steps {
                nodejs('Node21') {
                    sh "npm install --registry=https://registry.npm.taobao.org && npm run build"
                }
                sh "mkdir -p /etc/nginx/html/${APP_NAME}/"
                sh "rm -rf /etc/nginx/html/${APP_NAME}/*"
                sh "mv dist/* /etc/nginx/html/${APP_NAME}/"
            }
        }

        stage('ack deploy') {
          agent none
          steps {
            sh "cat builds/nginx.conf > /etc/nginx/conf.d/${APP_NAME}.conf"
            configFileProvider([configFile(fileId: '87b5c827-bd51-40af-99f4-31a800614e92', targetLocation: 'K8S-CONFIG', variable: 'KUBECONFIG')]) {
                sh 'export tag=$BUILD_ID && envsubst < $K8S_FILE | kubectl apply -f -'
                sh "kubectl rollout restart deployment -n ${K8S_NAMESPACE} nginx"
            }
          }
        }

    }
}