Jenkins Groovy scripting handling variables in shared libraries

Viewed 70

I am trying to execute the similar code , where common.groovy,helper.groovy,test.groovy are part of shared library.

The issue happening is when executing the test.execute from common.groovy & helper.groovy , the field value for ENABLE_FLAG is different. Can this be made to set once and have same value from wherever called ? Kindly throw some light on the issue

    ---common.groovy-----
    
    
   def call(){
    node {
        
        stage('Preparation') { // for display purposes
        echo "Preparation"
        checkout scm
        test.setEnableFlag(true)
        test.execute()
        helper.performAction()
        }
        stage('Build') {
        echo "Build"
        }
        stage('Results') {
            echo "Results"
        }
    }
}


    --helper.groovy---
def performAction(){ 
    echo "Inside performAction"
    test.execute() 
}

------ test.Groovy------

import groovy.transform.Field

@Field ENABLE_FLAG = false

def setEnableFlag(value){
  echo "Inside setEnableFlag "
    ENABLE_FLAG = value
}

def execute(){
  echo "Inside execute"
    if(ENABLE_FLAG){
      echo "ENABLE_FLAG : ${ENABLE_FLAG}"
  } else {   
      echo "ENABLE_FLAG ELSE: ${ENABLE_FLAG}"
  }
}

    ---Jenkinsfile---

library "testsharedlib@main"

common()
0 Answers
Related