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
How to Use Sed to Extract Substring
How Do So_Reuseaddr and So_Reuseport Differ
Can't Call C Standard Library Function on 64-Bit Linux from Assembly (Yasm) Code
How to Install Gcc 4.9.2 on Rhel 7.4
Get Destination Address of a Received Udp Packet
Split One File into Multiple Files Based on Delimiter
How to Create Docker Overlay Network Between Multi Hosts
Curl to Access a Page That Requires a Login from a Different Page
Bluez: How to Set Up a Gatt Server from the Command Line
How to Print a Character in Linux X86 Nasm
How to Shutdown a Spring Boot Application in a Correct Way
Assembling 32-Bit Binaries on a 64-Bit System (Gnu Toolchain)
How to Change Permissions For a Folder and Its Subfolders/Files
How to Recursively Find All Files in Current and Subfolders Based on Wildcard Matching
Bash While Read Loop Extremely Slow Compared to Cat, Why
Why Does a Jvm Report More Committed Memory Than the Linux Process Resident Set Size