Jenkinsfile Accessing variables from shared libraries

35 views
Skip to first unread message

Kernel Panic

unread,
Dec 16, 2020, 6:10:19 PM12/16/20
to Jenkins Users
Hello there
Maybe this is a very basic question, but am not being able to access a global variable
from a shared library from a Jenkinsfile

under vars I defined something like this

globalVars.groovy
class Vars () {

    def CustomMessage = "This is a new deployment"

}

from my Jenkinsfile

library identifier: 'globalVars@master', \
                     retriever: modernSCM([$class: 'GitSCMSource', \
                                credentialsId: 'tfsservice', \
                                remote: 'http://myrepo.com', \
                                traits: [gitBranchDiscovery()]])


pipeline {

stages {
    stage('Do Some Stuff') {
      steps {
        script {

           sh 'echo ${CustomMessage}'

          }
        }
      }
    }

}

But the above returns nothing, what am I doing wrong?
Thanks
Regards

Adrian Wyssmann

unread,
Dec 17, 2020, 2:25:49 AM12/17/20
to Jenkins Users
I see several problem
  1. using ' will not expand the parameters is your call should be sh "echo ${CustomMessage}"
  2. CustomMessage is declared within the class Vars(), so you cannot access it
You could implement a getter an then do something like this "echo ${globalVars.getCustomMessage()}"

Maybe others have better ideas...?

Ven H

unread,
Dec 17, 2020, 3:45:11 AM12/17/20
to jenkins...@googlegroups.com
In my case, I do the following. I have a GlobalVars.groovy file with below content.

#!/usr/bin/env groovy
package com.<companyName>.<ProductName>;

public class GlobalVars {
   static String myVar = ""
}
Then in my Shared Library class, I use it like below.
GlobalVars.myVar = ""
Try it like this and check. Here, I have declared the class as Public and the variable as static. That's the only difference.

Regards,
Venkatesh


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/099e3aee-dffc-459d-8eb5-d64204effb58n%40googlegroups.com.
Message has been deleted

Kernel Panic

unread,
Dec 19, 2020, 10:56:08 PM12/19/20
to Jenkins Users
Hello

Defining a global class worked for me, at least did what I wanted to do,
Thanks you very much for your time and support
Regards

Kernel Panic

unread,
Dec 20, 2020, 12:07:56 AM12/20/20
to Jenkins Users
Hello

Is there a way to access those variables from as sh command from the script section?
for example this works:

script {
                      echo GlobalVariables.MyVariable

                    }

but the same with sh does not, that just echos GlobalVariables.MyVariable and not the variable content.
script {

              sh  ' echo GlobalVariables.MyVariable'

                    }

Thanks
Regards


El jueves, 17 de diciembre de 2020 a las 5:45:11 UTC-3, venh...@gmail.com escribió:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages