node('jenkins-jnlp') {
    //checkout源代码
    stage('Checkout') {
        checkout scm
    }
    //npm编译
    stage('Build pnpm') {
        sh "rm -rf /usr/bin/node"
        sh "rm -rf /usr/bin/npm"
        sh "rm -rf /usr/bin/npx"
        sh "rm -rf /usr/bin/yarn"
        sh "rm -rf /usr/bin/pnpm"
        sh "ln -s /opt/node-v20.9.0-linux-x64-glibc-217/bin/node /usr/bin/node"
        sh "ln -s /opt/node-v20.9.0-linux-x64-glibc-217/bin/npm /usr/bin/npm"
        sh "ln -s /opt/node-v20.9.0-linux-x64-glibc-217/bin/npx /usr/bin/npx"
        sh "ln -s /opt/node-v20.9.0-linux-x64-glibc-217/bin/yarn /usr/bin/yarn"
        sh "ln -s /opt/node-v20.9.0-linux-x64-glibc-217/bin/pnpm /usr/bin/pnpm"

        sh "# rm -rf node_modules"
        sh "# rm -rf yarn.lock"
        sh "# rm -rf package-lock.json"
        sh "# rm -rf pnpm-lock.yaml"
        sh "# pnpm config set registry https://registry.npmmirror.com"
        sh "# pnpm config set sharp_binary_host=https://npmmirror.com/mirrors/sharp"
        sh "# pnpm config set sharp_libvips_binary_host=https://npmmirror.com/mirrors/sharp-libvips"
        sh "# npm config set registry https://registry.npmjs.org"
        sh "# pnpm install"
        sh "npm run build:fast"
    }
    //dockerfile编译
    stage('Build docker') {
        try { sh "docker build -t shop-vite-dev:latest -f deploy/Dockerfile ." } catch (e) {}
        try { sh "docker images | grep none | xargs -t -r docker rmi" } catch (e) {}
        try { sh "docker rm -f shop-vite-dev" } catch (e) {}
        try { sh "docker run -it -d -p 9001:8080 --name shop-vite-dev shop-vite-dev:latest" } catch (e) {}
    }
}
