12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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 = "$PROGRAM_ID" //项目应用名-镜像名称
- WORKSPACE = "/root/jenkins" //jenkins home 宿主机路径
- PROJECT_JAR_PATH = "$PROJECT_JAR_PATH" //项目打包后所在相对路径
- PROJECT_ROOT_PATH = "zkt-alarm" //项目根路径名称
- CONTAINER_HOME = "$CONTAINER_HOME" //容器内的基本路径
- WORKER_HOME = "$CONTAINER_HOME/$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: 'zkt-git-server', url: 'http://39.106.8.246:3003/persagy/persagy-zkt-project-alarm.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'
- }
- }
- //部署流程五:项目版本本地备份
- stage('backup') {
- steps {
- sh 'rm -rf $BACKUP_PATH/'
- sh 'mkdir -p $BACKUP_PATH/'
- sh 'cp ./target/*.jar $BACKUP_PATH/'
- }
- }
- //部署流程六:项目docker部署运行
- stage('deploy') {
- steps {
- sh 'rm -rf /root/jenkins/workspace/zkt-project-alarm/docker/.env'
- sh 'echo "tag = $VERSION" >> /root/jenkins/workspace/zkt-project-alarm/docker/.env'
- sh 'ssh -tt root@192.168.64.16 "rm -rf /mnt/zkt/zkt-project-alarm/ && mkdir -p /mnt/zkt/zkt-project-alarm/ && scp -r root@192.168.64.18:/root/jenkins/workspace/zkt-project-alarm/docker/ /mnt/zkt/zkt-project-alarm/docker/ && cd /mnt/zkt/zkt-project-alarm/ && sh ./docker/deploy.sh " '
- }
- }
- }
- }
|