5

I have a contants.groovy file as below

def testFilesList = 'test-package.xml'
def testdataFilesList = 'TestData.xml'
def gitId = '9ddfsfc4-fdfdf-sdsd-bd18-fgdgdgdf'

I have another groovy file that will be called in Jenkins pipeline job

def constants
node ('auto111') {
  stage("First Stage") {
    container('alpine') {
      script {
        constants = evaluate readTrusted('jenkins_pipeline/constants.groovy')
        def gitCredentialsId = constants."${gitId}"
      }
    }
  }
}

But constants."${gitId}" is says "cannot get gitID from null object". How do I get it?

Damien-Amen
  • 7,232
  • 12
  • 46
  • 75

1 Answers1

6

It's because they are local variables and cannot be referenced from outside. Use @Field to turn them into fields.

import groovy.transform.Field

@Field
def testFilesList = 'test-package.xml'
@Field
def testdataFilesList = 'TestData.xml'
@Field
def gitId = '9ddfsfc4-fdfdf-sdsd-bd18-fgdgdgdf'

return this;

Then in the main script you should load it using load step.

script {
    //make sure that file exists on this node
    checkout scm
    def constants = load 'jenkins_pipeline/constants.groovy'
    def gitCredentialsId = constants.gitId
} 

You can find more details about variable scope in this answer

Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62
  • I tried that. It still says `Cannot get property 'gitId' on null object`. The `constants` variable itself is null – Damien-Amen Jun 18 '18 at 18:16
  • @Damien-Amen you should use `load` step to load groovy files. I updated the answer. – Vitalii Vitrenko Jun 18 '18 at 19:13
  • I tried what you suggested. I was able to load the file successfully. But no matter what I did, It could not get the `Field` from the `constants.groovy`. When I just print the variable `constants` it prints the content of the file. I tried `constants."${testdataFilesList}"` `"${constants.testdataFilesList}"` `constants.testdataFilesList` `"${constants}".testdataFilesList` `"${constants}"."${testdataFilesList}"` `"${constants}"."${testdataFilesList}"` `"${constants}".'testdataFilesList'` `constants.'testdataFilesList' – Damien-Amen Jun 19 '18 at 01:02
  • @Damien-Amen did your `constants.groovy` file contain `return this` in the end? If not, try to add. – Vitalii Vitrenko Jun 19 '18 at 05:48
  • I tried adding `return this` in the `constants.groovy` as well. It still could not find the fields in that file. – Damien-Amen Jun 19 '18 at 11:45
  • Finally I got it working. I had to do `return this;` instead of `return this` (Missing semi colon. Don't know how that matters. But used this link as reference to get the clue https://jenkins.io/doc/pipeline/steps/workflow-cps/#load-evaluate-a-groovy-source-file-into-the-pipeline-script. Pls update your answer and I will accept it. – Damien-Amen Jun 19 '18 at 12:29
  • @Damien-Amen I updated the answer. This is however very strange why it didn't work without a semicolon. – Vitalii Vitrenko Jun 19 '18 at 12:33
  • Yeah. Also I used `evaluate readTrusted('jenkins_pipeline/constants.groovy')` to read the file using `lightweight` checkout so that I don't have to `checkout scm` explicitly. – Damien-Amen Jun 19 '18 at 12:39