jenkins流水线学习(工具)

ops/2024/10/9 7:26:42/

Jenkins特意为了更好使用pipeline,开发了一些工具类,方便我们更好地在step中处理各种需求。

一、Pipeline Utility Steps

Pipeline Utility Steps
这是个插件,需要在jenkins插件管理安装。参考来源:Jenkins高级篇之Pipeline方法篇-Pipeline Utility Steps-2-方法readJSON和writeJSON_pipeline readjson-CSDN博客

1、findFiles    (需要在项目下要有log文件)

2、readJSON   (方法有两种参数,分别是文件路径和字符串;示例是文件路径;datas = readJSON text : json_string)

3、writeJSON   (file入参是文件路径,json可以是readJSON的结果,也可以是转换为json的字符串;示例是writeJSON的方式)

4、readProperties (读取properties文件,得到是一个map对象)

5、readYaml    (读取yaml文件,得到是一个map对象;有2种参数,同上面的readJson;   datas = readYaml text : yaml_string)

6、writeYaml    (入参是readYaml的结果,也可以是一个map)

pipeline {agent anystages {stage('env') {steps {script {println env.JOB_NAMEprintln env.BUILD_NUMBERprintln env.WORKSPACE}}}stage('Utility Steps method---findFiles') {steps {script {files = findFiles(glob: '**/*.log')println files[0].name}}}stage('Utility Steps method---readJSON') {steps {script {file_path_read = env.WORKSPACE + "/package.json"rd_json = readJSON file : file_path_readprintln rd_jsonprintln rd_json.name}}}stage('Utility Steps method---writeJSON') {steps {script {file_path_write = env.WORKSPACE + "/test_jenkins.json"input_json =  env.WORKSPACE + "/package.json"input = readJSON file : input_jsonwriteJSON file: file_path_write, json: input}}}stage('Utility Steps method---readProperties') {steps {script {properties_file = env.WORKSPACE + "/test_jenkins.properties"props = readProperties interpolate: true, file: properties_fileprintln props}}}stage('Utility Steps method---readYaml') {steps {script {yaml_file = env.WORKSPACE + "/test_jenkins.yaml"y = readYaml file : yaml_fileprintln y}}}stage('Utility Steps method---writeYaml') {steps {script {yaml_path = env.WORKSPACE + "/test_jenkins_write.yaml"input_path = env.WORKSPACE + "/test_jenkins.yaml"input_map = readYaml file : input_pathwriteYaml file: yaml_path , data: input_map}}}}
}

二、pipeline basic steps


这是一个最基础的pipeline组件,这个也是一个独立的插件,在安装pipeline的时候默认会自动安装
官网:Pipeline: Basic Steps

1、deleteDir()
  这个是默认递归删除WORKSPACE下的文件和文件夹
  使用场景:做完了每一个stage里面的事情,我们需要在post{...}里面写一些clean up操作
2、dir()
  这个方法是改变当前的工作目录
3、error()
  遇到这个方法,该stage和后面的stage都会显示失败
4、fileExists
  判断一个文件是否存在,返回值是布尔类型
5、isUnix()
  判断当前运行的Jenkins node环境是不是linux环境
6、pwd()
  linux是pwd,windows上是dir。所以这个插件就干脆支持一个方法,统称为pwd()

pipeline{agent anystages{stage("dir") {steps{// 如果没有这个目录,会自动创建这个目录dir("${env.WORKSPACE}/testdata"){sh "pwd"}}}stage("fileExists") {steps{script {json_file = "${env.WORKSPACE}/test_jenkins.log"if(fileExists(json_file) == true) {echo("log file is exists")}else {error("here haven't find log file")}}}}stage("isUnix") {steps{script {if(isUnix() == true) {echo("this jenkins job running on a linux-like system")}else {error("the jenkins job running on a windows system")}}}}stage("pwd()") {steps{script {sh("pwd")println "==========="println pwd()}}}}post {always {script {//写相关清除/恢复环境等操作代码//deleteDir()println "结束"}}}
}

三、pipeline basic steps(邮件)

pipeline{agent anyenvironment {to_email = "yin921125@qq.com"to_email_address_list = "yin921125@qq.com,yin921125@163.com"}stages{stage("发邮件demo 1") {steps{script {mail to: to_email,subject: "Running Pipeline: ${currentBuild.fullDisplayName}",body: "Something is wrong with ${env.BUILD_URL}"}}}stage("发邮件demo 2") {steps{script {mail to: to_email,cc: 'xxxxx@qq.com',            // 抄送charset:'UTF-8',            // or GBK/GB18030mimeType:'text/plain',        // or text/htmlsubject: "Running Pipeline: ${currentBuild.fullDisplayName}",body: "Something is wrong with ${env.BUILD_URL}, just for test send mail via pipeline code"}}}stage("发邮件demo 3") {steps{script {subject = "Jenkins Job : " + env.JOB_NAME + "/" + env.BUILD_IDresult_url = env.BUILD_URL + "console"text = """<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body><div><h1>Summary</h1><div><h2>Jenkins Build</h2><ul><li>Job URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li><li>Build Result URL : <a href='${result_url}'>${result_url}</a></li></ul></div><div><h2>GIT Branch</h2><ul><li>这是一个测试分支</li></ul></div></div></body></html>"""mail body: text, subject: subject,  mimeType: 'text/html', to: to_email_address_list}}}}post{always{script {module_test = load env.WORKSPACE + "/test_groovy.groovy"}}failure {script {module_test.send_email_results("Failed","Master",to_email_address_list)}}success {script {module_test.send_email_results("Success","Master",to_email_address_list)}}}
}

上面引用的test_groovy.groovy如下

def send_email_results(status,GITBranch,to_email_address_list) {subject = "Jenkins Job : " + env.JOB_NAME + "/" + env.BUILD_ID + " has " +  statusresult_url = env.BUILD_URL + "console"text = """<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body><div><h1>Summary</h1><div><h2>Jenkins Build</h2><ul><li>Job URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li><li>Build Result URL : <a href='${result_url}'>${result_url}</a></li></ul></div><div><h2>GIT Branch</h2><ul><li>这是测试引用其他groovy文件的邮件${GITBranch}</li></ul></div></div></body></html>"""mail body: text, subject: subject,  mimeType: 'text/html', to: to_email_address_list
}return this

 四、pipeline basic steps(readFile、retry、sleep)

1、readFile,读取文件内容(两个参数,第一个是file的path,第二个是encoding,默认是根据你当前平台的编码)
  如果不知道文件是什么格式(格式例如txt,log,json,properties,config,yaml),则推荐用readFile
  如果拿来读一个图片或者音频视频文件,读取二进制文件会采用Base64转码的字符串输出。
2、retry和sleep
  retry只能在出现异常的地方才能使用,否则只跑一遍就结束
  retry(3)这个3是一个整数,表示尝试的次数,sleep中的2表示2秒,注意这单位是秒

pipeline{agent anystages{stage("init") {steps{script {json_file = "${env.WORKSPACE}/package.json"file_contents = readFile json_fileprintln file_contents}}}stage("retry and sleep") {steps{script{try {retry(3) {println "here we are test retry fuction"sleep 5println 10/0}}catch (Exception e) {println e}}}}}
}

五、pipeline basic steps(timeout 、waitUntil 、withEnv 

1、timeout (超时则aborted)
2、waitUntil (直到返回true才跳出,否则一直重试)
3、withEnv (添加环境变量)

pipeline{agent anystages{stage("init") {steps{script {// 设置超时5s,默认单位是分钟timeout(time: 50, unit: 'SECONDS') {//initialRecurrencePeriod :设置重试之间的初始等待时间(以毫秒为单位)。// 默认为 250 毫秒。每次失败都会将尝试之间的延迟减慢至最多 15 秒//quiet :如果为真,则每次检查条件时该步骤都不会记录消息。默认为假。类型: booleanprintln "是unix系统吗" + isUnix()waitUntil(initialRecurrencePeriod: 1000,quiet: true){script {isUnix()}}}}}}stage("env") {steps{script {withEnv(['java_home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el6_10.i386/jre']) {sh("$java_home/bin/java -version")println "test with withEnv feature"}}}}}
}

 六、pipeline basic steps(writeFile

println env.JOB_NAME
println env.BUILD_NUMBERpipeline{agent anystages{stage("writeFile demo") {steps{script {write_file_path = "${env.WORKSPACE}/test_jenkins.txt"file_contents = "Hello Anthony!! 这是一个测试例子"// 写文件writeFile file: write_file_path, text: file_contents, encoding: "UTF-8"// 读文件fileContents = readFile file: write_file_path, encoding: "UTF-8"println fileContents}}}}
}

参考:jenkins流水线学习二(工具) - whitewall - 博客园 (cnblogs.com) 


http://www.ppmy.cn/ops/14098.html

相关文章

原生小程序自定义vantUI中van-collapse手风琴组件的标题

可以根据官网的提示&#xff1a; Vant Weapp - 轻量、可靠的小程序 UI 组件库 自己做的&#xff1a; <van-collapse accordion value"{{ activeName }}" bind:change"onChange"><van-collapse-item name"{{index}}"><!-- 这是自…

MybatisPlus 常用注解

一、前言 Mybatis-Plus是一个在Mybatis基础上进行增强的ORM框架&#xff0c;提供了许多便捷的注解来简化数据库操作。本文将介绍Mybatis-Plus常用的注解以及它们的使用方法。 二、常用注解 2.1、TableName TableName注解用于指定实体类对应的数据库表名。使用该注解可以省去…

No spring.config.import property has been defined

运行Springcloud项目出现下面错误&#xff1a; Description: No spring.config.import property has been defined Action: Add a spring.config.importnacos: property to your configuration. If configuration is not required add spring.config.importoptional:nac…

CISSP通关学习笔记:共计 9 个章节(已完结)

1. 笔记说明 第 0 章节为开篇介绍&#xff0c;不包括知识点。第 1 - 8 章节为知识点梳理汇总&#xff0c;8 个章节的知识框架关系如下图所示&#xff1a; 2. 笔记目录 「 CISSP学习笔记 」0.开篇「 CISSP学习笔记 」1.安全与风险管理「 CISSP学习笔记 」2.资产安全「 CISSP…

Centos Top 30常用命令及详解

在Linux的众多发行版中&#xff0c;CentOS以其稳定性和高效性备受青睐。掌握CentOS的操作对于系统管理员来说至关重要&#xff0c;而熟悉一系列核心命令更是提高工作效率的关键。从文件管理到系统监控&#xff0c;我们精心编制的Top 50常用CentOS命令列表是每位Linux用户的实战…

Linux系统安全:从面临的攻击和风险到安全加固、安全维护策略(文末有福利)

1. Linux面临的攻击与风险 1.1. Linux系统架构 Linux系统架构解读&#xff1a; 用户之间隔离内核态与用户态之间隔离用户进程一般以低权限用户运行系统服务一般以特权服务运行用户态通过系统调用进入内核态内核对系统资源进行管理和分配 1.2. Linux系统常见安全威胁 1.2.1.…

Android 获取sha1的快速有效的简单方法

第一步 下载apk 点击下载&#xff08;https://download.csdn.net/download/xiaohui2015/9751428&#xff09; 第二步 试用Windows键R键打开命令行 输入 adb install把apk用鼠标左键拖动到命令行自动输入路径 回车安装 第三步 打开apk 选择你要查看的apk&#xff0c;一键…

流程图画图规范

流程图画图规范 问题描述解决办法 问题描述 记录一下流程图画图规范 解决办法 1.使用标准形状&#xff1a; 矩形框&#xff08;通常表示处理步骤或操作&#xff09;。 菱形框&#xff08;表示决策点&#xff0c;两个或更多出口路径&#xff0c;通常标有“Yes”和“No”&…