【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 小时前
CentOS 安装jenkins笔记
笔记·centos·jenkins
saynaihe4 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
小蜜蜂爱编程4 小时前
ubuntu透网方案
运维·服务器·ubuntu
AI视觉网奇5 小时前
git 访问 github
运维·开发语言·docker
G_whang5 小时前
jenkins使用Jenkinsfile部署springboot+docker项目
spring boot·docker·jenkins
头发那是一根不剩了5 小时前
nginx:SSL_CTX_use_PrivateKey failed
运维·服务器
七夜zippoe6 小时前
破解 VMware 迁移难题:跨平台迁移常见问题及自动化解决方案
运维·自动化·vmware
hweiyu007 小时前
docker简介
运维·docker·容器
Sally璐璐7 小时前
OpenVPN:深度解析开源 VPN 解决方案
运维·开源