当前位置: 首页 > article >正文

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


http://www.kler.cn/a/133265.html

相关文章:

  • Tessy学习笔记—requirement(需求)的管理
  • 利用python 检测当前目录下的所有PDF 并转化为png 格式
  • Unity音频导入设置
  • 论文 | On Second Thought, Let’s Not Think Step by Step!
  • python makedirs() 详解
  • html数据类型
  • Halcon (3):窗体常用语法使用
  • 无损音频播放软件 Colibri mac中文版特点介绍
  • 瑞吉外卖Day06
  • vue3-setup中如何通过ref调用子组件的函数
  • Vulkan渲染引擎开发教程 一、开发环境搭建
  • 二叉树最近公共祖先
  • 腾讯云服务器价格计算器真心好用,推荐给大家!
  • linux在非联网、无网络环境下,使用yumdownload、reportrack方法安装rpm包
  • oracle-buffer cache
  • Python调用企微机器人: 发送常用格式汇总
  • C++各种字符转换
  • 常用的软件架构设计模式
  • 【算法挨揍日记】day31——673. 最长递增子序列的个数、646. 最长数对链
  • python-opencv五种自动白平衡算法,附源码直接可用(均值、完美反射、灰度世界、动态阈值、基于图像分析的偏色检测及颜色校正)
  • Flutter笔记:Matrix4矩阵变换与案例
  • 安卓手机投屏到电视,跨品牌、跨地域同样可以实现!
  • hive sql 行列转换 开窗函数 炸裂函数
  • 对象与this
  • PS学习笔记——初识PS界面
  • sql server 多行数据合并一行显示