Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
GouravIN opened this issue Feb 2, 2021 · 0 comments

Comments

@GouravIN
Copy link

GouravIN commented Feb 2, 2021

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}"
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant