欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot项目发版Pipeline

程序员文章站 2022-05-08 17:37:45
...
def ServiceName    = "demo"
def ProjectDir     = "demo"
def DockerfileDir  = "project/demo"
 
def gitCommit
def gitBranch
def shortGitCommit
 
node {
    def DOCKER_HUB_ADDR='registry.test.inside'
 
    stage('Git Clone and  setup') {
        dir("docker") {
            git credentialsId: 'Testgitlab', url: 'http://git.test.work/ops/cicd.git', branch: 'test'
        }
        dir("source") {
            def myRepo = checkout([$class: 'GitSCM',
                branches: [[name: '*/test']],
                userRemoteConfigs: [[credentialsId: '70cxxx-2xce-4xef-94xf-a5axxxaa198b', url: '[email protected]:project/demo.git']]
                ])
        gitCommit = myRepo.GIT_COMMIT
        gitBranch = myRepo.GIT_BRANCH
        shortGitCommit = "${gitCommit[0..5]}"
        }
    }
    stage('Build') {
      try {
          sh """
          mvn -s /etc/maven/settings.xml -f source/pom.xml clean -X package -Ptest -Dmaven.test.skip=true
          """
      }
      catch (exc) {
        println "Failed to test - ${currentBuild.fullDisplayName}"
        throw(exc)
      }
    }
    stage('Sonarqube') {
          sh """
          mvn -s /etc/maven/settings.xml -f source/pom.xml sonar:sonar -Dsonar.organization="test" -Dsonar.projectKey=${ServiceName} \
		  -Dsonar.projectName=${ServiceName} -Dsonar.host.url=http://sonar.test.work:9000 -Dsonar.login=xxxxxe2c63f
          """
    }
    stage('Publish Docker') {
        withCredentials([[$class: 'UsernamePasswordMultiBinding',
            credentialsId: 'dockerhub',
            passwordVariable: 'DOCKER_HUB_PASSWORD',
            usernameVariable: 'DOCKER_HUB_USER']]) {
            sh """
            docker login ${DOCKER_HUB_ADDR} -u ${DOCKER_HUB_USER} -p ${DOCKER_HUB_PASSWORD}
            docker build \
            --build-arg app=${ServiceName} \
            -t ${DOCKER_HUB_ADDR}/project/${ServiceName}:${shortGitCommit} \
            -f docker/${DockerfileDir}/Dockerfile \
            source/${ProjectDir}/target
            docker push ${DOCKER_HUB_ADDR}/project/${ServiceName}:${shortGitCommit}
            """
        }
    }
    stage('Deploy to test') {
        sh """
        helm init \
        --client-only \
        --stable-repo-url \
        http://10.20.10.106:8080
 
        helm repo update
 
        if ! helm ls --output=yaml |grep "Chart: ${ServiceName}-0";then
            helm install \
            --namespace=default \
            --name=${ServiceName} \
            --set image.tag=${shortGitCommit} \
            --description "Install -> ${shortGitCommit}" \
            stable/${ServiceName}
        fi
        """
         
        def NodePort=sh([script: "kubectl get svc -n default -l app.kubernetes.io/name=${ServiceName} -o jsonpath='{.items[0].spec.ports..nodePort}'",returnStdout:true]).trim()
 
        sh """
        helm upgrade ${ServiceName} \
        --set image.repository=${DOCKER_HUB_ADDR}/project/${ServiceName},image.tag=${shortGitCommit},port.containerPort=${NodePort} \
        --description "Upgrade -> ${shortGitCommit}" \
        stable/${ServiceName}
        """
    }
    }
相关标签: pipeline