How to Publish an Artifact to Nexus or Artifactory in Jenkins
Bind the repository credentials, then PUT the versioned artifact to the Nexus or Artifactory URL with curl.
Use withCredentials to bind a username/password, then curl -u upload the built artifact to a versioned path in the repository manager.
Steps
- Store the repository credentials in Jenkins.
- Bind them with
withCredentials. - Upload the versioned artifact with
curl -u "$USER:$PASS" --upload-file.
Pipeline
Jenkinsfile
pipeline {
agent any
stages {
stage('Publish') {
steps {
withCredentials([usernamePassword(credentialsId: 'artifactory',
usernameVariable: 'REPO_USER', passwordVariable: 'REPO_PASS')]) {
sh '''
curl -f -u "$REPO_USER:$REPO_PASS" \
--upload-file build/app-$BUILD_NUMBER.jar \
"https://repo.example.com/libs-release/com/acme/app/$BUILD_NUMBER/app-$BUILD_NUMBER.jar"
'''
}
}
}
}
}Gotchas
- Pass
-fso curl returns a non-zero exit on an HTTP error and fails the stage. - Publish to a release repo with an immutable version path so a tag is never overwritten.
Related guides
How to Build and Push a Docker Image in JenkinsBuild a Docker image in a Jenkins Declarative Pipeline and push it to a registry with docker.withRegistry, ta…
How to Promote a Build Between Environments in JenkinsPromote the exact artifact that passed staging to production in Jenkins by reusing the same build identifier…