pipeline jenkins流水线

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

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

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

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

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

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

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

流水线1

pipeline {agent anytools {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 anytools {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 anyparameters {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 anyparameters {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 anyparameters {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 anyparameters {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',#这个是钉钉idtype: '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 anyparameters {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.mzph.cn/news/145468.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#设置数据库索引

C#设置数据库索引 C# ASP.NET Core微服务设置数据库索引 C# ASP.NET Core微服务 设置数据库索引 设置数据库索引时,无需将.OnDelete(DeleteBehavior.Cascade)改为 .OnDelete(DeleteBehavior.NoAction)改变删除行为,向数据库添加索引的方法 modelBuild…

微信抽奖活动怎么做

微信抽奖活动:打破传统,创新互动,带给你超乎想象的惊喜体验! 随着互联网的飞速发展,人们越来越热衷于参与各种线上活动。而微信,作为中国最大的社交平台之一,自然成为了各种活动的聚集地。今天…

Android设计模式--状态模式

真知即所以为行,不行不足谓之知 一,定义 当一个对象的内在状态改变时,允许改变其行为,这个对象看起来像是改变了其类。 这么说可能很难理解,通俗来讲就是当一个对象它有多种状态的时候,把每一种状态的行为…

集合的removeIf方法

删除单个指定元素 如下:删除list中的字符串 “123” list.removeIf("123"::equals);删除另一个集合内包含的元素 即删除交集元素。 如:listA删除在listB中同样存在的元素: listA.removeIf(listB::contains);

大数据毕业设计选题推荐-机房信息大数据平台-Hadoop-Spark-Hive

✨作者主页:IT研究室✨ 个人简介:曾从事计算机专业培训教学,擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…

本地MQTT协议消息服务远程连接教程介绍

Mosquitto是一个开源的消息代理,它实现了MQTT协议版本3.1和3.1.1。它可以在不同的平台上运行,包括Windows、Linux、macOS等。mosquitto可以用于物联网、传感器、移动应用程序等场景,提供了一种轻量级的、可靠的、基于发布/订阅模式的消息传递…

高标准农田数字孪生

高标准农田是指通过土地整治、土壤改良、水利设施、农电配套、机械化作业等措施,提升农田质量和生产能力,达到田块平整、集中连片、设施完善、节水高效、宜机作业、土壤肥沃、生态友好、抗灾能力强、与现代农业生产和经营方式相适应的旱涝保收、稳产高产…

office2010图标显示不正常

问题:安装office2010软件后,.doc文件图标不正常,而.docx文件图标正常 ! 解决方法:亲测有效 1、使用快捷键WinR,打开“运行”,输入regedit,按回车键。 2、对于 Word 2007/Word 2010…

【docker启动的Jenkins时,遇到时区问题处理】

1、查看容器中的时区 [rootlocalhost jenkins]# docker exec -it jenkins cat /etc/timezone Etc/UTC而本地使用的是Asia/shanghai [rootlocalhost jenkins]# timedatectl | grep Time zoneTime zone: n/a (CST, 0800)###查看 [rootlocalhost jenkins]# cd /usr/share/zoneinf…

【Coppeliasim仿真】 坐标系间平滑插补

在仿真环境中控制两个参考框架(ReferenceFrame1和ReferenceFrame2)之间进行平滑的插值运动。在两个参考框架之间插值运动的过程中,使用了两种不同的方法,通过设置useMethodNb来选择使用的方法。 方法1使用了旋转轴和角度的计算&a…

Windows 小狼毫 如何在安装后指定用户配置文件文件夹

Windows 小狼毫 如何在安装后指定用户配置文件文件夹 打开小狼毫的安装目录,双击目录中的 WeaselSetup.exe 就会弹出截图中的那个窗口,然后指定你的用户文件夹目录就可以了

云ES高级监控告警

一、高级监控告警配置 1.1 操作入口 1.2 配置告警模块 1.2.1 新建联系人 添加联系人后,需要进行激活 1.2.2 新建联系人组 1.2.3 创建报警组 1.2.4 新建报警规则

鸿蒙4.0真机调试踩坑

传言鸿蒙next版本将不再兼容Android,所以领导安排做下鸿蒙开发的调研工作。 鸿蒙开发指南其实已经非常的友好了。但是鸿蒙开发本身还是有些坑要踩,这篇文章主要讲了鸿蒙真机调试问题。 目前手上的真机为华为 nova6,处理器为麒麟990.鸿蒙系统…

敲敲云与简道云流程设计引擎对比:选择更适合您的产品

在当今数字化时代,流程管理和自动化变得越来越重要。作为APaaS服务的两个知名产品,敲敲云和简道云都提供了流程设计引擎,帮助企业实现高效的流程管理。然而,在比较两者之后,您可能会发现敲敲云在多个方面具有优势&…

Github小彩蛋显示自己的README,git 个人首页的 README,readme基本语法

先上效果👇 代码在下面,流程我放最下面了,思路就是创建一个和自己同名的仓库,要公开,创建的时候会提示小彩蛋你的reademe会展示在你的首页,或许你在这个readme里面的修改都会在你的主页上看到了&#x1f44…

OpenLayer系列——【一】初识OpenLayer与OpenLayer视图操作

初识OpenLayer 1、初始化地图渲染 安装openlayer依赖 npm i ol首先准备一个容器用来渲染地图 <div id"map" ref"map" style"width: 100%; height: 100%" />导入依赖初始化地图 import ol/ol.css; import OSM from ol/source/OSM.js; …

芸鹰蓬飞:抖店服务分怎么快速升分?

在这个平台上&#xff0c;抖店服务分数的高低直接关系到商家在抖音平台上的曝光和信任度。那么&#xff0c;如何快速提升抖店服务分&#xff0c;成为了广大商家亟需解决的问题。本文将从多个角度&#xff0c;深入探讨提升抖店服务分的有效方法。 一、了解抖店服务分的评估标准 …

Vue实现封装自定义指令

目录 一、什么是自定义指令&#xff1f; 二、自定义指令的使用 Vue中的自定义指令使用Vue.directive函数进行定义。该函数接受两个参数&#xff0c;第一个是指令名称&#xff0c;第二个是指令选项对象。 上述代码中&#xff0c;我们定义了一个名为my-directive的自定义指令…

009.配置文件定制bash

在命令行中输入的绝大部分命令都可以放置在一个特殊的文件中&#xff0c;留待登录或启动新的bash会话时执行。将函数定义、别名以及环境变量设置放置在这种特殊文件中&#xff0c;是一种定制shell的常用方法。 # 定义ls命令使用的颜色 LS_COLORSno00:di01;46:ln00;36:pi40;33:…

深度解析:用Python爬虫逆向破解dappradar的URL加密参数(最详细逆向实战教程,小白进阶高手之路)

特别声明:本篇文章仅供学习与研究使用,不得用做任何非法用途,请大家遵守相关法律法规 目录 一、逆向目标二、准备工作三、逆向分析 - 太详细了!3.1 逆向前的一些想法3.1.1 加密字符串属性猜测3.1.2 是否可以手动复制加密API?3.2 XHR断点调试3.3 加密前各参数属性的变化情况…