1

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 {
       }
   }
}
xren
  • 1,381
  • 5
  • 14
  • 29

1 Answers1

0
  1. enable the library in 'Global Pipeline Libraries' (i add the library 'pipeline-shared-lib')

  2. create a shared-library (with the necessary structure)

src/net/kukinet/Utils.groovy

package net.kukinet;

def myVar = 1
def sayHello() {
    print ('hello')
}
  1. 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()
    }
}
chenchuk
  • 5,324
  • 4
  • 34
  • 41
  • I already did that. Now, in sayHello, I want to use docker or other third-party shared library variables, how should I do that ? – xren Aug 18 '17 at 22:57