Answers: <p>try this one</p>
  
  <pre><code>static void main(String[] args) { 
        // Initializing 2 variables 
        def x = 5; 
        def y = 10;
  
        //Performing addition of 2 operands 
        println(x+y);
   }
  </code></pre>
  
  <p>}</p>
  
  <p>check this <a href="https://www.tutorialspoint.com/groovy/groovy_arithmetic_operators.htm" rel="nofollow noreferrer">URL</a> </p>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-28T16:35:12.390Z
Answers: 2019-01-28T16:35:12.390Z
Answers: Ahmed Badawy
Answers: <p>Your example code has a typo: <code>b = &quot;second letter;</code> is missing the closing quote. Even if you fix that typo your code still won't work.</p>
  <p>If you print the <code>child</code> variable it will be <code>null</code>?</p>
  <p>That is because you are missing <code>return this</code> at the bottom of <code>child-groovy-file</code>.</p>
  <p>It's mentioned in the Jenkins <a href="https://www.jenkins.io/doc/pipeline/steps/workflow-cps/" rel="nofollow noreferrer">load</a> documentation:</p>
  <blockquote>
  <p>Where <code>pipeline.groovy</code> defines <code>functionA</code> and <code>functionB</code> functions (among
  others) before ending with <code>return this</code>;</p>
  </blockquote>
  <p>I fixed your code and ran this POC:</p>
  <p><a href="https://i.sstatic.net/uSmRr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uSmRr.png" alt="Example code" /></a></p>
  <p>This resulted in:</p>
  <p><a href="https://i.sstatic.net/wjeHy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wjeHy.png" alt="results of example code" /></a></p>
  <p>While loading scripts like this might have some use cases, they are not very common.  If you are looking to have reusable code you should use Jenkins <a href="https://www.jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">Shared Libraries</a> feature. If you want to see an example of what can be done with a shared-library see <a href="https://github.com/DontShaveTheYak/jenkins-std-lib" rel="nofollow noreferrer">The Jenkins Standard Library</a>, which can do things like run Github Actions! I also happen to be the author of that library.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-08-11T18:03:38.627Z
Answers: 2022-08-11T18:03:38.627Z
Answers: Levi
Answers: <p>It just could be that:</p>
  <pre><code>import groovy.transform.Field
  
  @Field public a = &quot;first letter&quot;
  @Field public b = &quot;second letter&quot;
  </code></pre>
  <p>works (I got hint from <a href="https://stackoverflow.com/questions/37800195/">https://stackoverflow.com/questions/37800195/</a>)</p>
  <p>When referencing (environment variables) I saw the format did not exactly
  work, but the following did:</p>
  <pre><code>@Field public c; c = 'prefix ' + VAR + ' suffix'
  </code></pre>
  <p>(probably &quot;prefix ${VAR} suffix&quot; worked but did not try that (either))</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-08-11T09:37:35.270Z
Answers: 2022-08-11T09:37:35.270Z
Answers: Tomi Ollila
Body: <p>I have a Jenkins parent groovy file which consumes the child groovy file</p>
  
  <p><strong>Parent grooy file:</strong></p>
  
  <pre><code>...
  def child = load 'child-groovy-file';
  child.execute();
  </code></pre>
  
  <p><strong>Child groovy file:</strong></p>
  
  <pre><code>a = "first letter";
  b = "second letter;
  def execute
  {
  echo "a is "+a;
  ...
  }
  </code></pre>
  
  <blockquote>
    <p>But this is not working. Unable to access variables, which are defined
    in Child Groovy file, when trying to access from Parent groovy file.
    When I create a node block and trying to consume directly the child
    groovy file, It is working.</p>
  </blockquote>
  
  <p>Is the Environment variables only the solution to access globally?</p>
  
Tags: <jenkins-pipeline><jenkinsfile><groovy><environment-variables>
Title: How to define global variables in groovy file, which I want to load and consume?
UserDisplayName: omkar sirra
ThreadText: # How to define global variables in groovy file, which I want to load and consume?
  
  (Asked by: username_0 on 2019-01-28)
  
  I have a Jenkins parent groovy file which consumes the child groovy file
  
  **Parent grooy file:**
  
  ```
  ...
  def child = load 'child-groovy-file';
  child.execute();
  
  ```
  
  **Child groovy file:**
  
  ```
  a = "first letter";
  b = "second letter;
  def execute
  {
  echo "a is "+a;
  ...
  }
  
  ```
  
  > But this is not working. Unable to access variables, which are defined
  > in Child Groovy file, when trying to access from Parent groovy file.
  > When I create a node block and trying to consume directly the child
  > groovy file, It is working.
  
  Is the Environment variables only the solution to access globally?
  
  **Tags:** jenkins-pipeline, jenkinsfile, groovy, environment-variables
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2019-01-28. Score: 0
  
  try this one
  
  ```
  static void main(String[] args) { 
        // Initializing 2 variables 
        def x = 5; 
        def y = 10;
  
        //Performing addition of 2 operands 
        println(x+y);
   }
  
  ```
  
  }
  
  check this [URL](https://www.tutorialspoint.com/groovy/groovy_arithmetic_operators.htm)
  
  
  ---
  
  ### Answer by username_2 on 2022-08-11. Score: 0
  
  Your example code has a typo: `b = "second letter;` is missing the closing quote. Even if you fix that typo your code still won't work.
  
  If you print the `child` variable it will be `null`?
  
  That is because you are missing `return this` at the bottom of `child-groovy-file`.
  
  It's mentioned in the Jenkins [load](https://www.jenkins.io/doc/pipeline/steps/workflow-cps/) documentation:
  
  > Where `pipeline.groovy` defines `functionA` and `functionB` functions (among
  > others) before ending with `return this`;
  
  I fixed your code and ran this POC:
  
  [![Example code](https://i.sstatic.net/uSmRr.png)](https://i.sstatic.net/uSmRr.png)
  
  This resulted in:
  
  [![results of example code](https://i.sstatic.net/wjeHy.png)](https://i.sstatic.net/wjeHy.png)
  
  While loading scripts like this might have some use cases, they are not very common. If you are looking to have reusable code you should use Jenkins [Shared Libraries](https://www.jenkins.io/doc/book/pipeline/shared-libraries/) feature. If you want to see an example of what can be done with a shared-library see [The Jenkins Standard Library](https://github.com/DontShaveTheYak/jenkins-std-lib), which can do things like run Github Actions! I also happen to be the author of that library.
  
  
  ---
  
  ### Answer by username_3 on 2022-08-11. Score: 0
  
  It just could be that:
  
  ```
  import groovy.transform.Field
  
  @Field public a = "first letter"
  @Field public b = "second letter"
  
  ```
  
  works (I got hint from <https://stackoverflow.com/questions/37800195/>)
  
  When referencing (environment variables) I saw the format did not exactly
  work, but the following did:
  
  ```
  @Field public c; c = 'prefix ' + VAR + ' suffix'
  
  ```
  
  (probably "prefix ${VAR} suffix" worked but did not try that (either))
  
  
  ---
  
  