jenkins流水线学习(工具)

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

一、Pipeline Utility Steps

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

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 any
    stages {
        stage('env') {
            steps {
                script {
                    println env.JOB_NAME
                    println env.BUILD_NUMBER
                    println 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_read
                    println rd_json
                    println 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_json

                    writeJSON 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_file
                    println props
                }
            }
        }
        stage('Utility Steps method---readYaml') {
            steps {
                script {
                    yaml_file = env.WORKSPACE + "/test_jenkins.yaml"
                    y = readYaml file : yaml_file
                    println 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_path

                    writeYaml 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 any
    stages{
        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 any
    environment {
        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/GB18030
                    mimeType:'text/plain',        // or text/html
                    subject: "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_ID
                    result_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 " +  status
    result_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 any
    stages{
        stage("init") {
            steps{
                script {
                    json_file = "${env.WORKSPACE}/package.json"
                    file_contents = readFile json_file
                    println file_contents
                }
            }
        }
        stage("retry and sleep") {
            steps{
                script{
                    try {
                        retry(3) {
                            println "here we are test retry fuction"
                            sleep 5
                            println 10/0
                        }
                    }catch (Exception e) {
                        println e
                    }
                }
            }
        }
    }
}

五、pipeline basic steps(timeout 、waitUntil 、withEnv 

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

pipeline{
    agent any
    stages{
        stage("init") {
            steps{
                script {
                    // 设置超时5s,默认单位是分钟
                    timeout(time: 50, unit: 'SECONDS') {
                        //initialRecurrencePeriod :设置重试之间的初始等待时间(以毫秒为单位)。
                        // 默认为 250 毫秒。每次失败都会将尝试之间的延迟减慢至最多 15 秒
                        //quiet :如果为真,则每次检查条件时该步骤都不会记录消息。默认为假。类型: boolean
                        println "是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_NUMBER

pipeline{

    agent any
    stages{
        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) 

上一篇:16. Spring 事务和事务传播机制-1. 事务回顾


下一篇:Redis之路系列(5)功夫在诗外