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: 'git@git.test.work: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}
        """
    }
    }
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐