<p>When you have a variable inside of a string that is called a gstring or groovy string.</p>
<p>In Groovy, which is the language used inside of the script block, you have to use a certain style of quotes if you want to render the variables into their values. Also called string interpolation and the documentation for groovy's gstrings is <a href="https://groovy-lang.org/syntax.html#_string_interpolation" rel="nofollow noreferrer">here</a>.</p>
<p>What you need to do is change your <code>'''</code> blocks to <code>&quot;&quot;&quot;</code>. Because string interpolation only works with <code>&quot;</code> quotes and not <code>'</code> quotes.</p>
<p>Here is a full example of how I would probably write the code:</p>
<pre><code>script {

    // This single forward slash makes the first line of the string begin with export and not be empty
    String bashVars = &quot;&quot;&quot;\
        export GIT_USERNAME=${GIT_USERNAME};
        export GIT_PASSWORD=${GIT_PASSWORD};
        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
        export CHECKOUT_POINT=${CHECKOUT_POINT};
        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
    &quot;&quot;&quot;

    withCredentials([
        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
    ]) {

        // Groovy ternary operator aka one line if statement
        String scriptPath = env.APP_ENV == 'testing' ? './scripts/awesome_script_2.sh' : './scripts/awesome_script.sh'

        String script = &quot;&quot;&quot;\
            #!/bin/bash
            ${bashVars}
            ${scriptPath}
        &quot;&quot;&quot;

        sh(script)
    }
}
</code></pre>
<p>Notice I used variable typing? It will give you better error messages if something does go wrong in your pipeline.</p>
<p>As you advance and become better with Jenkins you might want to switch to scripted pipelines. They are a lot more powerful than declarative.</p>
<p>EDIT: While my answer is great for anyone wanting to know the specifics about string interpolation in groovy, the answer by <a href="https://devops.stackexchange.com/a/16330/4427#4427">@a25bedc5-3d09-41b8-82fb-ea6c353d75ae</a> is really good for someone wanting to have a scripted pipeline with conditional stages based on something like a variable.</p>

CC BY-SA 4.0
2022-07-21T01:39:47.360Z
2022-07-23T16:03:28.683Z
2022-07-23T16:03:28.683Z
Levi
<p>The <code>withCredentials</code> would be setting the environment variables for you so you wouldn't need to use export as they will already be passed into the shell.</p>
<p>So you can pass in the remaining variables as environment variables and then use <code>when</code> to control the flow.</p>
<pre class="lang-java prettyprint-override"><code>pipeline {
    agent any
    stages {
        
        stage('Stage Testing') {
            when { 
                environment name: 'APP_ENV', value: 'testing' 
            }
            environment {
                PROYECT_DIRECTORY = '&lt;replace this with testing value&gt;'
                CHECKOUT_POINT    = '&lt;replace this with testing value&gt;'
                GIT_HTTPS_REPO_DEPLOY    = '&lt;replace this with testing value&gt;'
                MARIADB_HOSTNAME    = '&lt;replace this with testing value&gt;'
            }
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        ./scripts/awesome_script_2.sh
                        '''
                    }
                }
            }
        }
        stage('Stage Production') {
            when { 
                environment name: 'APP_ENV', value: 'production' 
            }
            environment {
                PROYECT_DIRECTORY = '&lt;replace this with production value&gt;'
                CHECKOUT_POINT    = '&lt;replace this with production value&gt;'
                GIT_HTTPS_REPO_DEPLOY    = '&lt;replace this with production value&gt;'
                MARIADB_HOSTNAME    = '&lt;replace this with production value&gt;'
            }
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        ./scripts/awesome_script.sh
                        '''
                    }
                }
            }
        }
    }
}
</code></pre>
<p>You will also want to avoid string interpolation when dealing with credentials as explained <a href="https://docs.cloudbees.com/docs/admin-resources/latest/automating-with-jenkinsfile/string-interpolation#_interpolation_of_sensitive_environment_variables" rel="nofollow noreferrer">here</a> so it is best to use the <code>''</code> quotes when handling credentials.</p>

CC BY-SA 4.0
2022-07-22T18:32:24.660Z
2022-07-22T18:32:24.660Z
a25bedc5-3d09-41b8-82fb-ea6c353d75ae
<p>I have a block of code that work fine at the moment. But reuse that code is a little ugly.</p>
<pre><code>pipeline {
    agent any
    stages {
        stage('Stage 1') {
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        export GIT_USERNAME=${GIT_USERNAME};
                        export GIT_PASSWORD=${GIT_PASSWORD};
                        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                        export CHECKOUT_POINT=${CHECKOUT_POINT};
                        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};

                        ./scripts/awesome_script.sh
                        '''
                    }
                }
            }
        }
    }
}
</code></pre>
<p>The problem is when I apply if statements to use diferents scripts bash inside sh() block. The script <code>./scripts/awesome _script.sh</code> need that variables exported. Therefore the code looks like this.</p>
<pre><code>script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        export GIT_USERNAME=${GIT_USERNAME};
                        export GIT_PASSWORD=${GIT_PASSWORD};
                        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                        export CHECKOUT_POINT=${CHECKOUT_POINT};
                        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};

                        ./scripts/awesome_script.sh
                        '''
                        if(env.APP_ENV == 'testing'){
                            sh '''#!/bin/bash
                            export GIT_USERNAME=${GIT_USERNAME};
                            export GIT_PASSWORD=${GIT_PASSWORD};
                            export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                            export CHECKOUT_POINT=${CHECKOUT_POINT};
                            export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                            export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};

                            ./scripts/awesome_script_2.sh
                            '''
                        }
                    }
                }
</code></pre>
<p><strong>Very ugly, right ? :/ :(</strong></p>
<p>What I am looking is something similar like to this, but with correct syntax:</p>
<pre><code>script {
                    env.STRING_BLOCK_WITHOUT_PROCESSING = '''
                        export GIT_USERNAME=${GIT_USERNAME};
                        export GIT_PASSWORD=${GIT_PASSWORD};
                        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                        export CHECKOUT_POINT=${CHECKOUT_POINT};
                        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
                    ''';
                    
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        ${STRING_BLOCK_WITHOUT_PROCESSING}

                        ./scripts/awesome_script.sh
                        '''
                        if(env.APP_ENV == 'testing'){
                            sh '''#!/bin/bash
                            ${STRING_BLOCK_WITHOUT_PROCESSING}
                            
                            ./scripts/awesome_script_2.sh
                            '''
                        }
                    }
                }
</code></pre>
<p>Thanks for all help you can give me. &lt;3</p>

<jenkins><jenkins-pipeline><bash><groovy>
Reuse block string without processing it on the fly
navebinario
# Reuse block string without processing it on the fly

(Asked by: username_0 on 2022-07-19)

I have a block of code that work fine at the moment. But reuse that code is a little ugly.
pipeline {
    agent any
    stages {
        stage('Stage 1') {
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        export GIT_USERNAME=${GIT_USERNAME};
                        export GIT_PASSWORD=${GIT_PASSWORD};
                        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                        export CHECKOUT_POINT=${CHECKOUT_POINT};
                        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};

                        ./scripts/awesome_script.sh
                        '''
                    }
                }
            }
        }
    }
}

The problem is when I apply if statements to use diferents scripts bash inside sh() block. The script ./scripts/awesome _script.sh need that variables exported. Therefore the code looks like this.
script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        export GIT_USERNAME=${GIT_USERNAME};
                        export GIT_PASSWORD=${GIT_PASSWORD};
                        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                        export CHECKOUT_POINT=${CHECKOUT_POINT};
                        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};

                        ./scripts/awesome_script.sh
                        '''
                        if(env.APP_ENV == 'testing'){
                            sh '''#!/bin/bash
                            export GIT_USERNAME=${GIT_USERNAME};
                            export GIT_PASSWORD=${GIT_PASSWORD};
                            export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                            export CHECKOUT_POINT=${CHECKOUT_POINT};
                            export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                            export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};

                            ./scripts/awesome_script_2.sh
                            '''
                        }
                    }
                }

Very ugly, right ? :/ :(
What I am looking is something similar like to this, but with correct syntax:
script {
                    env.STRING_BLOCK_WITHOUT_PROCESSING = '''
                        export GIT_USERNAME=${GIT_USERNAME};
                        export GIT_PASSWORD=${GIT_PASSWORD};
                        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                        export CHECKOUT_POINT=${CHECKOUT_POINT};
                        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
                    ''';
                    
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        ${STRING_BLOCK_WITHOUT_PROCESSING}

                        ./scripts/awesome_script.sh
                        '''
                        if(env.APP_ENV == 'testing'){
                            sh '''#!/bin/bash
                            ${STRING_BLOCK_WITHOUT_PROCESSING}
                            
                            ./scripts/awesome_script_2.sh
                            '''
                        }
                    }
                }

Thanks for all help you can give me. <3

**Tags:** jenkins, jenkins-pipeline, bash, groovy


---

## 2 answers

### Answer by username_1 on 2022-07-21. Score: 1 | Accepted answer

When you have a variable inside of a string that is called a gstring or groovy string.
In Groovy, which is the language used inside of the script block, you have to use a certain style of quotes if you want to render the variables into their values. Also called string interpolation and the documentation for groovy's gstrings is here.
What you need to do is change your ''' blocks to """. Because string interpolation only works with " quotes and not ' quotes.
Here is a full example of how I would probably write the code:
script {

    // This single forward slash makes the first line of the string begin with export and not be empty
    String bashVars = """\
        export GIT_USERNAME=${GIT_USERNAME};
        export GIT_PASSWORD=${GIT_PASSWORD};
        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
        export CHECKOUT_POINT=${CHECKOUT_POINT};
        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
    """

    withCredentials([
        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
    ]) {

        // Groovy ternary operator aka one line if statement
        String scriptPath = env.APP_ENV == 'testing' ? './scripts/awesome_script_2.sh' : './scripts/awesome_script.sh'

        String script = """\
            #!/bin/bash
            ${bashVars}
            ${scriptPath}
        """

        sh(script)
    }
}

Notice I used variable typing? It will give you better error messages if something does go wrong in your pipeline.
As you advance and become better with Jenkins you might want to switch to scripted pipelines. They are a lot more powerful than declarative.
EDIT: While my answer is great for anyone wanting to know the specifics about string interpolation in groovy, the answer by @username_2 is really good for someone wanting to have a scripted pipeline with conditional stages based on something like a variable.


---

### Answer by username_2 on 2022-07-22. Score: 1

The withCredentials would be setting the environment variables for you so you wouldn't need to use export as they will already be passed into the shell.
So you can pass in the remaining variables as environment variables and then use when to control the flow.
pipeline {
    agent any
    stages {
        
        stage('Stage Testing') {
            when { 
                environment name: 'APP_ENV', value: 'testing' 
            }
            environment {
                PROYECT_DIRECTORY = ''
                CHECKOUT_POINT    = ''
                GIT_HTTPS_REPO_DEPLOY    = ''
                MARIADB_HOSTNAME    = ''
            }
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        ./scripts/awesome_script_2.sh
                        '''
                    }
                }
            }
        }
        stage('Stage Production') {
            when { 
                environment name: 'APP_ENV', value: 'production' 
            }
            environment {
                PROYECT_DIRECTORY = ''
                CHECKOUT_POINT    = ''
                GIT_HTTPS_REPO_DEPLOY    = ''
                MARIADB_HOSTNAME    = ''
            }
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''#!/bin/bash
                        ./scripts/awesome_script.sh
                        '''
                    }
                }
            }
        }
    }
}

You will also want to avoid string interpolation when dealing with credentials as explained here so it is best to use the '' quotes when handling credentials.


---

