Tomcat项目发版Pipeline

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: '70cxxx7-2xce-4xef-9xdf-a5ab6xxx198b', 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 install package -U -X -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 \
            upgrade \
            ${ServiceName} \
            --set image.tag=${shortGitCommit},image.repository=${DOCKER_HUB_ADDR}/project/${ServiceName} \
            --recreate-pods \
            --description "Upgrade -> ${shortGitCommit}" \
            stable/${ServiceName}
        else
            helm \
            install \
            --namespace=default \
            --name=${ServiceName} \
            --set image.tag=${shortGitCommit} \
            --description "Install -> ${shortGitCommit}" \
            stable/${ServiceName}
        fi
        """
    }
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值