How to Use Artifactory with Jenkins for Artifact Management
To use
Artifactory with Jenkins, install the Artifactory plugin in Jenkins, configure the Artifactory server details, and then use pipeline steps or freestyle jobs to upload or download artifacts. This setup helps automate artifact management during your build and deployment processes.Syntax
The basic syntax to use Artifactory in Jenkins pipelines involves defining the Artifactory server, creating a build info object, and using rtUpload or rtDownload steps to manage artifacts.
Key parts:
rtServer: Defines the Artifactory server connection.rtUpload: Uploads files to Artifactory.rtDownload: Downloads files from Artifactory.buildInfo: Tracks build metadata for traceability.
groovy
def server = Artifactory.server 'ARTIFACTORY_SERVER_ID' def upload = Artifactory.newUpload() upload.pattern = 'target/*.jar' upload.target = 'libs-release-local/my-app/' def buildInfo = Artifactory.newBuildInfo() pipeline { agent any stages { stage('Upload') { steps { script { server.upload(upload, buildInfo) server.publishBuildInfo(buildInfo) } } } } }
Example
This example shows a Jenkins pipeline that uploads a built JAR file to Artifactory and publishes build info for tracking.
groovy
pipeline {
agent any
environment {
ARTIFACTORY_SERVER = 'my-artifactory'
}
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Upload to Artifactory') {
steps {
script {
def server = Artifactory.server env.ARTIFACTORY_SERVER
def uploadSpec = '''{
"files": [
{
"pattern": "target/*.jar",
"target": "libs-release-local/my-app/"
}
]
}'''
def buildInfo = Artifactory.newBuildInfo()
server.upload spec: uploadSpec, buildInfo: buildInfo
server.publishBuildInfo buildInfo
}
}
}
}
}Output
[Pipeline] sh
[INFO] Scanning for projects...
[INFO] Building jar: /workspace/target/my-app.jar
[Pipeline] script
Uploading 1 artifact(s) to Artifactory...
Build info published successfully.
Common Pitfalls
- Not installing or configuring the Artifactory plugin in Jenkins before use.
- Using incorrect Artifactory server ID or credentials causing connection failures.
- Wrong file patterns in upload/download specs leading to no artifacts transferred.
- Forgetting to publish build info, which breaks traceability in Artifactory.
Always verify server configuration and test upload/download steps separately.
groovy
pipeline {
agent any
stages {
stage('Wrong Upload') {
steps {
script {
def server = Artifactory.server 'wrong-id'
def uploadSpec = '{"files":[{"pattern":"target/*.jar","target":"libs-release-local/my-app/"}]}'
def buildInfo = Artifactory.newBuildInfo()
// This will fail due to wrong server ID
server.upload spec: uploadSpec, buildInfo: buildInfo
server.publishBuildInfo buildInfo
}
}
}
}
}
// Correct way: Use the right server ID configured in Jenkins global settings.Quick Reference
- Install Artifactory Plugin: Manage Jenkins > Plugins > Install 'Artifactory'.
- Configure Server: Manage Jenkins > Configure System > Artifactory Servers.
- Upload Artifacts: Use
server.upload()with upload spec. - Download Artifacts: Use
server.download()with download spec. - Publish Build Info: Call
server.publishBuildInfo(buildInfo)after uploads.
Key Takeaways
Install and configure the Artifactory plugin in Jenkins before using it.
Use Jenkins pipeline steps like rtUpload and rtDownload to manage artifacts.
Always publish build info to Artifactory for traceability.
Verify server IDs and credentials to avoid connection errors.
Test artifact upload/download separately to catch configuration issues early.