Jenkins groovy code fails when called from a folder shared library - jenkins

I have a groovy class which I am attempting to import from a folder-level shared library.
Here is the groovy class -
package abc.esmm
#Singleton
class JiraCommands implements Serializable
{
def steps
def jiraCommandsTool
def Initialize(steps)
{
this.steps = steps
jiraCommandsTool = "${steps.WORKSPACE}/JenkinsPipeline/UtilityScripts/bin/JiraCommands"
}
def AddFixVersionToJiraIssues(jiraIssues, fixVersion, overwriteFixVersionParam=false)
{
def overwriteFixVersion = "False"
if(overwriteFixVersionParam)
{
overwriteFixVersion = "True"
}
steps.sh(returnStdout: true, script: "${jiraCommandsTool} -command addFixVersionToJira -jiraIssues \"${jiraIssues}\" -fixVersion ${fixVersion} -overwriteFixVersion ${overwriteFixVersion}").trim()
}
}
I try to create an instance of this class with this pipeline code :
#Library('LotteryFolderPipelineLibs')
import abc.esmm.JiraCommands
node('All_LinuxBuildPool')
{
JiraCommands.instance.Initialize(this)
}
This works ok when called from a Jenkins Global shared library, but not when called from a folder-level shared library. The global shared library and the folder-level shared library point to the same code. When called from a folder-level shared library I receive this error :
CpsCallableInvocation{methodName=getInstance, call=null, receiver=class abc.esmm.JiraCommands, arguments=[]}
Finished: FAILURE
Does anyone know why this is happening ?

Related

Jenkins class with stage inside of it

I'm having some issues trying to use the stage function into a class
I have 3 files
My pipeline
A shared function
A class
In my shared function I'm wanting to use this class, and in the class, I have my stage, like this:
Shared function:
new myClass(text: text).print()
And in my class I have this:
class myClass implements Serializable {
String text
myClass print() {
stage("Printing message") {
echo text
}
}
}
But when I do this I receive an error saying the myClass doesn't have the stage function.
Anyone knows what I can do with this?

How to read a workspace file from within Jenkins Shared Library?

I have a Jenkins pipeline, it imports a shared library which in turn "grabs" a dependency. I want to call a method on the class which takes a File parameter.
But the file doesn't exist when I try to access it in the shared library.
The Pipeline
#Library('my shared library')
pipeline {
// ...
stages {
stage {
steps {
script {
f = new File("foo.txt")
libFn(myFile: f)
}
}
}
}
}
The Jenkins Shared Library
// ./vars/libFn.groovy
#Grab("3rd party java library")
import LibraryClass
def call(Map params) {
LibraryClass libraryClass = new LibraryClass();
libraryClass.processFile(params.myFile);
}
The 3rd Party Java Library
public class LibraryClass {
public void processFile(File file) {
// do something with a file
}
}

Accessing plugins in jenkins shared library classes

I wanted to create a class in /src directory which can access docker and other plugin steps.
So I have a class that looks like this;
class someClassName implements Serializable {
def env
def steps
def docker
someclassName(env, steps, docker){
this.step = step
this.docker = docker
this.env = env
}
def runCommands(String img, List commands){
docker.image(img).inside {
commands.each {
steps.sh it
}
}
}
Now in Jenkinsfile I will have
#Library('name#branch') _
def x = new com.JenkinsLibrary.someClassName(env, steps, docker)
x.runCommands('maven:latest', ['mvn clean', 'mvn test'])
What i dont like is how I have a constructor for each object so that I can call methods that belong to that object. Is there a better object that i can use for my constructor instead of having to use env, steps, docker, etc?
Also, what pipeline steps are available under steps object? same for env?
Try sending along the surrounding CPSScript:
class someClassName implements Serializable {
def script
someclassName(script){
this.script = script
}
def runCommands(String img, List commands){
script.docker.image(img).inside {
commands.each {
script.sh it
}
}
}
}
and you provide the script by using this in the pipeline script:
#Library('name#branch') _
def x = new com.JenkinsLibrary.someClassName(this)
x.runCommands('maven:latest', ['mvn clean', 'mvn test'])

how to run a complete Jenkins scripted pipeline stage from shared libraries src file

I recently started with Jenkins shared libraries in Jenkins pipeline.
I created a "func.groov" class and located it under "src/org/prj/func.groovy" :
package org.prj
import jenkins.model.
class func implements Serializable {
def steps
func(steps) {
this.steps = steps
}
def sh(args) {
steps.sh "echo ${args}"
}
def setLBL(CurrentNodeName,newLabelName){
jenkins.model.Jenkins.instance.slaves.each{ slave ->
if (slave.getNodeName() == CurrentNodeName){
slave.setLabelString(newLabelName)
}
}
}
Jenkinsfile (scripted) looks like:
#Library('prj') import org.prj.func
def utils = new func(steps)
node(lbl)
{
stage("A"){
Build_node_lbl = env.NODE_NAME+System.currentTimeMillis()
utils.setLBL(env.NODE_NAME,Build_node_lbl)
}
}
so currently it works. my question is how to create a full stage (like "A") as a function in func.groovy shared lib which will include, for example:
GIT checkout step
sh compilation step
Artifactory deploy step
Im actually looking to create a "building blocks" (a "Build" in my example) with Jenkins pipeline and shard libraries.
1. With Class Instantiation
You can create a class like you would do in Java. Then in your Jenkinsfile you instantiate the class and call its function.
src/org/prj/MyPipeline.groovy:
package org.prj
class MyPipeline {
def steps
MyPipeline(steps) {this.steps = steps}
public def build() {
steps.node('lbl') {
steps.stage('A') {
// Do build stuff
// steps.sh(..)
}
}
}
}
Jenkinsfile:
import org.prj.MyPipeline
def pipeline = new MyPipeline(this)
pipeline.build()
2. With Static Functions
You may also work with static contexts, without instantiation. However, this would require to hand over the caller context to the pipeline:
src/org/prj/MyPipeline.groovy:
package org.prj
class MyPipeline {
public static def build(caller) {
caller.node('lbl') {
caller.stage('A') {
// Do build stuff
caller.sh(..)
}
}
}
}
Jenkinsfile:
import org.prj.MyPipeline
MyPipeline.build(this)

jenkins use other variables in my own shared library

I'm writing my own shared library. Now I want to use global variable in my code. How can I make that happen ?
I.E. I write a class.
class MyWork {
build() {
// here I want to use global docker(which is docker-plugin)
docker.doSomething {
}
}
}
enable the library in 'Global Pipeline Libraries' (i add the library 'pipeline-shared-lib')
create a shared-library (with the necessary structure)
src/net/kukinet/Utils.groovy
package net.kukinet;
def myVar = 1
def sayHello() {
print ('hello')
}
create a pipeline job and create the object
JenkinsJob.groovy
#!groovy
// this need to be enabled in jenkins configuration ( in: manage jenkins)
#Library('pipeline-shared-lib') import net.kukinet.*
node (){
u = new net.kukinet.Utils();
stage('preperations') {
print(u.myVar)
u.sayHello()
}
}

Resources