【Jenkins】一种用纯Bash实现参数传递的Jenkinsfile的写法

groovy 复制代码
 /**
 * Auther: luojiaao
 */

 /**
 * run command.
 *
 * This function executes shell based on the operating system.
 *
 * @param m Map containing trace/returnStdout/returnStatus/sops_var.
 * @return Output of the command.
 */
run_vars = [:]
def run(Map m) {
    is_unix = isUnix()
    
    // Run Param
    if (is_unix) {
        if(m.trace){
            arg_x="-x"
        }else{
            arg_x=""
        }
    } else {
        if(m.trace){
            arg_x=""
        }else{
            arg_x="@echo off"
        }
    }
    
    // Replace Var to CMD
    try{
        def pattern_in = /\$\{(\w+?)\}/
        def matcher_in = (m.cmd  =~ pattern_in)
        def list_matcher_in = []
        if (matcher_in.find()){
            for(sub_in in matcher_in){
                if(run_vars.get(sub_in[1], 'NoFind') != 'NoFind'){
                    list_matcher_in.add(sub_in[1])
                }
            }
            for(i in list_matcher_in.toSet()){
                m.cmd = m.cmd.replace('${'+i+'}', run_vars.get(i))
            }
        }
    }catch(NotSerializableException ex){}

    // Run
    if (m.returnStdout){
        // get std
        if (is_unix) {
            return sh(returnStdout: true, script: "#!/bin/sh ${arg_x}\n${m.cmd}")
        } else {
            return bat(returnStdout: true, script: "${arg_x}\n${m.cmd}")
        }
    }else if (m.returnStatus){
        // get status
        if (is_unix) {
            return sh(returnStatus: true, script: "#!/bin/sh ${arg_x}\n${m.cmd}")
        } else {
            return bat(returnStatus: true, script: "${arg_x}\n${m.cmd}")
        }
    }else if (m.sops_var){
        // get sops
        def out = ""
        if (is_unix) {
            out = sh(returnStdout: true ,script: "#!/bin/sh ${arg_x}\n${m.cmd}")
        } else {
            out = bat(returnStdout: true ,script: "${arg_x}\n${m.cmd}")
        }
        println(out)
        // save var to global from mark of SOPS_VAR
        try{
            def pattern_out = /<SOPS_VAR>(\w+?):(.+?)<\/SOPS_VAR>/
            def matcher_out = (out  =~ pattern_out)
            if (matcher_out.find()){
                for(sub_out in matcher_out){
                    run_vars[sub_out[1]] = sub_out[2]
                }
            }
        }catch(NotSerializableException ex){}
    }else{
        if (is_unix) {
            sh(script: "#!/bin/sh ${arg_x}\n${m.cmd}")
        } else {
            bat(script: "${arg_x}\n${m.cmd}")
        }
    }
}

pipeline{
    agent any
    options{
        skipDefaultCheckout()
    }
    stages {
        stage('SAVE') {
            steps {
                script {
                    // 保存VAR变量值为AAAAA
                    run(cmd: 'echo "<SOPS_VAR>VAR:AAAAA</SOPS_VAR>"', sops_var: true)
                }
            }
        }
        stage('PRINT') {
            steps {
                script {
                    // 执行之前会替换VAR的值
                    run(cmd: 'echo ${VAR}',trace: true)
                }
            }
        }
    }
}
相关推荐
曾经的三心草2 小时前
最新版本组件的docker下载-Seata
运维·docker·容器
梁正雄4 小时前
15、Docker swarm-2-安装与存储
运维·docker·容器
fyakm4 小时前
Linux文件搜索:grep、find命令实战应用(附案例)
linux·运维·服务器
wanhengidc6 小时前
云手机存在的意义是什么
运维·服务器·arm开发·安全·智能手机
snow@li7 小时前
运维:部署Jenkins
运维·jenkins
大海绵啤酒肚8 小时前
OpenStack虚拟化平台之T版搭建部署
linux·运维·云计算·openstack
The Chosen One98510 小时前
【Linux】Linux权限讲解 :写给文件的一封情书
linux·运维·服务器
Thexhy12 小时前
在centos 7上配置FIP服务器的详细教程!!!
linux·运维·centos
FJW02081412 小时前
DevOps——CI/CD持续集成与持续交付/部署的理解与部署
运维·ci/cd·devops
Java 码农13 小时前
Linux shell sed 命令基础
linux·运维·服务器