【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)
                }
            }
        }
    }
}
相关推荐
物联网软硬件开发-轨物科技4 小时前
【轨物方案】聚焦锯床设备智能化升级,打造工业互联网新范式
运维·科技·物联网
suzhou_speeder4 小时前
企业数字化网络稳定运行与智能化管理解决方案
运维·服务器·网络·交换机·poe·poe交换机
weixin_307779135 小时前
Jenkins Pipeline 完全指南:核心概念、使用详解与最佳实践
开发语言·ci/cd·自动化·jenkins·etl
RisunJan5 小时前
Linux命令-grpck命令(验证和修复组配置文件(`/etc/group` 和 `/etc/gshadow`)完整性的工具)
linux·运维·服务器
Evan芙6 小时前
nginx日志管理及日志格式定制
运维·nginx
The star"'6 小时前
04-管理变量和事实
运维·云计算·ansible
林疏safe7 小时前
灯塔部署云服务器docker 部署方式,以及忘记密码如何查找
运维·服务器·docker
云和数据.ChenGuang7 小时前
openEuler系统下安装MongoDB的技术教程
运维·数据库·mongodb·压力测试·运维工程师·运维技术
广东大榕树信息科技有限公司7 小时前
如何实现动环监控系统的国产化与智能化?
运维·网络·物联网·国产动环监控系统·动环监控系统
tzhou644527 小时前
自动化运维利器Ansible
运维·自动化·ansible