How to build and deploy artifacts using JFrog CLI in a Jenkins pipeline

Joey Naor
2020-11-05 07:32

Intro:
The JFrog CLI is a compact and smart client that provides a simple interface that automates access to JFrog products simplifying your automation scripts and making them more readable and easier to maintain.

In complex Jenkins pipeline scripts, we often integrate the JFrog CLI in order to use one or more of its various functions.

It is possible to invoke a JFrog CLI call which will build and deploy an artifact and its build info to Artifactory. This can be helpful for users who prefer relying on the CLI and refrain from using the Artifactory Jenkins Plugin.

How it is done:
Jenkins has a list of predefined environment variables. Among these variables, we have JOB_NAME, and BUILD_NUMBER. We can set these variables to JFROG_CLI_BUILD_NAME & JFROG_CLI_BUILD_NUMBER, which will be picked up automatically by the JFrog CLI when building & publishing the project.

Here’s an example for a declarative Jenkins pipeline which builds and publishes a Maven project and its build info:pipeline {
    agent any
    environment {
        JFROG_CLI_BUILD_NAME = "${env.JOB_NAME}"
        JFROG_CLI_BUILD_NUMBER = "${env.BUILD_NUMBER}"
    }
    stages {
        stage ('Run JFrog CLI') {
            steps {
                sh 'jfrog rt mvn -f /path/to/pom.xml clean install' // build & deploy artifacts
                sh 'jfrog rt bp' // publish build info
            }
        }
    }
}

In the above pipeline, we first declare the CLI environment variables, and set them to the already existing Jenkins variables – build name (job name) and build number. 

Afterwards, we invoke the JFrog CLI binary to run ‘mvn clean install’ on our pom.xml file. This will resolve dependencies from Artifactory, and deploy the artifacts as well.

Lastly, we invoke the build publish feature (bp), which uses our defined CLI variables (JFROG_CLI_BUILD_NAME & JFROG_CLI_BUILD_NUMBER) to deploy the build info.

Please note that for this setup, you will need to fully configure your JFrog CLI (jfrog rt c, jfrog rt mvnc), and move the binary to /usr/bin in order to have it accessible by the Jenkins user.