如何通过Jenkins Pipeline脚本向Jfrog Artifactory多目标上传多份.zip构件
Hey there! Let's break down how to write a Jenkins Pipeline script that uploads different types of ZIP artifacts (regular ZIPs and Maven-built ZIPs) to separate paths in JFrog Artifactory. I'll walk you through a complete, reusable example with clear explanations.
Prerequisites First
Before diving into the script, make sure you have these set up:
- The JFrog Artifactory Plugin installed in your Jenkins instance.
- An Artifactory server configured in Jenkins (with valid credentials that have upload permissions to your target repositories).
- Your project has regular ZIP artifacts (e.g., pre-built resources) and a Maven project that generates ZIP artifacts (via
mvn package).
Complete Jenkins Pipeline Script
pipeline { agent any tools { maven 'M3' // Replace with your configured Maven tool name in Jenkins } environment { ARTIFACTORY_SERVER_ID = 'your-artifactory-server' // Match your Jenkins-configured Artifactory server ID REGULAR_ZIP_SOURCE = 'src/main/resources/regular-artifacts/*.zip' // Path to your regular ZIPs MAVEN_ZIP_SOURCE = 'target/*.zip' // Path to Maven-built ZIPs REGULAR_TARGET_REPO = 'generic-local/regular-builds/${BUILD_NUMBER}/' // Target path for regular ZIPs MAVEN_TARGET_REPO = 'maven-local/com/yourcompany/your-project/${MAVEN_VERSION}/' // Target path for Maven ZIPs } stages { stage('Checkout Source Code') { steps { git url: 'https://your-git-repo-url.git', branch: 'main' } } stage('Build Maven Project') { steps { script { // Pull version from pom.xml to align with Maven repo structure def pom = readMavenPom file: 'pom.xml' env.MAVEN_VERSION = pom.version // Build the Maven project to generate the ZIP artifact sh 'mvn clean package -DskipTests' } } } stage('Upload Regular ZIPs to Artifactory') { steps { script { def artifactory = Artifactory.server(ARTIFACTORY_SERVER_ID) // Define upload spec for regular ZIPs def uploadSpec = """{ "files": [ { "pattern": "${REGULAR_ZIP_SOURCE}", "target": "${REGULAR_TARGET_REPO}" } ] }""" artifactory.upload(uploadSpec) echo "✅ Regular ZIPs uploaded to Artifactory path: ${REGULAR_TARGET_REPO}" } } } stage('Upload Maven ZIPs to Artifactory') { steps { script { def artifactory = Artifactory.server(ARTIFACTORY_SERVER_ID) // Define upload spec for Maven ZIPs (with optional properties) def uploadSpec = """{ "files": [ { "pattern": "${MAVEN_ZIP_SOURCE}", "target": "${MAVEN_TARGET_REPO}", "props": "build.number=${BUILD_NUMBER},project.name=${JOB_NAME}" } ] }""" artifactory.upload(uploadSpec) echo "✅ Maven ZIPs uploaded to Artifactory path: ${MAVEN_TARGET_REPO}" } } } } post { success { echo "🎉 All artifacts uploaded successfully to Artifactory!" } failure { echo "❌ Artifact upload failed. Check the logs for details." } } }
Key Details Explained
- Environment Variables: I've extracted all configurable values (server IDs, paths) into environment variables for easy maintenance—no need to dig through the script to update paths later.
- Maven Version Handling: Using
readMavenPompulls the version directly from yourpom.xml, ensuring the Maven ZIP is uploaded to the correct versioned path in Artifactory (following Maven repo conventions). - Upload Specs: The JSON-based upload specs let you precisely define which files to upload and where to send them. You can add more patterns or properties here if needed.
- Artifactory Server Reference: By using the pre-configured server ID from Jenkins, we avoid hardcoding credentials, keeping the script secure.
Quick Tips
- If your regular ZIPs are generated during the pipeline (not in Git), update the
REGULAR_ZIP_SOURCEpath to point to the output directory of that generation step. - Verify that your Artifactory repositories (
generic-localandmaven-localin the example) exist and that the Jenkins service account has upload permissions. - You can add a validation stage after uploads to confirm artifacts exist in Artifactory using the
Artifactory.download()method with a dry-run flag.
内容的提问来源于stack exchange,提问作者itgeek
相关产品推荐
相关产品推荐

