pipeline jenkins流水线

Pipeline 是 Jenkins 中一种灵活且强大的工作流机制,它允许您以代码的形式来定义和管理持续集成和持续交付的流程。

Pipeline 的作用主要体现在以下几个方面:

  1. 可编排的构建流程:使用 Pipeline,您可以将一个或多个阶段(Stage)组合起来,形成一个完整的构建流程。每个阶段可以包含多个步骤,这些步骤可以是构建、测试、部署、通知等。这样,您可以通过代码编排构建过程,以满足特定的需求和流程要求。

  2. 可重复和可维护的构建配置:Pipeline 的配置是基于代码的,可以将其纳入版本控制系统进行管理。这样,您可以轻松地重用和共享构建配置,而不需要手动复制和粘贴配置。此外,Pipeline 也支持条件、循环、参数化等灵活的控制结构,使得配置更加灵活和可维护。

  3. 可视化的流水线视图:Jenkins Pipeline 提供了一个可视化的流水线视图,可以展示整个流程的执行情况、阶段的状态、构建日志等信息。通过流水线视图,您可以清晰地了解流程的进度和结果,并可以快速定位出现的问题。

  4. 支持多分支和多环境:Pipeline 允许您创建多个并行的或串行的流水线,以支持不同的分支和环境需求。例如,您可以定义针对不同分支的不同构建流程,也可以将流程调整为对应的测试、预发布和生产环境等。

总之,Pipeline 提供了一种结构化、可编排和可重复的方式来定义和管理软件交付流程,帮助实现持续集成和持续交付的自动化。它为持续交付提供了更高的可视化、可管理性和可扩展性,使软件交付过程更加可靠和可控。

流水线1

pipeline {
    agent any
    tools {
        maven 'Maven-3.8.8'
    }
    environment {
        codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"
    }
    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'
            }
        }
    }
}

在这里插入图片描述

流水线2:

推送镜像到harbor

pipeline {
    agent any
    tools {
        maven 'Maven-3.8.8'
    }
    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('Build Docker Image') {
            steps {
                sh 'docker image build . -t "${imageUrl}:${imageTag}"'
            }           
        }
        stage('Push Docker Image') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'harbor-user-credential', \
                        passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {
                    sh "echo ${harborUserPassword} | docker login -u ${env.harborUserName} --password-stdin ${harborServer}"
                    sh "docker image push ${imageUrl}:${imageTag}"
                }
            }   
        }        
    }
}

在这里插入图片描述

harborServer=‘harbor.luohw.net’ 是harbor域名

harbor-user-credential为harbor的认证凭证,在系统管理里面添加

条件执行流水线

pipeline {
    agent any
    parameters {
        booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')
    }
    tools {
        maven 'Maven-3.8.8'
    }
    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 {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Build Docker Image') {
            steps {
                sh 'docker image build . -t "${imageUrl}:${imageTag}"'

            }           
        }
        stage('Push Docker Image') {
            when {
                expression { params.pushImage }
            }
            steps {
                // input(message: 'continue?')
                withCredentials([usernamePassword(credentialsId: 'harbor-user-credential', passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {
                    sh "echo ${harborUserPassword} | docker login -u ${env.harborUserName} --password-stdin ${harborServer}"
                    sh "docker image push ${imageUrl}:${imageTag}"
                }
            }   
        }        
    }
}

自动触发流水线

pipeline {
    agent any
    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('Build Docker Image') {
            steps {
                sh 'docker image build . -t "${imageUrl}:${imageTag}"'
                // input(message: '镜像已经构建完成,是否要推送?')
            }           
        }
        stage('Push Docker Image') {
            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}"
                }
            }   
        }        
    }
}


使用curl 命令会自动触发流水线执行
 curl -X POST -H "Content-Type: application/json" -d '{ "ref": "refs/heads/main" }' -vs  http://192.168.1.51:8080/generic-webhook-trigger/invoke?token="fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat"

配置gitlab触发流水线
http://192.168.1.51:8080/generic-webhook-trigger

fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat #这个是流水线配置中triggers 中的token

在这里插入图片描述
点击测试成功触发流水线执行

示例: 发送邮件通知

pipeline {
    agent any
    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('Build Docker Image') {
            steps {
                sh 'docker image build . -t "${imageUrl}:${imageTag}"'
                // input(message: '镜像已经构建完成,是否要推送?')
            }           
        }
        stage('Push Docker Image') {
            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 {
        always {
            mail to: '2368756722@qq.com',
            subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
            body: "${env.BUILD_URL} has result ${currentBuild.result}"
        }
    }    
}

效果
在这里插入图片描述

钉钉通知

pipeline {
    agent any
    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('Build Docker Image') {
            steps {
                sh 'docker image build . -t "${imageUrl}:${imageTag}"'
                // input(message: '镜像已经构建完成,是否要推送?')
            }           
        }
        stage('Push Docker Image') {
            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 {
        always {
            dingtalk(
                robot: 'dingtalk222',#这个是钉钉id
                type: 'TEXT',
                text: [
                    "Status of pipeline: ${currentBuild.fullDisplayName}",
                    "${env.BUILD_URL} has result ${currentBuild.result}."
                ]
            )
        }
    }

}

企业微信通知,把下面替换上面面的post端即可

    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'
        }
    }

使用代码质量扫描

pipeline {
    agent any
    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') {
            steps {
                sh 'docker image build . -t "${imageUrl}:${imageTag}"'
                // input(message: '镜像已经构建完成,是否要推送?')
            }           
        }
        stage('Push Docker Image') {
            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'
        }
    } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<h3>回答1:</h3><br/>Jenkins流水线Pipeline)是一种基于脚本的持续集成和交付工具,它可以通过编写脚本来定义整个构建过程,包括构建、测试、打包、部署等环节。Pipeline可以帮助团队更好地管理和控制软件开发过程,提高开发效率和质量。同时,Pipeline还支持可视化的界面,方便用户查看和管理构建过程。 <h3>回答2:</h3><br/>Jenkins流水线Pipeline是一种自动化工具,它允许用户在Jenkins中创建、测试和部署软件的过程。 Pipeline可以通过定义该过程的阶段和任务来帮助用户更好地管理软件开发项目的生命周期。Pipeline支持多种编程语言,包括Java、Python和Ruby等。 Pipeline的基本单元是阶段。每个阶段定义了软件开发流程中的一组操作,例如拉取代码、运行单元测试、构建和部署软件等。 在Pipeline中,阶段是由任务组成的。任务表示可执行的操作,例如运行shell脚本、调用Jenkins插件或编译代码等。 Pipeline通过Jenkinsfile文件来定义软件开发过程。Jenkinsfile是一个Groovy脚本,它描述了软件开发阶段和任务的结构、依赖关系和输入输出。 Pipeline的一个显著特点是它可以支持持续交付和持续集成。持续交付指的是在软件生命周期中自动构建、测试和部署软件的过程,以便在任何时候可以将代码快速交付到生产环境中。持续集成指在代码库中提交新代码时自动构建和测试已有代码的过程,以便尽早发现潜在的问题和错误。 总之,PipelineJenkins中的一个强大的工具,可以帮助您更好地管理软件开发项目的生命周期。它通过定义软件开发过程中的阶段和任务,支持持续交付和持续集成,并以Jenkinsfile作为描述文件,可以方便地管理和维护软件开发过程。 <h3>回答3:</h3><br/>Jenkins是一款开源的自动化构建工具,它提供了丰富的插件来支持不同的构建方式和工具。其中流水线Pipeline插件是Jenkins的核心插件之一,它提供了一种声明式的DSL语言来实现复杂的连续构建流程和自动化测试。 Pipeline的概念是Jenkins提供的一个全新的构建方式,它允许用户定义完整的构建流程,从代码的检出到构建、测试和部署。和传统的构建方式不同,Pipeline通过将整个构建流程视为一个整体,提供了更多的自定义和可编程性。 Pipeline有两种实现方式,一种是基于Scripted Pipeline,另一种是基于Declarative Pipeline。Scripted Pipeline允许用户使用Groovy脚本来编写自己的构建流程,而Declarative Pipeline则提供了一种更加声明式的方式来定义构建流程。 Pipeline的核心是Stage和Step。Stage表示构建流程中的一个阶段,每个Stage实际上是一个具有清晰定义作用域的语句块,Step则是在Stage中执行的具体操作,常见的操作包括代码检出、编译、测试和部署等。用户可以根据自己的需求自由组合Stage和Step来构建自己的连续集成流水线Pipeline的优势在于其可扩展性和可重用性,用户可以通过自定义插件来扩展支持自己的构建工具和流程,也可以通过将常用的构建模板抽象出来来提高构建流程的可重用性,从而提高整个团队的效率。 综上所述,JenkinsPipeline插件为用户提供了一种全新的连续构建流程实现方式,让构建流程更加灵活和可控。它能够高效地管理构建流程的各个阶段,提高团队的协作效率,从而为项目的成功交付做出重要贡献。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值