bigskull
bigskull

Reputation: 809

How to access variables outside stages in Jenkins file Groovy function?

My jenkins file looks like below:

import groovy.json.*
def manifestFile = "C:\\manifest.yml"

node {
  stage('Build') { 

  }
  stage('Deploy') { 
    checkDeployStatus()
  } 
}

def boolean checkDeployStatus() {
  echo "${manifestFile}"
  return true
}

The exception that i am getting is below:

groovy.lang.MissingPropertyException: No such property: manifestFile for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)

How do i access variables outside the node?

Upvotes: 10

Views: 10066

Answers (2)

Nirmal
Nirmal

Reputation: 181

try this

import groovy.json.*
manifestFile = "C:\\manifest.yml"

node {
  stage('Build') { 

  }
  stage('Deploy') { 
    checkDeployStatus()
  } 
}

def boolean checkDeployStatus() {
  echo "${manifestFile}"
  return true
}

Upvotes: 7

Rob Hales
Rob Hales

Reputation: 5321

Groovy has a different kind of scoping at the script level. I can't ever keep it all sorted in my head. Without trying explain all the reasons for it (and probably not doing it justice), I can tell you that (as you have seen), the manifestFile variable is not in scope in that function. Just don't declare the manifestFile (i.e. don't put def in front of it). That will make it a "global" (not really, but for your purposes here) variable, then it should be accessible in the method call.

Upvotes: 14

Related Questions