pipeline { agent any environment { // 镜像名称 IMAGE_NAME = "node-service" // 工作目录 WS = "${WORKSPACE}" // 自定义构建参数 PROFILE = "prod" // docker网络 DOCKER_NETWORK = "inspur" } // 定义流水线的加工流程 stages { // 流水线的所有阶段 stage('1.环境检查与初始化') { steps { sh 'pwd && ls -alh' sh 'printenv' sh 'docker version' sh 'git --version' } } stage('2.编译') { agent { docker { image 'node:20-slim' } } steps { sh 'pwd && ls -alh' sh 'node -v' sh 'cd ${WS} && npm config set registry https://registry.npmmirror.com && yarn -v' sh 'yarn config set registry https://registry.npmmirror.com && yarn' } } stage('3.打包') { agent { docker { image 'node:20-slim' } } steps { sh 'pwd && ls -alh' sh 'node -v' sh 'npm run build:webpack' } } stage('4.部署') { // 删除容器和虚悬镜像 steps { sh 'pwd && ls -alh' sh 'docker rm -f ${IMAGE_NAME} || true && docker rmi $(docker images -q -f dangling=true) || true' sh 'docker network list | grep "${DOCKER_NETWORK}" && echo "docker network ${DOCKER_NETWORK} is exist" || docker network create ${DOCKER_NETWORK}' sh 'docker run -d --name ${IMAGE_NAME} --network ${DOCKER_NETWORK} ${IMAGE_NAME}' } } } }