首页 > 其他分享 >Jenkins pipeline 使用agent docker编译构建

Jenkins pipeline 使用agent docker编译构建

时间:2023-01-31 14:11:58浏览次数:50  
标签:TAG pipeline -- agent Dsonar HARBOR sh Jenkins docker

Jenkins 使用agent docker构建pipeline

  此处用于记录,使用jenkins pipeline构建时,使用docker启动一个agent来构建编译环境。

// 需要在jenkins的Credentials设置中配置jenkins-harbor-creds、jenkins-k8s-config参数
pipeline {
    agent any
    environment {
        HARBOR_CREDS = credentials('jenkins-harbor-creds')
        K8S_CONFIG = credentials('jenkins-k8s-config')
        SONAR_HOME = tool('sonar-scanner')
        HARBOR_HOST= credentials('harbor-host')
        // git tag 需要使用x.x.x的形式
        GIT_TAG = sh(returnStdout: true,script: "#!/bin/bash \n if [[ `git describe --tags --always` =~ '-g' ]] ;then echo 'latest' ; elif [[ `git describe --tags --always` =~ '.' ]] ; then echo `git describe --tags --always` ; else echo 'latest' ;fi").trim()
    }
    parameters {
        string(name: 'DOCKER_IMAGE', defaultValue: 'xx/xxx', description: 'docker镜像名')
        string(name: 'APP_NAME', defaultValue: 'xxx', description: 'k8s中标签名')
        string(name: 'K8S_DEV_NAMESPACE', defaultValue: 'xxx', description: '开发环境k8s的namespace名称')
        string(name: 'K8S_TEST_NAMESPACE', defaultValue: 'xxx', description: '测试环境k8s的namespace名称')
    }
    stages {
        stage('Maven Build') {
            agent {
                docker {
                    image 'maven:3-jdk-8-alpine'
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh 'mvn -U clean  org.jacoco:jacoco-maven-plugin:prepare-agent  package -Dfile.encoding=UTF-8'
                stash includes: 'target/', name: 'app'
            }

        }
        
        stage('Sonar') {
             steps {
                unstash 'app'
                 withSonarQubeEnv('sonar-server') {
                     sh 'echo ${SONAR_HOME} ${APP_NAME}'
                     sh '${SONAR_HOME}/bin/sonar-scanner -Dsonar.projectKey=${APP_NAME} -Dsonar.projectName=${APP_NAME} -Dsonar.language=java -Dsonar.java.binaries=./target/classes -Dsonar.sources=./src  -Dsonar.core.codeCoveragePlugin=jacoco  -Dsonar.dynamicAnalysis=reuseReports -Dsonar.surefire.reportsPath=target/surefire-reports -Dsonar.jacoco.reportPaths=target/jacoco.exec -Dsonar.jacoco.itReportPath=target/jacoco.exec -Dsonar.java.source=1.8'
                 }
             }
        }
        
        stage('Jacoco'){
            steps {
                jacoco()
            }
        }

        
        stage('Docker Build') {
            when { not { environment name: 'GIT_TAG', value: 'latest' } }
            steps {
                unstash 'app'
                sh "docker login -u ${HARBOR_CREDS_USR} -p ${HARBOR_CREDS_PSW} ${HARBOR_HOST}"
                sh "docker build --build-arg JAR_FILE=`ls target/*.jar |cut -d '/' -f2` -t ${HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG} ."
                sh "docker push ${HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG}"
                sh "docker rmi ${HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG}"
            }
            
        }
         stage('Deploy test') {
                    when { not { environment name: 'GIT_TAG', value: 'latest' } }
                    agent {
                        docker {
                            image 'lwolf/helm-kubectl-docker'
                        }
                    }
                    steps {
                        sh "mkdir -p ~/.kube"
                        sh "echo ${K8S_CONFIG} | base64 -d > ~/.kube/config"
                        sh "sed -e 's#{IMAGE_URL}#${HARBOR_HOST}/${params.DOCKER_IMAGE}#g;s#{IMAGE_TAG}#${GIT_TAG}#g;s#{APP_NAME}#${params.APP_NAME}#g;s#{SPRING_PROFILE}#test#g;s#{NAME_SPACE}#${params.K8S_TEST_NAMESPACE}#g' k8s-deployment.tpl > k8s-deployment.yml"
                        sh "kubectl delete -f k8s-deployment.yml --namespace=${params.K8S_TEST_NAMESPACE} --ignore-not-found"
                        sh "kubectl apply -f k8s-deployment.yml --namespace=${params.K8S_TEST_NAMESPACE}"
                    }
         }
    }
    post { 
        always { 
            cleanWs()
            script {
                wrap([$class: 'BuildUser']) {
                    manager.addShortText("${BUILD_USER}")
                    manager.addShortText("${APP_NAME}")
                    manager.addShortText("${TAG}")
                    manager.addShortText("${K8S_TEST_NAMESPACE}")
                }
                if (env.ACTION == "Deploy"){
                    def cr = new Date("${TAG}" as long).format("yyyyMMddHHmmss")
                    manager.addShortText(cr)
                }
            }
        }
    }
}

 

标签:TAG,pipeline,--,agent,Dsonar,HARBOR,sh,Jenkins,docker
From: https://www.cnblogs.com/wushaoyu/p/17078784.html

相关文章