【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)
                }
            }
        }
    }
}
相关推荐
杨了个杨898223 分钟前
nginx常见功能部署
运维·服务器·nginx
小天源2 小时前
linux漏洞一键扫描
linux·运维·服务器·漏洞扫描
eWidget2 小时前
InfluxDB迁移至金仓数据库的同城容灾实践:性能显著提升、运维效率优化,某能源企业实现RPO_5秒的高可靠时序数据管理
运维·数据库·能源·时序数据库·kingbase·kingbasees·金仓数据库
m0_696212682 小时前
个人微信api
运维·服务器
en-route3 小时前
SSH Key 与 GPG Key 区别详解:Git 使用中的身份与签名机制
运维·git·ssh
小白鸽i5 小时前
【LINUX】将源码驱动文件编译并生效
linux·运维·服务器
做萤石二次开发的哈哈6 小时前
萤石开放平台 音视频 | 产品概述
运维·服务器·萤石云·萤石·萤石开放平台
0思必得06 小时前
[Web自动化] 处理爬虫异常
运维·爬虫·python·selenium·自动化·web自动化
China_Yanhy7 小时前
我的区块链运维日记 · 第 4 日:死掉的“活”节点 —— 攻克“同步滞后(Lag)”
运维·区块链
kida_yuan7 小时前
【Linux】说说我对 Wine 与 deepin-wine 的理解
linux·运维·wine