pipeline {
    agent any

    environment {
        // git账号，不用改
        GIT_CREDENTIAL_ID = '175776d4-6bbc-4da1-ace2-f2a89453fba4'
        // git地址
        GIT_ADDRESS = 'https://git.mall4j.com/tmerclub/tmerclub-pc'
        // git分支
        GIT_BRANCH = 'master'

        // harbor账号，不用改
        HARBOR_CREDENTIAL_ID = 'edd44d2d-f182-40ab-8e72-b4fcae6f793f'
        // harbor地址
        HARBOR_ADDR = '192.168.1.11:80'
        // 名命空间
        NAMESPACE = 'tmerclub-release'
    }
    stages {
        stage ('拉取git仓库代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/' + "$GIT_BRANCH"]], extensions: [], userRemoteConfigs: [[credentialsId: "$GIT_CREDENTIAL_ID", url: "$GIT_ADDRESS"]]])
            }
        }
        stage ('通过npm构建项目') {
            steps {
sh '''export PATH=/var/jenkins_home/nodejs/bin:$PATH
rm -f .env.production
tee ./.env.production <<-'EOF'
VITE_APP_RESOURCES_URL = 'https://mall4j-minio.mall4j.com/tmerclub'
VITE_APP_BASE_API = 'https://cloud-api.mall4j.com'
VITE_APP_WS_IM_API = 'wss://cloud-api.mall4j.com'
VITE_APP_H5_DOMAIN = 'https://h5.mall4j.com/cloud'
VITE_APP_MERCHANT_PLATFORM_URL = 'https://cloud-multishop.mall4j.com'
VITE_APP_SUPPLIER_PLATFORM_URL = 'https://cloud-supplier.mall4j.com'
EOF
npm config set registry https://registry.npmmirror.com
pnpm install --no-frozen-lockfile
pnpm run build'''
            }
        }
        stage('构建tmerclub-pc镜像') {
            steps {
                withEnv(["PROJECT_NAME=tmerclub-pc"]) {
                    sh 'docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                    withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                        sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                        sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                        sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                    }
                }
            }
        }
    }
}
