【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)
                }
            }
        }
    }
}
相关推荐
qq_3129201119 分钟前
Nginx限流与防爬虫与安全配置方案
运维·爬虫·nginx·安全
GanGuaGua25 分钟前
Linux系统:线程的互斥和安全
linux·运维·服务器·c语言·c++·安全
全糖去冰吃不了苦35 分钟前
ELK 集群部署实战
运维·jenkins
Lynnxiaowen1 小时前
今天继续昨天的正则表达式进行学习
linux·运维·学习·正则表达式·云计算·bash
-KamMinG1 小时前
“从零到一:使用GitLab和Jenkins实现自动化CI/CD流水线”
自动化·gitlab·jenkins
努力学习的小廉1 小时前
深入了解linux系统—— POSIX信号量
linux·运维·服务器
IT成长日记2 小时前
【Linux基础】Linux系统配置IP详解:从入门到精通
linux·运维·tcp/ip·ip地址配置
夜无霄2 小时前
安卓逆向(一)Ubuntu环境配置
linux·运维·爬虫·ubuntu
田野里的雨2 小时前
manticore离线安装(Ubuntu )
linux·运维·服务器·全文检索
wanhengidc2 小时前
云手机就是虚拟机吗?
运维·网络·安全·智能手机