pipeline { agent any parameters { choice(name: 'ENV', choices: ['prod'], description: '部署环境') choice(name: 'SERVICE', choices: ['weichi-crm-gateway', 'weichi-crm-work', 'weichi-crm-oa', 'weichi-crm-authorization', 'weichi-crm-admin', 'weichi-crm-bi', 'weichi-crm-crm', 'weichi-crm-job', 'weichi-crm-examine', 'weichi-crm-hrm'], description: '选择要部署的微服务') } environment { DOCKER_REGISTRY = "registry.cn-shanghai.aliyuncs.com" DOCKER_NAMESPACE = "advich-group" DOCKER_REPO = "${params.SERVICE}" DOCKER_IMAGE = "${DOCKER_REGISTRY}/${DOCKER_NAMESPACE}/${DOCKER_REPO}:latest" } stages { stage('拉取Git代码') { steps { checkout scmGit(branches: [[name: '${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fb3553e7-6ea6-4f34-92a0-6ba34a71c5b5', url: 'https://git.dev.advichcloud.com/wangfan/weichi-crm-server.git']]) } } stage('Build') { steps { dir("${params.SERVICE}") { sh ''' set -e mvn clean package -DskipTests ''' } } } stage('Build and Push Docker Image') { steps { script { withCredentials([usernamePassword( credentialsId: '41a5dc65-0283-4d56-ace5-36bc08350b08', usernameVariable: 'REGISTRY_USERNAME', passwordVariable: 'REGISTRY_PASSWORD' )]) { sh """ docker build -t ${DOCKER_IMAGE} . echo "\${REGISTRY_PASSWORD}" | docker login ${DOCKER_REGISTRY} -u \${REGISTRY_USERNAME} --password-stdin docker push ${DOCKER_IMAGE} """ } } } } stage('Deploy to Server') { when { expression { return params.ENV == 'prod' } } steps { sshPublisher(publishers: [sshPublisherDesc(configName:'CRM(阿里云)', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: """ docker login -u wangfan@1934160439855919 -p Initial0 ${DOCKER_REGISTRY} docker pull ${DOCKER_IMAGE} docker rm -f ${DOCKER_REPO} || true docker run -d --name ${DOCKER_REPO} ${DOCKER_IMAGE} """, execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } post { always { cleanWs() } success { echo 'Deployment succeeded!' } failure { echo 'Deployment failed.' } } }