I only looked at the pipeline cps-global-lib feature after reading your
e-mail. It is some pretty advanced stuff. :)
I am not a groovy expert, but I will try to explain to the best of my
understanding and hopefully it will lead you on the right path.
When you have a Jenkinsfile or some arbitrary myscript.groovy which is
evaluated by the pipeline plugin, at runtime a class is dynamically
generated called *WorkflowScript*. If your script throws an exception
which is not caught, you will see a stacktrace line like:
at WorkflowScript.run(WorkflowScript:4)
that gives you a hint that the error occurred on line 4 of your script.
WorkflowScript is a derived class of CpsScript (
https://github.com/jenkinsci/workflow-plugin/blob/master/cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScript.java
). If you keep looking through the inheritance hierarchy,
you will see that you are dealing with
http://docs.groovy-lang.org/next/html/gapi/groovy/lang/Script.html.
At this point it is worth reading about Groovy Shell:
http://www.groovy-lang.org/groovysh.html
I believe the problem you are hitting with respect to global variables
is the same as this:
http://stackoverflow.com/questions/6305910/how-do-i-create-and-access-the-global-variables-in-groovy
Under the covers, your pipeline script is being converted into
a Java class. As per the stackoverflow recommendation,
you should prefix your "global variables" with @Field.
So you would do something like this in your globals.groovy file:
// vars/globals.groovy
import groovy.transform.Field
@FieldString beginJobEmailBody = "The ${env.JOB_NAME} has begun"
@FieldString emailDevOpsTeam = "[email protected]"
/*** Sends an email to the team to notify of a build has begun*/
def beginBuildNotificationTestEmail() { //mail body:
"${this.beginJobEmailBody} " + paramMap.STAGE + " " +
paramMap.ENVIRONMENT, //subject: "${this.beginJobEmailSubject}" ,
//to: "${this.emailDevOpsTeam}"
echo "beginJobEmailBody: ${beginJobEmailBody}"
echo "emailDevOpsTeam: ${emailDevOpsTeam}"
}
and this in your Jenkinsfile
node {
globals.beginBuildNotificationTestEmail()}
I'm new to groovy, so if there is a better way to do it, let me know.
However, this example does work.
--
Craig
On Wed, Feb 3, 2016 at 3:45 PM, Tom Kierzkowski <
[email protected]> wrote:
I'm wondering how to declare these properly within the globals.groovy
> script. Here are some examples of what I've tried:
>
> beginJobEmailBody = "The ${env.JOB_NAME} job has begin on"
> def beginJobEmailBody = "The ${env.JOB_NAME} job has begin on"
> GString this.beginJobEmailBody = "The ${env.JOB_NAME} job has begin on";
>
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/CAG%3DrPVcDDLbMCUa8WtoQ2RUyTqB9%3DLvxFpbfR8yCRQ50nP6%2BqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.