pipeline {
  agent {
    docker {
      image 'cimg/node:18.18.2'
      args '-v /root/jenkins/data/jobs/${JOB_NAME}/data/node_modules:${WORKSPACE}/node_modules -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker'
    }
  }
  stages {
    stage('BUILD PROJECT') {
      steps {
        sh '''
          npm set registry https://registry.npmmirror.com/
          npm install -g pnpm 
          node -v
          pnpm -v
          pnpm install
          pnpm run build:uat
          cp -r ./dist ./build
        '''
      }
    }
    stage('BUILD IMAGE') {
      steps {
        sh '''
          cd build
          docker build -t $registry_address/$image_name:latest .'''
      }
    }
    stage('PUSH IMAGE') {
        steps {
          sh '''docker login --username=rcsadmin@gsst -p 9HRpm_hk registry.cn-shenzhen.aliyuncs.com
          docker push $registry_address/$image_name:latest'''
        }
    }
    stage('REMOVE LOCAL IMAGE') {
        steps {
          sh '''docker rmi -f $registry_address/$image_name:latest'''
        }
    }
    stage('Cleanup') {
      steps {
        cleanWs(deleteDirs: true)
      }
    }
  }
  environment {
    registry_address = 'registry.cn-shenzhen.aliyuncs.com/gsst'
    image_name = 'modellink-poe'
  }
}
