Jenkins管道读取脚本块中带有空格的参数

flseospp  于 11个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(160)

我试图读取脚本块中带有空格的Jenkins参数,运行到错误替换。你能帮忙吗?
这两个参数来自嵌入式HTML。
高级帐户类型是选择参数
层次空间是复选框参数
这就是我所尝试的

echo    premiumAccountType=\\"${params.\\"Premium Account Type\\"}\\"    >> $WORKSPACE/azr-storage-account/account.auto.tfvars
                    echo    is_hns_enabled=\\"${params.\\"Hierarchial Namespace\\"}\\"    >> $WORKSPACE/account.auto.tfvars
                    echo "{params.\"Premium Account Type\"} --- ${params.\"Hierarchial Namespace\"}"

字符串
我看到的错误……

/var/jenkins_home/workspace/test@tmp/durable-5c75f1d0/script.sh: line 37: premiumAccountType=\"${params.\"Premium Account Type\"}\": bad substitution
/var/jenkins_home/workspace/test@tmp/durable-5c75f1d0/script.sh: line 38: is_hns_enabled=\"${params.\"Hierarchial Namespace\"}\": bad substitution
/var/jenkins_home/workspace/test@tmp/durable-5c75f1d0/script.sh: line 39: } --- ${params."Hierarchial Namespace"}: bad substitution


当我尝试不带引号时,我得到了这个错误

/var/jenkins_home/workspace/test@tmp/durable-75ec08a0/script.sh: line 37: premiumAccountType=\"${params.Premium Account Type}\": bad substitution
/var/jenkins_home/workspace/test@tmp/durable-75ec08a0/script.sh: line 38: is_hns_enabled=\"${params."Hierarchial Namespace"}\": bad substitution
/var/jenkins_home/workspace/test@tmp/durable-75ec08a0/script.sh: line 39: TESTING ---- {params.Premium Account Type} --- ${params."Hierarchial Namespace"}:


另一项试验

echo "premiumAccountType=${params['Premium Account Type']}" >> $WORKSPACE/azr-storage-account/account.auto.tfvars
echo "is_hns_enabled=${params['Hierarchial Namespace']}" >> $WORKSPACE/account.auto.tfvars
echo "${params['Premium Account Type']} --- ${params['Hierarchial Namespace']}"


上述试验的错误

/var/jenkins_home/workspace/test@tmp/durable-6dd893b6/script.sh: line 38: is_hns_enabled=\"${params.\"Hierarchial\ Namespace\"}\": bad substitution
/var/jenkins_home/workspace/test@tmp/durable-6dd893b6/script.sh: line 39: Premium Account Type: syntax error in expression (error token is "Account Type")
/var/jenkins_home/workspace/test@tmp/durable-6dd893b6/script.sh: line 40: Hierarchial Namespace: syntax error in expression (error token is "Namespace")
/var/jenkins_home/workspace/test@tmp/durable-6dd893b6/script.sh: line 41: Premium Account Type: syntax error in expression (error token is "Account Type")
/var/jenkins_home/workspace/test@tmp/durable-6dd893b6/script.sh: line 42: ---> $premiumAccountType ---> ${env.premiumAccountType}: bad substitution

insrf1ej

insrf1ej1#

对参数名中空格和引号的处理似乎有些可疑。
为了测试,在Jenkins管道中尝试以下语法:

echo "premiumAccountType=${params['Premium Account Type']}" >> $WORKSPACE/azr-storage-account/account.auto.tfvars
echo "is_hns_enabled=${params['Hierarchial Namespace']}" >> $WORKSPACE/account.auto.tfvars
echo "${params['Premium Account Type']} --- ${params['Hierarchial Namespace']}"

字符串
这将使用单引号来定义paramsMap中的键,这应该允许键带有空格。
它删除了不必要的反斜杠和复杂的引号。
看看它是否读取了参数并解决了坏的替换错误。
另一项试验:
您遇到的语法错误表明shell脚本没有正确解释Groovy语法。需要在Jenkins Pipeline Groovy脚本的上下文中访问参数,而不是直接在shell脚本中访问。
尝试将Jenkins参数分配给管道脚本中的Groovy变量。
然后,将这些Groovy变量传递给shell脚本。

pipeline {
    // Define your pipeline stages
    stages {
        stage('Example') {
            steps {
                script {
                    // Assign parameters to Groovy variables
                    def premiumAccountType = params['Premium Account Type']
                    def hierarchicalNamespace = params['Hierarchial Namespace']

                    // Use the Groovy variables in the shell script
                    sh """
                        echo "premiumAccountType=${premiumAccountType}" >> $WORKSPACE/azr-storage-account/account.auto.tfvars
                        echo "is_hns_enabled=${hierarchicalNamespace}" >> $WORKSPACE/account.auto.tfvars
                        echo "${premiumAccountType} --- ${hierarchicalNamespace}"
                    """
                }
            }
        }
    }
}


这样,Jenkins管道的Groovy脚本部分(script { })用于提取参数并将其分配给Groovy变量。然后将这些变量传递到sh """ """块中的shell脚本中。
这应该通过确保Jenkins管道内参数的正确处理来解决糟糕的替换错误。

相关问题