
def NAMESPACE = "Dev"
def BODY= sh(
script:'''body=$(cat <<-EOF
{
"name": "${NAMESPACE}",
"type": "regularwebapp"
}
EOF
)
(echo $body)''',
returnStdout: true
).trim()
Das Obige funktioniert nicht, die Ausgabe ist wie folgt:
{
"name": "",
"type": "regularwebapp"
}
Antwort1
Groovy führt keine Variablensubstitution innerhalb '
von Zeichenfolgen in einfachen Anführungszeichen () durch. Verwenden Sie stattdessen Zeichenfolgen in doppelten Anführungszeichen ( "
) - dies erfordert auch das Escapen von Nicht-Groovy-Variablen:
def NAMESPACE = "Dev"
def BODY= sh(
script:"""body=\$(cat <<-EOF
{
"name": "${NAMESPACE}",
"type": "regularwebapp"
}
EOF
)
(echo \$body)""",
returnStdout: true
).trim()