Skip to content
Latchkey

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 -f so 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

Run this faster and cheaper on Latchkey managed runners. Start free →