Chris B. Behrens
Chris B. Behrens

Reputation: 6295

How to Reference A Jenkins Global Shared Library

After reviewing the docs, a number of questions here on SO, and trying a dozen or so different script configurations, I cannot figure out how to reference a shared Groovy library. I've added the library like so:

Jenkins Library Config

This appears to be working. I'm referencing the script like so:

Jenkins script reference in build

You can see the error message therein:

Script1: 1: unable to resolve class Library , unable to find class for annotation @ line 1, column 1. @Library('sonarQubeAPI')_

The script code, not I think it matters, looks like this:

import groovy.json.JsonSlurper

class SonarQubeAPI{
    static string getVersion(){
        return "1.0";
    }

    static void getSonarStatus(projectKey){
        def sonarQubeUserToken = "USERTOKEN";
        def projectStatusUrl = "pathtosonarqube/api/qualitygates/project_status?projectKey=" + projectKey;

        println("Retrieving project status for " + projectKey);

        def json = getJson(sonarQubeUserToken, projectStatusUrl);

        def jsonSlurper = new JsonSlurper();
        def object = jsonSlurper.parseText(json);

        println(object.projectStatus.status);
    }

    static string getJson(userToken, url){
        def authString  = "${userToken}:".getBytes().encodeBase64().toString();

        def conn = url.toURL().openConnection();
        conn.setRequestProperty( "Authorization", "Basic ${authString}" );

        return conn.content.text;
    }
}

I'm probably just a magic character off, but I can't seem to lock it down.

Upvotes: 0

Views: 1537

Answers (1)

StephenKing
StephenKing

Reputation: 37600

Shared libraries are a feature of Jenkins Pipelines, not of Jenkins (core) itself. You can use them only in Pipeline jobs (and child types like Multibranch Pipeline).

Upvotes: 2

Related Questions