jenkins-pipeline-shared icon indicating copy to clipboard operation
jenkins-pipeline-shared copied to clipboard

Is importing a class method inside a shared lib groovy script possible?

Open GouravIN opened this issue 4 years ago • 0 comments

Objective is to have certain methods which are recursive even among the shared lib files to be kept inside tools class. and call them onto the shared lib files.

Jenkinsfile:

@Library('shared-library') _

pipeline { agent any parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') booleanParam(name: 'DEBUG_BUILD', defaultValue: false, description: '') } stages { stage("Test") { steps { script { command label: 'running sleep', 'sleep 10' } } post { always { cleanWs() } } } } }

Shared lib file: command.groovy

#!/usr/bin/env groovy

import tools t = new tools()

def call(Map args = [:], String script) { script = "${args.sudo ? 'sudo ' : ''}${script}" def label = args.label ?: script sh label: label, script: script

t.printHello("Gourav")

}

Utility groovy class:

class tools { def printHello(arg) { echo "${arg}" } }

GouravIN avatar Feb 02 '21 16:02 GouravIN