Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upUpdated to correctly describe class behavior #611
Conversation
dc5fd00
to
dc75f6e
|
No. The whole point of |
|
Hey @jglick i was trying this example verbatim with the following Jenkinsfile and it was erroring out. @bitwiseman was trying to help me out with it. So far the only thing I have been able to get working was a step |
|
@jglick - I made it upper-case because it's a class name and it wasn't automatically creating an instance. The class example (renamed acmeB) under "Defining Global Variable" produces this error when I run it as the first item in my Jenkinsfile on my Jenkins:
(@abayer - this might interest you too)
I get this error:
|
|
@bitwiseman your latest example runs for me, but then I get the error the |
|
For example the following jenkinsfile:
Produces this error:
|
7bd4cc6
to
ef97a32
|
@zbintliff @jglick @rtyler |
|
I also recommend rewriting #defining-a-more-structured-dsl. In practice it is just really hard for people to understand how it works and fix problems that arise. Better to take plain old function calls with a |
| Avoid defining global variables with methods that interact or preserve state. | ||
| Use a static class or instantiate a local variable of a class instead. | ||
| The example below shows a global variable `customer` which stores a `name` value. |
This comment has been minimized.
This comment has been minimized.
jglick
Sep 13, 2017
Contributor
Wait, so you warn people not to do this, then show them how to do it?
|
@bitwiseman would you be able to resurrect this one, please. |
jenkinsadmin
commented
Nov 17, 2017
|
The context from the Jenkins Pipeline run is:
|
jenkinsadmin
commented
Nov 19, 2017
|
The context from the Jenkins Pipeline run is:
|
jenkinsadmin
commented
Nov 21, 2017
|
The context from the Jenkins Pipeline run is:
|
jenkinsadmin
commented
Nov 23, 2017
|
The context from the Jenkins Pipeline run is:
|
jenkinsadmin
commented
Nov 27, 2017
|
The context from the Jenkins Pipeline run is:
|
jenkinsadmin
commented
Nov 27, 2017
|
The context from the Jenkins Pipeline run is:
|
jenkinsadmin
commented
Nov 28, 2017
|
The context from the Jenkins Pipeline run is:
|
3f60b86
to
13ed7a5
|
The
|
| Use a static class or instantiate a local variable of a class instead. | ||
| ==== | ||
|
|
||
| === Defining custom steps |
This comment has been minimized.
This comment has been minimized.
jglick
Nov 28, 2017
Contributor
I would not advise referring to these as “steps” BTW. They are just functions that might look kind of like steps (or might not).
| `acme` object: | ||
| Declarative Pipeline does not allow global variable usage outside of a `script` | ||
| directive | ||
| (link:https://issues.jenkins-ci.org/browse/JENKINS-42360[JENKINS-42360]). |
This comment has been minimized.
This comment has been minimized.
jglick
Nov 28, 2017
Contributor
@abayer correct me if I am wrong, but I think so long as you declare the method as call, you can call it as if it were a step.
This comment has been minimized.
This comment has been minimized.
bitwiseman
Nov 28, 2017
Author
Contributor
@jglick I understand that from one perspective everything defined under vars is a "global variable. However, from a user perspective, "global variables" defined with thecall-method do not behave the same way as "global variables" defined with other method names.
Perhaps we could agree to call them "global variables" and "global functions"?
This comment has been minimized.
This comment has been minimized.
jglick
Jan 3, 2018
Contributor
Any variable referring to an object with a call method can be called like a function. This is just Groovy sugar. Technically Groovy has no “functions” at all. It has methods, etc.
| acme.name = 'Alice' | ||
| echo acme.name /* prints: 'Alice' */ | ||
| acme.caution 'The queen is angry!' /* prints: 'Hello, Alice. CAUTION: The queen is angry!' */ | ||
| @Library('utils') _ |
This comment has been minimized.
This comment has been minimized.
jglick
Nov 28, 2017
Contributor
There is a dedicated libraries section in Declarative. @abayer can assist.
| [WARNING] | ||
| ==== | ||
| Avoid defining global variables with methods that interact or preserve state. | ||
| Use a static class or instantiate a local variable of a class instead. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bitwiseman
Nov 28, 2017
Author
Contributor
@jglick You objected to providing an example, but you also feel the text is to vague. Which would you prefer?
|
I think there are some criticisms here which apply to content outside of the scope of this pull request specifically. I suggest taking a discussion to either jenkinsci-docs@ or jenkinsci-dev to discuss the nomenclature challenges between "Global Variables" as they're presented in the Snippet Generator, compared to "global variables" as they're referenced in Shared Libraries, in addition to some other thiings. |
bitwiseman commentedFeb 8, 2017
@jglick @rtyler
From discussion on IRC and testing, it looks like this is the current behavior of
classdeclarations.Is this correct behavior or should the class declaration automatically instantiate a new global variable?