pipeline { //pipeline 顶层 //any 在任何可用的agent上执行Pipeline或stage agent any //environment 自定义的环境变量 environment { //本项目此三个变量值如下,修改jenkins打包即可,其余不动 //PROGRAM_ID=integrated-eureka //APPLICATION_NAME=integrated-eureka //PROJECT_JAR_PATH=integrated-eureka PROGRAM_ID = "$PROGRAM_ID" //项目名称 APPLICATION_NAME = "$APPLICATION_NAME" //项目应用名-镜像名称 WORKSPACE = "/root/jenkins" //jenkins home 宿主机路径 PROJECT_JAR_PATH = "$PROJECT_JAR_PATH" //项目打包后所在相对路径 PROJECT_ROOT_PATH = "persagy-sync-data" //项目根路径名称 WORKER_HOME = "/root/$APPLICATION_NAME" //项目运行所在路径-容器内的路径 DEPLOY_FILE_PATH = "docker" //部署文件所在路径 MAVEN_PACKAGE_PATH = "$WORKSPACE/workspace/$PROGRAM_ID" //maven打包根路径 IMAGE_REPOSITORY_PREFIX="labisenlin.persagy.com" DOCKER_IMAGE = "$IMAGE_REPOSITORY_PREFIX/$IMAGE_REPOSITORY_PROJECT/$APPLICATION_NAME:$VERSION" //镜像全路径 BACKUP_PATH = "$JENKINS_HOME/program-backup/$PROGRAM_ID/$VERSION" //备份路径 } //不允许并行执行Pipeline,可用于防止同时访问共享资源等 options { disableConcurrentBuilds() } stages { //部署流程一:从SVN或者Git上拉取项目程序代码 stage('git checkout') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/$GIT_BRANCH']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'weiyizhong', url: 'http://39.106.8.246:3003/weiyizhong/persagy-sync-data.git']]]) } } //部署流程二:Java程序进行maven打包 stage('maven build') { steps { sh 'cd $WORKSPACE' sh 'mvn clean install -Dmaven.test.skip=true -f $MAVEN_PACKAGE_PATH/pom.xml' } } //部署流程三:根据项目dockerfile创建项目的docker镜像 stage('image buid') { steps { sh 'chmod -R +x ./$DEPLOY_FILE_PATH/' sh 'rm -rf ./$DEPLOY_FILE_PATH/*.jar && cp ./target/*.jar ./$DEPLOY_FILE_PATH' sh 'docker build -f ./$DEPLOY_FILE_PATH/Dockerfile --build-arg WORKER_HOME=$WORKER_HOME -t $DOCKER_IMAGE ./$DEPLOY_FILE_PATH' } } //部署流程四:项目推送到本地私有仓库 stage('repository push') { steps { //-H tcp://192.168.64.16:9999 sh 'docker login $IMAGE_REPOSITORY_PREFIX -u $DOCKER_USERNAME -p $DOCKER_PASSWORD' sh 'docker push $DOCKER_IMAGE' //只打镜像的话,将其打开 sh 'docker logout $IMAGE_REPOSITORY_PREFIX' sh 'docker rmi $DOCKER_IMAGE' } } } }