Home » DevOps » Checking for Artifactory in a Jenkins Pipeline

About David Wong

David Wong
David is a software developer who has worked in the UK, Japan and Australia. He likes building apps in Java, web and Android technologies.

Checking for Artifactory in a Jenkins Pipeline

One of my projects uses the Artifactory as the repository manager. Unfortunately when doing a Jenkins pipeline build, I sometimes forget to ensure the Artifactory server is up first and find the job has failed after running for a while.

I’ve added some script to my Jenkinsfile that will check for the Artifactory server early on and fail fast if it is not running.

Artifactory Check

For my purposes I just try to ping the Artifactory server. This can be done by sending a http request: http://[Your Artifactory URL]/artifactory/api/system/ping and if successful should return the string ‘OK’.

Jenkins Pipeline example

This particular example requires the HTTP Request plugin to be installed. I added a declarative stage in the pipeline before the actual build for the Artifactory check.

In this stage the HTTP request call from the plugin will be successful if the response status code is in the default range (100 to 399) and the response content includes the string ‘OK’. If the response does not fulfil these conditions, then the Jenkins job will fail quickly.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pipeline {
  agent any
  options {
    // Stop the build early in case of compile or test failures
    skipStagesAfterUnstable()
  }
  stages {
    stage('Build') {
      environment {
        // artifactory server url
        artifactoryUrl = 'http://[Your Artifactory URL]/artifactory'
     }
 
    // stage to check the Artifactory server is up, else will fail the job
    stage('Artifactory check') {
      steps {
        script {
          echo 'Pinging Artifactory'
 
          // for a successful ping, the response status code must be in default acceptable range (100:399)
          // and contain 'OK' in the content
          def pingResponse = httpRequest url: "${artifactoryUrl}/api/system/ping", validResponseContent: 'OK'
 
          echo "Ping response status code: ${pingResponse.status}"
          echo "Ping response: ${pingResponse.content}"
        }
      }
    }
 
    // continue with other stages for the job

Alternatively if you don’t want the Artifactory check to fail the job, just change the parameters to the HTTP request to allow all response status codes and any response content to pass. Then if the ping fails, you can just set a flag, send a notification, print some info, etc, instead and let the job continue.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
stage('Artifactory check') {
  steps {
    script {
 
      // Allow all response codes returned from the Artifactory ping request so it doesn't fail,
      // normal allowable codes are 100:399.
      def pingResponse = httpRequest url: "${artifactoryUrl}${artifactoryPingPath}", validResponseCodes: '100:599'
 
      echo "Ping response status code: ${pingResponse.status}"
      echo "Ping response: ${pingResponse.content}"
 
      if (pingResponse.status == 200 && pingResponse.content == 'OK')
        // flag successful check
      else
        // flag ping failure
    }
  }
}

Published on Java Code Geeks with permission by David Wong, partner at our JCG program. See the original article here: Checking for Artifactory in a Jenkins Pipeline

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments