• pipeline agent分布式构建


    开启 agent

    root@jenkins:~/learning-jenkins-cicd/07-jenkins-agents# docker-compose -f docker-compose-inbound-agent.yml up -d

    Jenkins配置添加

    pipeline { 
        agent { label 'docker-jnlp-agent' }
        parameters {
            booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')
        }    
        tools {
            maven 'Maven-3.8.8'
        }
        triggers {
            GenericTrigger(
                genericVariables: [
                    [key: 'ref', value: '$.ref']
                ],
                token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',
                causeString: 'Triggered on $ref',
                printContributedVariables: true,
                printPostContent: true
            )
        }   
        environment {
            codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"
            harborServer='harbor.luohw.net'
            projectName='spring-boot-helloworld'
            imageUrl="${harborServer}/ikubernetes/${projectName}"
            imageTag='latest'
        }
        stages {
            stage('Source') {
                steps {
                    git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"
                }
            }
            stage('Build') {
                steps {
                    sh 'mvn -B -DskipTests clean package'
                }
            }
            stage('Test') {
                steps {
                    sh 'mvn test'
                }
            }
            stage("SonarQube Analysis") {
                steps {
                    withSonarQubeEnv('SonaQube-Server') {
                        sh 'mvn sonar:sonar'
                    }
                }
            }
            stage("Quality Gate") {
                steps {
                    timeout(time: 30, unit: 'MINUTES') {
                        waitForQualityGate abortPipeline: true
                    }
                }
            }        
            stage('Build Docker Image') {
    	    agent { label 'master' }
                steps {
                    sh 'docker image build . -t "${imageUrl}:${imageTag}"'
                    // input(message: '镜像已经构建完成,是否要推送?')
                }           
            }
            stage('Push Docker Image') {
    	    agent { label 'master' }
                when {
                    expression { params.pushImage }
                }
                steps {
                    withCredentials([usernamePassword(credentialsId: 'harbor-user-credential', passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {
                        sh "docker login -u ${env.harborUserName} -p ${env.harborUserPassword} ${harborServer}"
                        sh "docker image push ${imageUrl}:${imageTag}"
                    }
                }   
            }        
        }
        post{
            success{
                qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5530d220-0983-490e-ada5-a74fa66570c8'
            }
            failure{
                qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5530d220-0983-490e-ada5-a74fa66570c8'
            }
        } 
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    192.168.1.51:50000

    示例:基于Kubernetes完成构建

    添加pod模版
    kube-agent222

    maven:3.8.6-eclipse-temurin-17-alpine

    挂载路径
    /root/.m2

    配置pod模版
    在这里插入图片描述

    编辑流水线

    pipeline {
        agent {
            kubernetes {
                inheritFrom 'kube-maven'
            }
        }
        parameters {
            booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')
        }    
        tools {
            maven 'Maven-3.8.8'
        }
        triggers {
            GenericTrigger(
                genericVariables: [
                    [key: 'ref', value: '$.ref']
                ],
                token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',
                causeString: 'Triggered on $ref',
                printContributedVariables: true,
                printPostContent: true
            )
        }   
        environment {
            codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"
            harborServer='hub.magedu.com'
            projectName='spring-boot-helloworld'
            imageUrl="${harborServer}/ikubernetes/${projectName}"
            imageTag='latest'
        }
        stages {
            stage('Source') {
                steps {
                    git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"
                }
            }
            stage('Build') {
                steps {
                    container('maven') {
                        sh 'mvn -B -DskipTests clean package'
                    }
                }
            }
            stage('Test') {
                steps {
                    container('maven') {
                        sh 'mvn test'
                    }
                }
            }
            stage("SonarQube Analysis") {
                steps {
                    container('maven') {                
                        withSonarQubeEnv('SonarQube-Server') {
                            sh 'mvn sonar:sonar'
                        }
                    }
                }
            }
            stage("Quality Gate") {
                steps {
                    timeout(time: 30, unit: 'MINUTES') {
                        waitForQualityGate abortPipeline: true
                    }
                }
            }               
        }
        post{
            success{
                qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c79e3215-d39f-44a7-a760-fa0ab63ca46b'
            }
            failure{
                qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c79e3215-d39f-44a7-a760-fa0ab63ca46b'
            }
        }   
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    在这里插入图片描述

    pipeline基于k8s构建

    注意:只能一个k8s中的jenkins,不然有影响
    在这里插入图片描述

    在这里插入图片描述

    pipeline {
        agent {
            kubernetes {
                inheritFrom 'kube-maven'
            }
        }
        parameters {
            booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')
        }    
        triggers {
            GenericTrigger(
                genericVariables: [
                    [key: 'ref', value: '$.ref']
                ],
                token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',
                causeString: 'Triggered on $ref',
                printContributedVariables: true,
                printPostContent: true
            )
        }   
        environment {
            codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"
            harborServer='harbor.luohw.net'
            projectName='spring-boot-helloworld'
            imageUrl="${harborServer}/ikubernetes/${projectName}"
            imageTag='latest'
        }
        stages {
            stage('Source') {
                steps {
                    git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"
                }
            }
            stage('Build') {
                steps {
                    container('maven') {
                        sh 'mvn -B -DskipTests clean package'
                    }
                }
            }
            stage('Test') {
                steps {
                    container('maven') {
                        sh 'mvn test'
                    }
                }
            }
            stage("SonarQube Analysis") {
                steps {
                    container('maven') {                
                        withSonarQubeEnv('SonaQube-Server') {
                            sh 'mvn sonar:sonar'
                        }
                    }
                }
            }
            stage("Quality Gate") {
                steps {
                    timeout(time: 30, unit: 'MINUTES') {
                        waitForQualityGate abortPipeline: true
                    }
                }
            }               
        }
        post{
            success{
                qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c79e3215-d39f-44a7-a760-fa0ab63ca46b'
            }
            failure{
                qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c79e3215-d39f-44a7-a760-fa0ab63ca46b'
            }
        }   
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    maven-and-docker

    pipeline {
      agent { 
        kubernetes {
          inheritFrom 'maven-and-docker'
          }
        }  
      triggers {
        GenericTrigger(
          genericVariables: [
            [key: 'ref', value: '$.ref']
          ],
          token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',
          causeString: 'Triggered on $ref',
          printContributedVariables: true,
          printPostContent: true
        )
      }   
      environment {
        codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"
        harborServer='harbor.luohw.net'
        projectName='spring-boot-helloworld'
        imageUrl="${harborServer}/ikubernetes/${projectName}"
        imageTag="${BUILD_ID}"
      }
      stages {
        stage('Source') {
          steps {
            git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"
          }
        }
        stage('Build') {
          steps {
            container('maven') {
              sh 'mvn -B -DskipTests clean package'
            }
          }
        }
        stage('Test') {
          steps {
            container('maven') {
              sh 'mvn test'
            }
          }
        }
        stage("SonarQube Analysis") {
          steps {
            container('maven') {                
              withSonarQubeEnv('SonaQube-Server') {
                sh 'mvn sonar:sonar'
              }
            }
          }
        }
        stage("Quality Gate") {
          steps {
            timeout(time: 30, unit: 'MINUTES') {
              waitForQualityGate abortPipeline: true
            }
          }
        } 
        stage('Build Image') {
          steps {
            container('dind') {
              sh 'docker image build -t ${imageUrl}:${imageTag} .'
            }
          }
        }
        stage('Push Image') {
          steps {
            withCredentials([usernamePassword(credentialsId: 'harbor-user-credential', passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {
              container('dind') {
                sh '''        #当在一个内部需要运行多条shell命令的时候,可以三引号,不必每一步写shell
                  echo  ${harborUserPassword} | docker login -u ${harborUserName} --password-stdin ${harborServer}
                  docker image push ${imageUrl}:${imageTag}
                  docker image tag ${imageUrl}:${imageTag} ${imageUrl}:latest 
                  docker image push ${imageUrl}:latest
                '''
              }
            }
          }
        }
      }  
      post {
        always {
          mail to: '2368756722@qq.com',
          subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
          body: "${env.BUILD_URL} has result ${currentBuild.result}"
        }
      }      
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    添加pod模版
    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    【python】爬虫记录每小时金价
    Go Quiz: 从Go面试题看recover注意事项第1篇
    关于面试--【namenode&fsimage&edits】
    TDengine3.0计算查询引擎的优化与升级
    MacOS安装conda
    DaoWiki(基于Django)开发笔记 20231113-2
    Docker简单使用总结
    通过 Azure 日志分析加强云安全
    3C电子胶黏剂在手机制造方面有哪些关键的应用
    Java并发基石—CAS原理实战
  • 原文地址:https://blog.csdn.net/m0_37749659/article/details/134460633