Multiline Bash Command in Jenkins Pipeline

Jenkins pipeline multiline script command as variable

you can do it like this. Not with a groovy variable but can be more dynamic with groovy function/method

def reusableScript(message) {
sh """
echo Hello World
echo Hi ${message}
"""
}
pipeline {
agent any;
stages {
stage('01') {
steps {
script {
reusableScript("From ${env.STAGE_NAME}")
}
}
}
stage('02') {
steps {
script {
reusableScript("From ${env.STAGE_NAME}")
}
}
}
}
}

Multiline bash command in Jenkins pipeline

because <<DATA specifies the end of here-doc <<-DATA suppress leading tabs but not spaces

cat <<-DATA
hello
<tab>DATA

another option is to add spaces in marker

cat << "    DATA"
hello
DATA

How to use bash variables in Jenkins multi-line shell script

Try this:

sh """#!/bin/bash
set -x

for file in *.map; do
filename="\$(basename "\$file" .map)"
echo "Uploading \$filename"

curl -X POST "${SERVER_URL}/assets/v1/sourcemaps" \
-F service_name="${SERVICE_NAME}" \
-F service_version="${revision}" \
-F bundle_filepath="${main_url}\$filename" \
-F sourcemap="@\${filename}.map" &
done
wait
"""

How can I read Jenkins pipeline variable in multiline shell?

You need to use """ like this:

sh """
"ssh -o StrictHostKeyChecking=no ${myserver} 'rm -rf temp && mkdir -p temp && mkdir -p real'"
"""

How to set variables in a multi-line shell script within Jenkins Groovy?

You need to change to triple single quotes ''' or escape the dollar \$

Then you'll skip the groovy templating which is what's giving you this issue



Related Topics



Leave a reply



Submit