'Call helpers in a Jenkins Shared Library

I have a Jenkins Shared Library organized like that :

 +- src                     
 |   +- main
 |       +- jenkins_shared_library
 |           +- helpers
 |               +- GitHelper.groovy  
 |               +- ArtifactoryHelper.groovy

with for example the ArtifactoryHelper :

#!/usr/bin/env groovy
package main.jenkins_shared_library.helpers

/** Helper for artifactory commands */
class ArtifactoryHelper implements Serializable
{
    ArtifactoryHelper(String projectName_artifactory='', String organizationName='', String projectName='')
    {
     ...
    }

    def downloadStable()
    {
    ...
    }
}

How I call thess class and method into my jenkins pipelines ?



Solution 1:[1]

It goes over it in more detail here but you need to import your shared library into your Jenkinsfile and then declare an instance of your class. After that you can call it.

@Library('somelib')
import main.jenkins_shared_library.helpers.ArtifactoryHelper

node ('build') {
    ArtifactoryHelper artifactoryHelper = new ArtifactoryHelper("projectName_artifactory", "organizationName", "projectName")
    
    stage('Download Stable') {
        artifactoryHelper.downloadStable()
    }
}

This assumes you already added your library as a shared library in the Jenkins management called "somelib".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1