Operations 5 min read

Integrating Jenkins with Artifactory Using Pipelines and Shared Libraries

This guide explains how to configure Artifactory in Jenkins, set up server credentials, and use both a Jenkins Shared Library and a Jenkinsfile to automatically publish build artifacts to Artifactory as part of a CI/CD pipeline.

DevOps Engineer
DevOps Engineer
DevOps Engineer
Integrating Jenkins with Artifactory Using Pipelines and Shared Libraries

This article introduces the integration of Artifactory with Jenkins, emphasizing that Artifactory without CI tool integration lacks practical value.

In Jenkins, navigate to Manage Jenkins → Configure System , add an Artifactory server by providing a Server ID (an alias for later pipeline use) and the Artifactory URL (e.g., http://art.company.com:8040/artifactory ), then test the connection to confirm successful configuration.

Two pipeline approaches are demonstrated:

Method 1 – Jenkins Shared Library

# 文件1 build.groovy
def call() {
  pipeline {
    // ... other code omitted ...
    post {
      success {
        script {
          if (env.BRANCH_NAME == 'develop') {
            artifactory("${PATTERN_RELEASE_PATH}", "${TARGET_PATH}", "${BUILD_NAME}", "${BUILD_NUMBER}")
            artifactory("${PATTERN_DEBUG_PATH}",   "${TARGET_PATH}", "${BUILD_NAME}", "${BUILD_NUMBER}")
          } else if (env.BRANCH_NAME.startsWith('PR')) {
            artifactory("${PATTERN_RELEASE_PATH}", "${TARGET_PATH}", "${BUILD_NAME}", "${BUILD_NUMBER}")
          }
        }
      }
    }
  }
}
# 文件2 artifactory.groovy
import groovy.transform.Field
@Field artifactoryServerId   = "art-1"
@Field artifactoryURL        = "http://art.company.com:8040/artifactory"
@Field artifactoryCredential = "d1cbab74-823d-41aa-abb7"

def call(String patternPath, String targetPath, String buildName, String buildNumber) {
  rtServer (
    id: "${artifactoryServerId}",
    url: "${artifactoryURL}",
    credentialsId: "${artifactoryCredential}"
  )
  rtPublishBuildInfo (serverId: "${artifactoryServerId}")
  rtUpload (
    serverId: "${artifactoryServerId}",
    spec: "{\n  \"files\": [\n    {\n      \"pattern\": \"${patternPath}\",\n      \"target\": \"${targetPath}\"\n    }\n  ]\n}\",
    buildNumber: "${buildNumber}",
    buildName: "${buildName}"
  )
}

Method 2 – Jenkinsfile

# Combine the above scripts into a single Jenkinsfile
pipeline {
  // ... other code omitted ...
  stage('config art') {
    rtServer (
      id: "art-1",
      url: "http://art.company.com:8040/artifactory",
      credentialsId: "d1cbab74-823d-41aa-abb7"
    )
  }
  post {
    success {
      script {
        if (env.BRANCH_NAME == 'develop') {
          rtUpload (
            serverId: "art-1",
            spec: "{\n  \"files\": [\n    {\n      \"pattern\": \"/release/build/*.zip\",\n      \"target\": \"demo/develop/\"\n    }\n  ]\n}\",
            buildNumber: "${buildNumber}",
            buildName: "${buildName}"
          )
        } else if (env.BRANCH_NAME.startsWith('PR')) {
          rtUpload (
            serverId: "art-1",
            spec: "{\n  \"files\": [\n    {\n      \"pattern\": \"/release/build/*.zip\",\n      \"target\": \"demo/pull-request/\"\n    }\n  ]\n}\",
            buildNumber: "${buildNumber}",
            buildName: "${buildName}"
          )
        }
      }
    }
  }
}

After configuring and running a build, a blue number indicates a successful Jenkins build, and a green circle confirms that the Jenkins–Artifactory integration succeeded; clicking the green circle shows the artifacts have been pushed to Artifactory.

Logging into Artifactory reveals the build artifacts are now available for download.

With this integration, every successful Jenkins build automatically publishes its artifacts to Artifactory, streamlining the CI/CD workflow.

ci/cddevopspipelineGroovyJenkinsArtifactory
DevOps Engineer
Written by

DevOps Engineer

DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.