Score:0

Not able to access groovy variable inside shell script in JenkinsFile

cn flag
def NAMESPACE = "Dev"

def BODY= sh(
script:'''body=$(cat <<-EOF
{
    "name": "${NAMESPACE}",
    "type": "regularwebapp"
}
EOF
)
(echo $body)''',
returnStdout: true
).trim()

The above doesnt work, output is as follows:

{
    "name": "",
    "type": "regularwebapp"
}
Score:0
jp flag

Groovy doesn't perform variable substitution inside single-quoted (') strings. Use double-quoted (") strings instead - this will also require escaping non-Groovy variables:

def NAMESPACE = "Dev"

def BODY= sh(
script:"""body=\$(cat <<-EOF
{
    "name": "${NAMESPACE}",
    "type": "regularwebapp"
}
EOF
)
(echo \$body)""",
returnStdout: true
).trim()
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.