1

I'm trying to get information from a groovy script located under vars called from a shared library Jenkins class but gets an error. some info:

  • Need - global configuration file. similar to Manage Jenkins -> Configure System -> Environment variables

  • Goal - to be able to get global values inside shared library without sending parameters from Jenkinsfile.

  • My solution - I tried to use "vars/script.groovy" which works for Jenkinsfile but not inside shared library.

file structure

.
├── src
│   └── org
│       └── jenkins
│            └──shared_library.groovy 
│── vars
│      └── globals.groovy
│
│── jenkinsfile.groovy

vars/globals.groovy

def my_global() {
return 'my_global_name'
}

shared_library class

package src.org.jenkins

class shared_library implements Serializable {
private steps

shared_library(steps) {
    this.steps = steps
}

def some_func(){
  println globals.my_global
}

jenkinsfile

@Library 'shared_library'
import org.jenkins.shared_library

my_shared_library = new shared_library(steps)

node(){
  stage('my_first_stage'){
    println globals.my_global
  }
  stage('my_second_stage'){
    println shared_library.some_func()
  }
}

so, I can see the value for the first print in pipeline, but for the second I get:

No such property: globals for class: src.org.jenkins.shared_library

Saikat
  • 14,222
  • 20
  • 104
  • 125
user1789357
  • 93
  • 2
  • 10

2 Answers2

1

You need to use the steps object as well to access the globals:

def some_func(){
    println steps.globals.my_global()
}

Taking your example that would become

shared_library class

package src.org.jenkins

class shared_library implements Serializable {
private steps

shared_library(steps) {
    this.steps = steps
}

def some_func(){
  println steps.globals.my_global()
}

Edit: Just saw your Jenkinsfile has a typo as well. Need to use the shared library object instead of the class in 'my_second_stage':

Jenkinsfile

@Library('shared_library')
import org.jenkins.shared_library

my_shared_library = new shared_library(steps)

node(){
  stage('my_first_stage'){
    println globals.my_global()
  }
  stage('my_second_stage'){
    println my_shared_library.some_func()
  }
}
Joerg S
  • 4,730
  • 3
  • 24
  • 43
  • I tried but it did not work in a sharedlibrary groovy file. can you please add an example? – user1789357 Jul 18 '18 at 09:33
  • Added the full example. What doesn't work? Do you get an error message? – Joerg S Jul 18 '18 at 20:03
  • I follow your example but keep getting "groovy.lang.MissingPropertyException: No such property: globals for class: org.jenkinsci.plugins.workflow.cps.DSL" I think you must have "@Library ('xxx')" in your class to be able to get info from vars/globals.groovy. – user1789357 Jul 23 '18 at 08:41
  • `@Library('shared_library')` (added braces) will be sufficient if you have `globals.groovy` in vars. How did you configure your global shared library? Is everything checked in? Is the repository configuration correct? – Joerg S Jul 23 '18 at 20:51
  • exactly. but I don't have @Library('shared_library') in my shared_library class. my example is exactly as yours. I created a simple new project for it as well as adding my "test" shared_library class to Jenkins config. I am able to use the globals from the jenkins_file but got the error once calling a func which emplacements "println steps.globals.my_global" as you described... thank! – user1789357 Jul 24 '18 at 06:51
  • Just realized that there was another typo in your example which I silently copied. As `my_global` is a method and no property you of course need to add parenthesis `()` to call that method. Otherwise you'll get a missing property error... – Joerg S Jul 24 '18 at 21:47
  • You defined `some_func` in shared_library class the same as in `vars/globals.groovy`. Is it possible to call `vars/globals.groovy` from shared_library class? – Maxim Suslov May 22 '20 at 11:45
  • In the `shared_library` class there's the `steps` member which fulfills solely one purpose: To access any steps as defined by Jenkins, some plugins or some loaded library. See also: https://www.jenkins.io/doc/book/pipeline/shared-libraries/ – Joerg S May 26 '20 at 13:48
0

If you are comfortable with defining the value in .properties or .json files you can use 'resource' folder

sharedlibrary/resource/global.properties

In your pipeline script or var/script.groovy

Use the libraryResource method

globalPropertyContent = libraryResource 'global.properties'

access the property values like:

globalPropertyContent.PROJECT_NAME
Samy
  • 632
  • 4
  • 14
  • I tried but it did not work in a sharedlibrary groovy file - " A resources directory allows the libraryResource step to be used from an external library to load associated non-Groovy files. Currently this feature is not supported for internal libraries." https://jenkins.io/doc/book/pipeline/shared-libraries/ – user1789357 Jul 18 '18 at 10:41