You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Pipeline中Windows环境批量解压嵌套Zip文件问题求助

Solution for Batch Unzipping Sub Zips in Windows Jenkins Pipeline

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-ChildItem finds all files matching the project_*.zip pattern in your target directory.
  • $_.BaseName extracts the zip filename without the .zip extension, so each zip gets unzipped to a dedicated folder (mimicking your Linux reference command).
  • Expand-Archive is 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 eachFileMatch uses a regex pattern (~/project_.*\.zip/) to find all matching zip files.
  • We explicitly pass each zip's full path to the unzip step, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:04:22