0
0
Jenkinsdevops~5 mins

External artifact repositories (Nexus, Artifactory) in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build software, you create files called artifacts like libraries or packages. External artifact repositories like Nexus or Artifactory store these files safely so you can share and reuse them easily.
When you want to save your built software files in a central place for your team to use.
When you need to share libraries between different projects without copying files manually.
When you want to keep track of different versions of your software packages.
When you want to download third-party libraries automatically during your build process.
When you want to improve build speed by caching dependencies in a repository.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
        // Simulate build step
      }
    }
    stage('Publish Artifact') {
      steps {
        script {
          def nexusUrl = 'http://nexus.example.com/repository/maven-releases/'
          def artifactPath = 'target/my-app-1.0.jar'
          sh "curl -u admin:admin123 --upload-file ${artifactPath} ${nexusUrl}my-app-1.0.jar"
        }
      }
    }
  }
}

This Jenkinsfile defines a simple pipeline with two stages: Build and Publish Artifact.

In the Publish Artifact stage, it uploads the built file to a Nexus repository using curl with authentication.

This shows how Jenkins can send artifacts to an external repository for storage and sharing.

Commands
This command uploads the built artifact file to the Nexus repository using curl with username and password authentication.
Terminal
curl -u admin:admin123 --upload-file target/my-app-1.0.jar http://nexus.example.com/repository/maven-releases/my-app-1.0.jar
Expected OutputExpected
No output (command runs silently)
-u admin:admin123 - Provides username and password for authentication
--upload-file target/my-app-1.0.jar - Specifies the file to upload
This command downloads the artifact from the Nexus repository to verify it was uploaded correctly.
Terminal
curl -u admin:admin123 http://nexus.example.com/repository/maven-releases/my-app-1.0.jar -O
Expected OutputExpected
No output (command runs silently)
-u admin:admin123 - Authentication credentials
-O - Saves the file with its original name
Key Concept

If you remember nothing else from this pattern, remember: external artifact repositories safely store and share your build files so teams can reuse and manage software versions easily.

Common Mistakes
Uploading artifacts without authentication or with wrong credentials
The repository will reject the upload and the artifact won't be saved.
Always provide correct username and password or token when uploading artifacts.
Not verifying the artifact upload by downloading or checking the repository
You might think the upload succeeded but the artifact could be missing or corrupted.
After upload, download the artifact or check the repository UI to confirm it is stored.
Summary
Use curl with authentication to upload build artifacts to Nexus or Artifactory repositories.
Verify artifact upload by downloading the file or checking the repository UI.
Configure Jenkins pipelines to automate artifact publishing to external repositories.