胡建-Jenkins-Java后端j

pipeline {
    // 选择标签为maven的模板
    agent { node { label "maven" } }
    options {
        buildDiscarder(logRotator(numToKeepStr: '3'))
    }
    parameters {
          gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH_TAG'
          choice choices: ['update', 'deploy'], description: 'deploy-首次部署  update-更新服务', name: 'OPERATE'
          choice choices: ['fujian'], description: '请选择需要发布的名称空间', name: 'NS_NAME'
          string(name: 'JAVA_PARAMS', defaultValue: '--spring.profiles.active=fj --NACOS_HOST=nacos-service.tools --NACOS_PORT=8848 --NACOS_NAMESPACE=fujian', description: '请输入配置参数')
    }
    stages{
      stage('代码拉取') {
        steps {
             script {
                  checkout scmGit(branches: [[name: '${BRANCH}']], extensions: [], userRemoteConfigs: [[credentialsId: 'aliyun', url: 'https://codeup.aliyun.com/glr/cems/cems-auth.git']])
                  env.BUILD_TAG = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                  env.APP_NAME = sh(returnStdout: true, script: "git config -l|git config -l|grep remote.origin.url|awk -F/ '{print \$NF}'|cut -d. -f1").trim()
             }
          }
        }
      stage('代码编译') {
        steps {
             script {
                  sh "mvn clean install package -Dmaven.test.skip=true"
             }
          }
        }
      stage('镜像构建、上传') {
        steps {
             script {
                withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerHubPassword',usernameVariable: 'dockerHubUser')]) {
                sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} harbor.glroad.com:8800"
                sh "docker build -t harbor.glroad.com:8800/cems/${APP_NAME}:${BUILD_TAG} ."
                sh "docker push harbor.glroad.com:8800/cems/${APP_NAME}:${BUILD_TAG}"
             }
           }
         }
       }
      stage('部署&更新') {
        steps {
            sh'''
               if [ "$OPERATE" = "deploy" ];then
                   apt update
                   apt-get install wget
                   wget 120.46.68.181/yaml/k8s-cems-auth.yaml
                   sed -i \"s/<APP_NAME>/${APP_NAME}/g\" k8s-cems-auth.yaml
                   sed -i \"s/<BUILD_TAG>/${BUILD_TAG}/g\" k8s-cems-auth.yaml
                   sed -i \"s/<NS_NAME>/${NS_NAME}/g\" k8s-cems-auth.yaml
                   sed -i \"s/<PARAMS>/${JAVA_PARAMS}/g\" k8s-cems-auth.yaml
                   kubectl apply -f k8s-cems-auth.yaml --record
               else
                   kubectl set image deployment/${APP_NAME} ${APP_NAME}=harbor.glroad.com:8800/cems/${APP_NAME}:${BUILD_TAG} -n ${NS_NAME} --record
               fi
             '''
         }
       }
    }
}

 

 

posted @ 2024-03-09 13:06  beawh  阅读(2)  评论(0编辑  收藏  举报