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

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

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

http://www.lryc.cn/news/232718.html

相关文章:

  • 软件工程理论与实践 (吕云翔) 第六章 面向对象分析课后习题及其解析
  • langchain(1):使用LangChain 调用 openai 的 text/chat model
  • rabbitMQ的扇出模式(fanout发布订阅)的生产者与消费者使用案例
  • VSCode打开Json 文件格式化
  • 【C++】:STL——标准模板库介绍 || string类
  • Python小白之PyCharm仍然显示“No module named ‘xlwings‘”
  • 在Uni-app中实现计时器效果
  • Linux脚本shell中将Windos格式字符转换为unix
  • 【分布式】MIT 6.824 Lab 2B实现细节分析
  • MySql 数据库初始化,创建用户,创建数据库,授权
  • 【洛谷算法题】P5712-Apples【入门2分支结构】
  • vue项目中的js文件使用vuex
  • 【Vue3】 computed 完整写法 全选反选 、计算商品总价
  • Mindomo Desktop for Mac(免费思维导图软件)下载
  • Spark资源规划-资源上线评估
  • RT-Thread STM32F407 定时器
  • C#asp.net考试系统+sqlserver
  • mac上配置maven
  • 解决vue-cli node-sass安装不成功问题
  • 【Mysql】Mysql内置函数介绍
  • 【Linux】vscode远程连接ubuntu失败
  • 如何设计开发一对一交友App吸引更多活跃用户
  • UE基础篇六:音频
  • vscode+python开发之虚拟环境和解释器切换
  • vite 样式按需加载
  • Flutter打包iOS过程中pod访问github失败
  • 使用VMware安装linux虚拟机
  • Kafka、RocketMQ、RabbitMQ的比较总结Kafka、RocketMQ、RabbitMQ的比较总结
  • r语言plot函数
  • Notepad++ 和正则表达式 只保留自己想要的内容