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
-- 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/9cea34b1-38b7-41fb-9e1f-fd6af0db38d3n%40googlegroups.com. |
I see several problem
You could implement a getter an then do something like this "echo ${globalVars.getCustomMessage()}" Maybe others have better ideas...? On Thursday, December 17, 2020 at 12:10:19 AM UTC+1 [hidden email] wrote:
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/099e3aee-dffc-459d-8eb5-d64204effb58n%40googlegroups.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 On Thu, Dec 17, 2020 at 12:56 PM Adrian Wyssmann <[hidden email]> wrote: I see several problem 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPp28eojNNe9xEFtD8rNhxXnY5Z118zrhGLJRVsqSqaL4qiHFA%40mail.gmail.com. |
Hi, I think you should not use global variables at all : only global functions. Trust me : you don't need global variables :) To use global functions : # /vars/myGlobFunc.groovy def call() { // .... } // This way you can call you function directly in "steps" if you are in declarative pipeline, from anywhere in scripted // This function has access to other global methods # Or this way, in /vars/myClass.groovy : def myGlobFunc.groovy { // ... } return this // myClass.myGlobFunc() // This function has also access to other global methods because of this special class declaration // But you cannot call this from steps in declarative, you must be inside script But if you really want global variables, I would recommand to use a single object containing what you need and give this object to people who need it, for example a class or a map : def myVars = [:] myVars['myGlobVar'] = 'hello' pipeline { // ... aFunction(myVars) } # But if you really really want global, just remove the "def" : myVars = [:] myVars['myGlobVar'] = 'hello' pipeline { // ... aFunction() // myVars can be read in this function } // But I thihnk this is a bad code Another way is to use static field of class, but it is also a bad pratice to use static field if you want them mutable. You could also just try to use the existing global var "env". This is a clean solution, but you must only use text values. env['myGlobVar'] = "hello" echo env.myGlobVar Good luck On Thu, Dec 17, 2020 at 9:45 AM Ven H <[hidden email]> wrote:
Yannick LACAUTE Consultant indépendant 39 rue de Wattignies, 75012 Paris Tel : 06 16 85 95 76 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAG1W_Axgd1GdUuD-6J4xFmEqQSMC_XU_ekaXAuAF8AvfXfS6Cg%40mail.gmail.com. |
In reply to this post by Ven H
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 El jueves, 17 de diciembre de 2020 a las 5:45:11 UTC-3, [hidden email] escribió:
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/eea95ea1-71e8-4fb3-9d7e-a6035f0ee0e8n%40googlegroups.com. |
In reply to this post by Ven H
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, [hidden email] escribió:
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/9db63ce0-9c9d-4683-b58d-a5cd1a4f8355n%40googlegroups.com. |
Hi,
First, if you want "echo", you could just use echo without the sh : echo "hello" Then, if you want to print a variable in the echo, use this syntax : echo "hello ${myGroovyVariable}" You must use double quote, if you use single quote the echo will print *exactly* what you have written Le dimanche 20 décembre 2020 à 06:07:56 UTC+1, [hidden email] a écrit :
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/797962ff-f126-4a16-a9d9-416b838d6107n%40googlegroups.com. |
Free forum by Nabble | Edit this page |