Windows环境Jenkins Pipeline变量设置与打印问题求助
Fixing Variable Interpolation & Execution Issues in Windows Jenkins Pipeline
Let's break down what's going wrong with your Pipeline code and fix it step by step—you're mixing up Groovy variable syntax with Windows batch variable syntax, which is why your variables aren't resolving correctly.
Key Problems in Your Original Code
- You're trying to reference Groovy variables (like
commandPRM) using Windows batch syntax (%commandPRM%), but those are two separate environments. Groovy variables don't exist in the batch shell unless you explicitly pass them. - You're using single quotes for
batcommands when you need variable interpolation—Groovy only expands variables inside double-quoted strings. - Windows uses
%errorlevel%to get command exit codes, not$?(that's for Unix-like shells). - When defining
commandPRM, you're using%rp%instead of directly referencing the Jenkins job parameterrp(Groovy can access job parameters directly by name).
Corrected Pipeline Code
script { // Print the Jenkins job parameter directly (Groovy can access it by name) bat "echo ${rp}" // Define Groovy variable using the job parameter def commandPRM = rp bat 'echo variable 1' script { bat 'echo variable 2' // Use double quotes to interpolate Groovy variable into batch command bat "echo ${commandPRM}" } // Build the dir command with Groovy string concatenation commandPRM = "dir ${commandPRM} /s /q" script { bat 'echo variable 3' // Interpolate the updated Groovy variable bat "echo ${commandPRM}" bat 'echo variable 4' bat "echo ${commandPRM}" } // Execute the command and capture exit code (Windows uses %errorlevel%) bat """ ${commandPRM} echo %errorlevel% > status """ println 'Performing print action...1' def r = readFile('status').trim() println r println 'Performing print action...2' // Compare as integer (exit code is a string, so convert it) if (r.toInteger() == 0) { println 'Performing some action...3' // Use double quotes to interpolate the job parameter for rmdir bat "rmdir ${rp} /s /q" } }
What Changed & Why
- Directly reference Jenkins parameters: Instead of
%rp%, userp(Groovy has access to job parameters as variables in the Pipeline context). - Groovy variable interpolation with double quotes: For any
batcommand that needs to use a Groovy variable, wrap the command in double quotes ("...") and use${variableName}to interpolate it. Single quotes ('...') treat the string as literal, so variables won't be expanded. - Windows exit code handling: Replaced
$?with%errorlevel%to correctly capture the exit code of thedircommand in Windows batch. - String concatenation: Used Groovy's string interpolation (
"dir ${commandPRM} /s /q") instead of'dir '+commandPRM + ' /s /q'for cleaner, more readable code. - Integer comparison: Converted the exit code string to an integer with
toInteger()before comparing to0—sincereadFilereturns a string, comparing it directly to the integer0would fail.
Expected Behavior After Fix
- The
echo ${commandPRM}commands will now print the correct value (firstWEB_SERVICE, thendir WEB_SERVICE /s /q). - The
dircommand will execute properly, and its exit code will be saved to thestatusfile. - If the
dircommand succeeds (exit code 0), thermdir ${rp} /s /qcommand will delete theWEB_SERVICEfolder from the workspace.
内容的提问来源于stack exchange,提问作者Hilal Emeksiz
相关产品推荐
相关产品推荐

