Pipeline 是 Jenkins 中一种灵活且强大的工作流机制,它允许您以代码的形式来定义和管理持续集成和持续交付的流程。
Pipeline 的作用主要体现在以下几个方面:
-
可编排的构建流程:使用 Pipeline,您可以将一个或多个阶段(Stage)组合起来,形成一个完整的构建流程。每个阶段可以包含多个步骤,这些步骤可以是构建、测试、部署、通知等。这样,您可以通过代码编排构建过程,以满足特定的需求和流程要求。
-
可重复和可维护的构建配置:Pipeline 的配置是基于代码的,可以将其纳入版本控制系统进行管理。这样,您可以轻松地重用和共享构建配置,而不需要手动复制和粘贴配置。此外,Pipeline 也支持条件、循环、参数化等灵活的控制结构,使得配置更加灵活和可维护。
-
可视化的流水线视图:Jenkins Pipeline 提供了一个可视化的流水线视图,可以展示整个流程的执行情况、阶段的状态、构建日志等信息。通过流水线视图,您可以清晰地了解流程的进度和结果,并可以快速定位出现的问题。
-
支持多分支和多环境: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'}}
}