jenkinsjenkins-pipeline

Jenkins pipeline read parameters with spaces in script block


I am trying to read Jenkins parameters with spaces in script block, running to bad substitution error. Can you please help?

These two parameters are from the Embedded HTML.

Premium Account Type is choice parameter

Hierarchical Namespace is a checkbox parameter

here is what I have tried

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\"}"

Error that I am seeing. ...

/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

When I tried with without quotes, I get this error

/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"}: 

Another trial

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']}"

Error for the above trial

/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

Solution

  • The handling of the spaces and quotes in the parameter names seems fishy.
    Try, for testing, the following syntax in your Jenkins pipeline:

    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']}"
    

    That would use single quotes to define the key in params map, which should allow for keys with spaces.
    And it removes unnecessary backslashes and complex quotation marks.

    See if it does read the parameters and resolve the bad substitution errors.


    Another trial:

    The syntax errors you are encountering suggest that the shell script is not interpreting the Groovy syntax correctly. The parameters need to be accessed within the context of a Jenkins Pipeline Groovy script, not directly in a shell script.

    Try instead to assign the Jenkins parameters to Groovy variables within the pipeline script.
    Then, pass these Groovy variables to the shell script.

    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}"
                        """
                    }
                }
            }
        }
    }
    

    That way, the Groovy script portion of the Jenkins pipeline (script { }) is used to extract the parameters and assign them to Groovy variables. These variables are then passed into the shell script within the sh """ """ block.

    That should resolve the bad substitution errors by making sure the correct handling of parameters within the Jenkins pipeline.