Jenkins Shared Library
Jenkins Pipeline 共享库介绍
目前 Groupama Jenkins Pipeline 共享库提供了 5 个入口方法,方法如下:
build:代码构建
checkout:代码拉取
codescan:代码扫描
notify:结果通知
logger:执行日志
build 代码构建
该方法主要用来对重复性的构建命令进行服用,目前支持 maven、gradle 两种方式进行构建。
使用方法
方法签名:Build(String buildType, String buildParams)
buildType:构建工具类型,支持 maven、gradle 两种方式
buildParams:构建参数,如:mvn clean install、gradle build
maven
#!groovy @Library('shared-lib') _ def build = new org.devops.build() pipeline { stages {stage('Build') { script {build.Build('maven', 'clean install') } } } }
gradle
#!groovy @Library('shared-lib') _ def build = new org.devops.build() pipeline {stages { stage('Build') { script {build.Build('gradle', 'build') } } } }
checkout 代码取
该方法主要用来对应用源码进行拉取,目前支持从 git、svn 两种版本管理工具中拉取代码。
使用方法
Checkout(String scmType, String url, String branchName, String credentialsId)
scmType:版本管理工具类型,支持 git、svn 两种
url:源码地址
branchName:分支名称
credentialsId:凭证 ID
git
#!groovy @Library('shared-lib') _ def checkout = new org.devops.checkout() pipeline { stages {stage('Checkout') { script {checkout.Checkout('git', 'https://github.com/727474430/web-logging-spring-boot-starter', 'master', 'tobo_wl') } } } }
svn
#!groovy @Library('shared-lib') _ def checkout = new org.devops.checkout() pipeline { stages {stage('Checkout') { script {checkout.Checkout('svn', 'https://10.28.1.160/svn/sourcecode/platform/code/groupama-microservice-v2', '', 'tobo_wl') } } } }
codescan 代码扫描
该方法主要用来对后端代码执行静态文件扫描,对应用中不符合规则的代码进行检索并提示,若代码不符合规范将中断执行流水线任务。
使用方法
SonarScan(String projectKey, String projectVersion, String javaVersion, String modules, String branchName)
projectKey:项目标识(名称)
projectVersion:项目版本
javaVersion:JDK 版本
modules:子模块名称,如果有多个使用 ‘,’ 分割
branchName:分支名称,默认 master
codescan
#!groovy @Library('shared-lib') _ def codescan = new org.devops.codescan() pipeline { stages {stage('SonarScan') { script {codescan.SonarScan('ticket', 'v1.0', '1.8', 'ticket-backend,ticket-common', 'dev') } } } }
logger 执行日志
该方法用来在流水线成打印不同级别的日志,通过不同颜色区分,目前支持 info、warning、error 三种级别
使用方法
Info(String message)、Warning(String message)、Error(String message)
message:消息内容
info
#!groovy @Library('shared-lib') _ def logger = new org.devops.logger() pipeline { stages {stage('Example') { script {logger.Info(" 这是一段 Info 日志 ") } } } }
warning
#!groovy @Library('shared-lib') _ def logger = new org.devops.logger() pipeline { stages {stage('Example') { script {logger.Warning(" 这是一段 Warning 日志 ") } } } }
error
#!groovy @Library('shared-lib') _ def logger = new org.devops.logger() pipeline { stages {stage('Example') { script {logger.Error(" 这是一段 Error 日志 ") } } } }
notify 结果通知
该方法用来发送消息通知,一般在构建结果阶段使用。目前支持钉钉、邮件两种方式。
使用方法
DingTalkNotify(String webhook, String jobName, String status, String version)
webhook:钉钉机器人 webhook
jobName:任务名称
status:执行状态
version:应用版本
MailNotify(String to, String status)
to:收件人邮箱
status:执行状态
钉钉
#!groovy @Library('shared-lib') _ def notify = new org.devops.notify() pipeline { stages {stage('Notification') { script {// ...} } } post { success { script {notify.DingTalkNotify("2kjdkj2jfk32ll","${JOB_NAME}", " 构建成功 ✅", "master") } } failure { script {notify.DingTalkNotify("2kjdkj2jfk32ll","${JOB_NAME}", " 构建失败 ❌", "master") } } } }
邮件
#!groovy @Library('shared-lib') _ def notify = new org.devops.notify() pipeline { stages {stage('Notification') { script {// ...} } } post { success { script {tool.MailNotify("727474430@qq.com", " 构建成功 ✅") } } failure { script {tool.MailNotify("727474430@qq.com", " 构建失败 ❌") } } } }
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!