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'])
Related
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 ?
I'm trying to create a JobGenerator class that will pass a build step down to the calling instance. I'm running into an issue where if I get this error when I try to run this:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.cps.CpsClosure2.build() is applicable for argument types: (java.util.LinkedHashMap) values: [[job:FooJob]]
class BuildGenerator implements Serializable {
static def generateJob() {
return [
"TestJob",
{ ->
build(
job: 'FooJob'
)
},
]
}
}
node(){
def tasks = [:]
def label
def task
stage("Build") {
def generator = new BuildGenerator()
tasks["Testing"] = generator.generateJob()[1]
parallel tasks
}
}
If I remove the generateJob function outside of the class then it works fine. What am I doing wrong with closures here? I'm new to groovy/jenkins world.
Well... This is the way Groovy/Jenkins pipeline work. build is available inside node as the rest of steps and functions. If you wish to access these you have to pass the CPS instance to the method, like this (or use constructor to pass the instance only once):
class BuildGenerator implements Serializable {
static def generateJob(script) {
return [
"TestJob",
{ ->
script.build(
job: 'FooJob'
)
},
]
}
}
node(){
def tasks = [:]
def label
def task
stage("Build") {
def generator = new BuildGenerator()
tasks["Testing"] = generator.generateJob(this)[1]
parallel tasks
}
}
When I run the below Jenkins pipeline script:
def some_var = "some value"
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
pipeline {
agent any
stages {
stage ("Run") {
steps {
pr()
}
}
}
}
I get this error:
groovy.lang.MissingPropertyException: No such property: some_var for class: groovy.lang.Binding
If the def is removed from some_var, it works fine. Could someone explain the scoping rules that cause this behavior?
TL;DR
variables defined with def in the main script body cannot be accessed from other methods.
variables defined without def can be accessed directly by any method even from different scripts. It's a bad practice.
variables defined with def and #Field annotation can be accessed directly from methods defined in the same script.
Explanation
When groovy compiles that script it actually moves everything to a class that roughly looks something like this
class Script1 {
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
def run() {
def some_var = "some value"
pipeline {
agent any
stages {
stage ("Run") {
steps {
pr()
}
}
}
}
}
}
You can see that some_var is clearly out of scope for pr() becuse it's a local variable in a different method.
When you define a variable without def you actually put that variable into a Binding of the script (so-called binding variables). So when groovy executes pr() method firstly it tries to find a local variable with a name some_var and if it doesn't exist it then tries to find that variable in a Binding (which exists because you defined it without def).
Binding variables are considered bad practice because if you load multiple scripts (load step) binding variables will be accessible in all those scripts because Jenkins shares the same Binding for all scripts. A much better alternative is to use #Field annotation. This way you can make a variable accessible in all methods inside one script without exposing it to other scripts.
import groovy.transform.Field
#Field
def some_var = "some value"
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
//your pipeline
When groovy compiles this script into a class it will look something like this
class Script1 {
def some_var = "some value"
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
def run() {
//your pipeline
}
}
Great Answer from #Vitalii Vitrenko!
I tried program to verify that. Also added few more test cases.
import groovy.transform.Field
#Field
def CLASS_VAR = "CLASS"
def METHOD_VAR = "METHOD"
GLOBAL_VAR = "GLOBAL"
def testMethod() {
echo "testMethod starts:"
def testMethodLocalVar = "Test_Method_Local_Var"
testMethodGlobalVar = "Test_Metho_Global_var"
echo "${CLASS_VAR}"
// echo "${METHOD_VAR}" //can be accessed only within pipeline run method
echo "${GLOBAL_VAR}"
echo "${testMethodLocalVar}"
echo "${testMethodGlobalVar}"
echo "testMethod ends:"
}
pipeline {
agent any
stages {
stage('parallel stage') {
parallel {
stage('parallel one') {
agent any
steps {
echo "parallel one"
testMethod()
echo "${CLASS_VAR}"
echo "${METHOD_VAR}"
echo "${GLOBAL_VAR}"
echo "${testMethodGlobalVar}"
script {
pipelineMethodOneGlobalVar = "pipelineMethodOneGlobalVar"
sh_output = sh returnStdout: true, script: 'pwd' //Declared global to access outside the script
}
echo "sh_output ${sh_output}"
}
}
stage('parallel two') {
agent any
steps {
echo "parallel two"
// pipelineGlobalVar = "new" //cannot introduce new variables here
// def pipelineMethodVar = "new" //cannot introduce new variables here
script { //new variable and reassigning needs scripted-pipeline
def pipelineMethodLocalVar = "new";
pipelineMethodLocalVar = "pipelineMethodLocalVar reassigned";
pipelineMethodGlobalVar = "new" //no def keyword
pipelineMethodGlobalVar = "pipelineMethodGlobalVar reassigned"
CLASS_VAR = "CLASS TWO"
METHOD_VAR = "METHOD TWO"
GLOBAL_VAR = "GLOBAL TWO"
}
// echo "${pipelineMethodLocalVar}" only script level scope, cannot be accessed here
echo "${pipelineMethodGlobalVar}"
echo "${pipelineMethodOneGlobalVar}"
testMethod()
}
}
}
}
stage('sequential') {
steps {
script {
echo "sequential"
}
}
}
}
}
Observations:
Six cases of variables declarations
a. Three types (with def, without def, with def and with #field) before/above pipeline
b. within scripted-pipeline (with def, without def) within pipeline
c. Local to a method (with def) outside pipeline
new variable declaration and reassigning needs scripted-pipeline within pipeline.
All the variable declared outside pipeline can be accessed between the stages
Variable with def keyword generally specific to a method, if it is declared inside script then will not be available outside of it. So need to declare global variable (without def) within script to access outside of script.
My jenkins file looks like below:
import groovy.json.*
def manifestFile = "C:\\manifest.yml"
node {
stage('Build') {
}
stage('Deploy') {
checkDeployStatus()
}
}
def boolean checkDeployStatus() {
echo "${manifestFile}"
return true
}
The exception that i am getting is below:
groovy.lang.MissingPropertyException: No such property: manifestFile for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
How do i access variables outside the node?
Groovy has a different kind of scoping at the script level. I can't ever keep it all sorted in my head. Without trying explain all the reasons for it (and probably not doing it justice), I can tell you that (as you have seen), the manifestFile variable is not in scope in that function. Just don't declare the manifestFile (i.e. don't put def in front of it). That will make it a "global" (not really, but for your purposes here) variable, then it should be accessible in the method call.
try this
import groovy.json.*
manifestFile = "C:\\manifest.yml"
node {
stage('Build') {
}
stage('Deploy') {
checkDeployStatus()
}
}
def boolean checkDeployStatus() {
echo "${manifestFile}"
return true
}
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)