final String SERVICE_NAME = "linepatrol-web"
final String DIST_MAIN_PATH = "/var/af/standard-release/client/dist"

pipeline {
  agent any
  tools {
    nodejs 'node20'
  }
  stages {
    stage("build") {
      steps {
        script {
          sh "yarn install --registry https://registry.npmjs.org --proxy http://aote-office.8866.org:31691 --https-proxy http://aote-office.8866.org:31691"
          sh "yarn run linux-build"
          sh "tar -zcvf ./dist_${SERVICE_NAME}.tar.gz ./dist"
        }
      }
    }
    stage('ssh') {
      steps {
        script {
          def remote = [
            name         : 'aote-office.8866.org',
            host         : 'aote-office.8866.org',
            port         : '20302' as int,
            allowAnyHosts: true
          ]
          withCredentials([sshUserPrivateKey(credentialsId: '50.67', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
            remote.user = userName
            remote.identityFile = identity
            // 更新dist包
            sshCommand remote: remote, command: "pwd"
            sshCommand remote: remote, command: "mkdir -p ${DIST_MAIN_PATH}"
            sshPut remote: remote, from: "dist_${SERVICE_NAME}.tar.gz", into: "${DIST_MAIN_PATH}/"
            sshCommand remote: remote, command: "cd ${DIST_MAIN_PATH} && tar -xvf ./dist_${SERVICE_NAME}.tar.gz && rm -rf ${DIST_MAIN_PATH}/dist_${SERVICE_NAME} && mv dist dist_${SERVICE_NAME}"
          }
        }
      }
    }
  }
}

