带多动态参数的Jenkins任务:基于Artifactory的多Zip打包需求
Alright, let's tackle setting up this parameterized Jenkins job that pulls dynamic Zip lists from your Artifactory repos (A and B) and packages your selected files. I'll break this down into actionable steps that are easy to follow:
Prerequisites First
Before diving in, make sure you have these covered:
- The Artifactory Plugin installed in Jenkins (it simplifies auth and API interactions with Artifactory)
- The Active Choices Plugin installed (this is critical for creating dynamic, API-driven parameter lists)
- Network access from your Jenkins server/agents to your Artifactory instance
- Your Artifactory API endpoints for listing files in Repo A and Repo B (they should return structured JSON with file details)
Step 1: Create a New Parameterized Freestyle Job
- Head to your Jenkins dashboard → Click New Item
- Name your project (e.g., "Multi-Zip Package Builder") → Select Freestyle project → Hit OK
Step 2: Add Dynamic Parameters for Repo A and Repo B
We'll use Active Choices parameters to pull live Zip lists from your Artifactory APIs.
For Repo A (Single Selection)
- Under the General section, check This project is parameterized
- Click Add Parameter → Select Active Choices Reactive Parameter
- Configure the parameter:
- Name:
REPO_A_ZIPS - Description: "Pick one Zip file from Repo A"
- Choice Type: Single Select
- Script: Choose Groovy Script and paste this (adjust API URL and auth logic to match your setup):
// Replace with your Artifactory Repo A list API endpoint def apiUrl = "http://your-artifactory-url/artifactory/api/storage/repoA?list&deep=0&listFolders=0" // Secure way: Pull credentials from Jenkins credential store (replace "artifactory-creds" with your credential ID) def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class, Jenkins.instance, null, null ).find { it.id == "artifactory-creds" } def auth = "Basic " + "${creds.username}:${creds.password}".bytes.encodeBase64().toString() // Fetch and parse API response def connection = new URL(apiUrl).openConnection() connection.setRequestProperty("Authorization", auth) def response = new JsonSlurper().parseText(connection.content.text) // Filter and return only .zip files return response.children.findAll { it.uri.endsWith(".zip") }.collect { it.uri.substring(1) } - Uncheck Run on master if your agents have access to Artifactory (better for scalability)
- Name:
For Repo B (Multi-Selection)
- Click Add Parameter again → Select Active Choices Reactive Parameter
- Configure the parameter:
- Name:
REPO_B_ZIPS - Description: "Select one or more Zip files from Repo B"
- Choice Type: Multi Select
- Script: Use this Groovy script (similar to Repo A, just point to Repo B's API):
def apiUrl = "http://your-artifactory-url/artifactory/api/storage/repoB?list&deep=0&listFolders=0" def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class, Jenkins.instance, null, null ).find { it.id == "artifactory-creds" } def auth = "Basic " + "${creds.username}:${creds.password}".bytes.encodeBase64().toString() def connection = new URL(apiUrl).openConnection() connection.setRequestProperty("Authorization", auth) def response = new JsonSlurper().parseText(connection.content.text) return response.children.findAll { it.uri.endsWith(".zip") }.collect { it.uri.substring(1) }
- Name:
Step 3: Download Selected Zips from Artifactory
Next, add build steps to pull the chosen files into a single directory. We'll use the Artifactory Plugin for this (cleaner than raw curl commands).
- Add build step → Artifactory Download
- Configure the download specs:
- For Repo A: Use this JSON spec to pull the selected Zip:
{ "files": [ { "pattern": "repoA/${REPO_A_ZIPS}", "target": "./downloads/" } ] } - For Repo B: Use a Groovy script to generate a dynamic spec for all selected Zips:
def repoBZips = REPO_B_ZIPS.split(',') def files = repoBZips.collect { [pattern: "repoB/${it}", target: "./downloads/"] } return [files: files] - This will collect all selected Zips into the
./downloads/folder.
- For Repo A: Use this JSON spec to pull the selected Zip:
Step 4: Package All Zips into a Single Archive
Add a final build step to bundle the downloaded files into your desired package format.
For Linux/macOS agents:
Add an Execute shell step with:
zip -r final-multi-zip-package.zip ./downloads/
For Windows agents:
Add an Execute Windows batch command step with:
powershell Compress-Archive -Path ./downloads/* -DestinationPath final-multi-zip-package.zip
Optional: Upload the Final Package to Artifactory
If you want to store the finished package back in Artifactory, add an Artifactory Upload build step with this spec:
{ "files": [ { "pattern": "final-multi-zip-package.zip", "target": "your-target-repo/package-outputs/" // Replace with your target repo path } ] }
Key Tips for Success
- Security: Never hardcode credentials! Always use Jenkins' credential store to retrieve auth details.
- API Testing: Test your Artifactory list API endpoints first with
curlor Postman to ensure they return the correct Zip file list. - Plugin Updates: Keep the Artifactory and Active Choices plugins up to date to avoid compatibility issues.
内容的提问来源于stack exchange,提问作者Springhills

