Jenkins与Artifactory集成的最佳工作流技术咨询
Hey there! Let me walk you through the best practices for integrating Jenkins with Artifactory, focusing on CLI/API/Jenkins plugin workflows (no GUI required)—since you mentioned you're new to Artifactory and couldn't find clear guidance in the docs.
Push Artifacts: Ditch Manual Build Tracking, Use Build Info
Your second approach (using build info publishing) is way more scalable and reliable than maintaining a last-build.txt file. Build Info automatically links your artifacts to Jenkins build metadata (build number, job name, timestamp, etc.), and Artifactory persists this data so you never have to worry about manual file sync or loss.
With Jenkins Artifactory Plugin
Use the rtUpload and rtPublishBuildInfo steps in your Jenkinsfile to tie artifacts to builds:
// Upload artifacts to Artifactory, associating them with the current build rtUpload( serverId: 'your-artifactory-server-id', // Configured in Jenkins global settings spec: '''{ "files": [ { "pattern": "my-build-dev-*.tar.gz", "target": "my-repository/dev/" // Your target repo path } ] }''', buildName: 'my-dev-build', // Name your build for easy tracking buildNumber: env.BUILD_NUMBER // Use Jenkins' auto-generated build number ) // Publish the build metadata to Artifactory rtPublishBuildInfo(serverId: 'your-artifactory-server-id')
With JFrog CLI
If you prefer scripting outside Jenkins, use jf rt upload with build parameters, then publish the build info:
# Upload the artifact and link to the current build jf rt upload "my-build-dev-${BUILD_NUMBER}.tar.gz" my-repository/dev/ \ --build-name my-dev-build \ --build-number ${BUILD_NUMBER} # Publish the build metadata to Artifactory jf rt build-publish my-dev-build ${BUILD_NUMBER}
Pull Latest Artifacts: Leverage Artifactory's Build Metadata
Forget maintaining last-build.txt—Artifactory can tell you exactly what the latest build number is for a given build name. Here's how to pull the latest artifact without manual tracking:
With Jenkins Artifactory Plugin
Fetch the latest build info first, then download the corresponding artifact:
// Get metadata for the latest build of "my-dev-build" def latestBuildInfo = rtBuildInfo( serverId: 'your-artifactory-server-id', buildName: 'my-dev-build', buildNumber: 'latest' ) // Download the artifact from that latest build rtDownload( serverId: 'your-artifactory-server-id', spec: '''{ "files": [ { "pattern": "my-repository/dev/my-build-dev-{build.number}.tar.gz", "target": "./downloads/" } ] }''', buildName: 'my-dev-build', buildNumber: latestBuildInfo.buildNumber )
With JFrog CLI
Query the latest build number, then download the artifact:
# Fetch the latest build number (uses jq for JSON parsing—install it first if needed) LATEST_BUILD=$(jf rt build-info my-dev-build latest --format json | jq -r '.buildInfo.number') # Download the latest artifact jf rt download "my-repository/dev/my-build-dev-${LATEST_BUILD}.tar.gz" ./downloads/
Pull Historical Artifacts: Target Specific Builds Directly
With Build Info enabled, pulling historical artifacts is straightforward—just specify the exact build number you need:
With Jenkins Artifactory Plugin
rtDownload( serverId: 'your-artifactory-server-id', spec: '''{ "files": [ { "pattern": "my-repository/dev/my-build-dev-{build.number}.tar.gz", "target": "./downloads/" } ] }''', buildName: 'my-dev-build', buildNumber: '128' // Replace with your target historical build number )
With JFrog CLI
# Directly download the artifact from build 128 jf rt download "my-repository/dev/my-build-dev-128.tar.gz" ./downloads/
Bonus: Query Build History with API
If you need to list all past builds for a project, use Artifactory's REST API:
# Get the last 10 builds for "my-dev-build" curl -X GET "http://your-artifactory-url/artifactory/api/build/my-dev-build?start=0&limit=10" \ -u your-username:your-password
Overall Recommended Workflow
- Push: Every Jenkins build uploads artifacts and publishes Build Info to Artifactory—no manual
last-build.txtneeded. - Pull Latest: Fetch the latest build number from Artifactory's metadata, then pull the corresponding artifact.
- Pull Historical: Target specific build numbers directly, using plugin/CLI commands or API queries to find past builds.
This approach is far more reliable than manual file tracking—Artifactory handles all build metadata persistence, so you never lose track of which artifact belongs to which build.
内容的提问来源于stack exchange,提问作者oTolev

