Jenkins: 在未设置时绕过Shell变量验证

huangapple go评论51阅读模式
英文:

Jenkins: Bypass Shell Variable validation when not set

问题

以下是您要翻译的部分:

I have conditionally set Jenkins parameter to read USER_INPUT_VALUE based on upstream ${SHOW_OPTION} condition YES or NO. 

if ("$SHOW_OPTION" == ("NO")) {
     properties([ //No USER INPUT passed
       ])
else
{
     properties([
       parameters([ string(name: 'USER_INPUT_VALUE', defaultValue: 'mydefault']")
     ])
}

Later Jenikinsfile calls a shell script as ONE Liner which assigns a shell variable like below, When SHOW_OPTION is NO, I am assigning the hardcoded value, else actual USER value,

if [ ${SHOW_OPTION} == "NO" ]; then TARGET='"NO_USER_VALUE"'; else TARGET=$USER_INPUT_VALUE; fi;
But when SHOW_OPTION is NO, build complains as `groovy.lang.MissingPropertyException: No such property: USER_INPUT_VALUE for class: groovy.lang.Binding`

Why script is validating else part here as it is not applicable. Is there any way to skip this validation in shell script.

Note: When I run one liner on regular shell I dont see any issue (verified with echo $?)
英文:

I have conditionally set Jenkins parameter to read USER_INPUT_VALUE based on upstream ${SHOW_OPTION} condition YES or NO.

if ("$SHOW_OPTION" == ("NO")) {
     properties([ //No USER INPUT passed
       ])
else
{
     properties([
       parameters([ string(name: 'USER_INPUT_VALUE', defaultValue: 'mydefault']")
     ])
}

Later Jenikinsfile calls a shell script as ONE Liner which assigns a shell variable like below, When SHOW_OPTION is NO, I am assigning the hardcoded value, else actual USER value,

if [ ${SHOW_OPTION} == "NO" ]; then TARGET='"NO_USER_VALUE"'; else TARGET=$USER_INPUT_VALUE; fi;

But when SHOW_OPTION is NO, build complains as groovy.lang.MissingPropertyException: No such property: USER_INPUT_VALUE for class: groovy.lang.Binding

Why script is validating else part here as it is not applicable. Is there any way to skip this validation in shell script.

Note: When I run one liner on regular shell I dont see any issue (verified with echo $?)

答案1

得分: 1

声明性流水线

  • 不要忘记使用 #!/bin/bash 开始 Bash 脚本。
  • 构建参数不会传递给 Shell 脚本。
  • 只有 Jenkins 环境变量会传递给 Shell 脚本。
def scriptContent = """#!/bin/bash

echo "脚本参数"
echo "$@" 

#if [ $SHOW_OPTION == "NO" ]; then TARGET=\""NO_USER_VALUE"\"; else TARGET=$USER_INPUT_VALUE; fi;

echo "Shell 结果:"
echo TARGET="$TARGET"
"""

pipeline {
    agent any
    
    environment {
        SHOW_OPTION="NO"
    }    
    
    stages {
        stage('设置参数') {
            steps {
                
                // 模拟您的 Shell 脚本
                writeFile(file: "script.sh", text: scriptContent) 
                
                script { 
                    if ("$SHOW_OPTION" == ("NO")) {
                         println "没有配置参数"
                    }else{
                         properties([
                           parameters([
                                string(defaultValue: 'mydefault', name: 'USER_INPUT_VALUE')
                            ])  
                         ])
                    }
                }
                
                println "构建参数"
                println params
                // 模拟您的 Shell 脚本
                sh "chmod +x script.sh"
                sh "./script.sh"
            }
        }
    }   
}

当 SHOW_OPTION 为 "YES" 时的结果:

Jenkins: 在未设置时绕过Shell变量验证

当 SHOW_OPTION 为 "NO" 时的结果:

Jenkins: 在未设置时绕过Shell变量验证

> 注意:参数应该手动删除,因为当 SHOW_OPTION 为 "YES" 时,参数会被添加到流水线中。因此,我建议不在流水线中配置动态参数。您可以以标准方式添加参数并比较其值是否为 null。

脚本流水线

如果有人使用脚本流水线,这将按预期工作:

def SHOW_OPTION= "true";

node {

    if ("$SHOW_OPTION" == "NO") {
     println "没有参数"
    }else{
        properties(
            [
                parameters([
                        string(defaultValue: 'mydefault', name: 'USER_INPUT_VALUE')
                ])   
            ]
        )      
    }

    println params.USER_INPUT_VALUE
    
}
英文:

declarative pipeline

  • Don't forget to start the bash script with #!/bin/bash
  • Build parameters are not sent to shell script.
  • Only jenkins env vars are sent to the shell script.
def scriptContent = """#!/bin/bash

echo "script arguments"
echo "$@" 

#echo "env variables passed to script"
#env 

if [ $SHOW_OPTION == "NO" ]; then TARGET=\'"NO_USER_VALUE"\'; else TARGET=$USER_INPUT_VALUE; fi;

echo "shell result:"
echo TARGET="$TARGET"
""";

pipeline {
    agent any
    
    environment {
        SHOW_OPTION="NO"
    }    
    
    stages {
        stage('Setup parameters') {
            steps {
                
                //simulating your shell script
                writeFile(file: "script.sh" , text: scriptContent) 
                
                script { 
                    if ("$SHOW_OPTION" == ("NO")) {
                         println "no parameters are configured"
                    }else{
                         properties([
                           parameters([
                                string(defaultValue: 'mydefault', name: 'USER_INPUT_VALUE')
                            ])  
                         ])
                    }
                }
                
                println "build params"
                println params
                //simulating your shell script
                sh "chmod +x script.sh"
                sh "./script.sh"
            }
        }
    }   
}

Result when SHOW_OPTION is "YES":

Jenkins: 在未设置时绕过Shell变量验证

Result when SHOW_OPTION is "NO":

Jenkins: 在未设置时绕过Shell变量验证

> Note: The parameter should be deleted manually because when SHOW_OPTION=YES , the parameters was added to the pipeline. Because of this, I advice to not configure dynamic parameters in the pipeline. You could add the parameter in a standard way and compare if its value is null

scripted pipeline

In case someone is using a scripted pipeline, this work as expected:

def SHOW_OPTION= "true";

node {

    if ("$SHOW_OPTION" == "NO") {
     println "no parameters"
    }else{
        properties(
            [
                parameters([
                        string(defaultValue: 'mydefault', name: 'USER_INPUT_VALUE')
                ])   
            ]
        )      
    }

    println params.USER_INPUT_VALUE
    
}

huangapple
  • 本文由 发表于 2023年3月12日 08:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定