简体   繁体   中英

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库配置

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

Jenkins脚本参考构建

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM