Jenkins Pipeline中Windows环境批量解压嵌套Zip文件问题求助
Got it, let's fix this batch unzip issue you're facing on Windows. The core problem is that Jenkins' built-in unzip step doesn't natively support wildcard matching for multiple files—it only accepts a single, explicit file path. When you pass project_*.zip, it tries to find a file literally named that, hence the "does not exist" error.
Below are two reliable solutions tailored for Windows environments:
Option 1: Use PowerShell (Native Windows Approach)
Windows PowerShell has built-in commands to handle file traversal and unzipping, making this a lightweight and robust choice. Here's how to integrate it into your Pipeline:
pipeline { agent any stages { stage('Batch Unzip Sub Zips') { steps { script { def targetDir = "D:\\jenkins\\DEV\\extract\\project" // Execute PowerShell to match and unzip each sub-zip powershell """ Get-ChildItem -Path "${targetDir}\\project_*.zip" | ForEach-Object { // Create an extract directory named after the zip file (without .zip suffix) $extractFolder = Join-Path -Path "${targetDir}" -ChildPath $_.BaseName // Create the directory if it doesn't exist if (-not (Test-Path -Path $extractFolder)) { New-Item -ItemType Directory -Path $extractFolder | Out-Null } // Unzip the file to the target directory (overwrite existing files with -Force) Expand-Archive -Path $_.FullName -DestinationPath $extractFolder -Force } """ } } } } }
Key Notes:
Get-ChildItemfinds all files matching theproject_*.zippattern in your target directory.$_.BaseNameextracts the zip filename without the.zipextension, so each zip gets unzipped to a dedicated folder (mimicking your Linux reference command).Expand-Archiveis Windows' native unzip command—no extra tools needed.
Option 2: Use Groovy to Iterate and Call Jenkins unzip Step
If you prefer using Jenkins' built-in unzip step (instead of PowerShell), you can use Groovy to manually iterate over matching files and call unzip for each one:
pipeline { agent any stages { stage('Batch Unzip via Groovy') { steps { script { def targetDir = new File("D:\\jenkins\\DEV\\extract\\project") // Match all files starting with "project_" and ending with ".zip" targetDir.eachFileMatch(~/project_.*\.zip/) { zipFile -> // Create extract directory using the zip filename (without extension) def extractDir = new File(targetDir, zipFile.name.take(zipFile.name.lastIndexOf('.'))) // Ensure the directory exists extractDir.mkdirs() // Call Jenkins' unzip step for each individual zip file unzip zipFile: zipFile.absolutePath, dir: extractDir.absolutePath, quiet: true } } } } } }
Key Notes:
- Groovy's
eachFileMatchuses a regex pattern (~/project_.*\.zip/) to find all matching zip files. - We explicitly pass each zip's full path to the
unzipstep, which avoids the wildcard issue since it's a single valid file path every time. extractDir.mkdirs()ensures the target folder exists before unzipping.
Why Your Original Attempt Failed
Jenkins' unzip step's zipFile parameter does not resolve wildcards—it treats the input as a literal file name. When you passed D:\\jenkins\\DEV\\extract\\project\\project_*.zip, it looked for a file with that exact name (including the *), which doesn't exist in your directory.
内容的提问来源于stack exchange,提问作者asur

