-1

I have an issue where the the prompt is allowing user to pick the params value based on what is loaded into the variables. The user can select the value in the variables , but the value of the params is not returning. The echo is blank and also inside the node it is not returning the params value.

+ echo

    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // dir
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    java.lang.NullPointerException: Cannot invoke method $() on null object
        at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)

Script:

#!/usr/bin/env groovy      
 stage('Connect Primary') {     
     node("Primary") {       
    script {
    
    GET_LISTSTANDBY= sh (script: "sudo cat /pathtofile/samplestandby.txt", returnStdout: true).trim()
    println "$GET_LISTSTANDBY"
    }
    
        
        stage('Connect Primary DB Server') { 
        
                    node("nodename2") {
             
        sh """
        
            sudo su - postgres -c  'repmgr cluster show | grep -i "standby"  | sed 's/standby.*//'  | sed -r 's/^.{4}//'  | cut -d "|" -f 2 | sed 's/^[[:space:]]*//'  > samplestandby.txt'
        samplestandby=`sudo cat /pathtofile/samplestandby.txt | sed 's/ //g'`
        echo "\${samplestandby}"
        sudo cp -R /pathtofile/samplestandby.txt  ${env.WORKSPACE}/dir-switch
        """.stripIndent()
        
        script {
        
        GET_samplestandby= sh (script: "sudo cat /pathtofile/samplestandby.txt", returnStdout: true).trim()
        println "$GET_samplestandby"
        }
        
                        }
               }
                stage('Prompt to select Standby') {
                    
                        script {
                            def nodechosen = input message: 'Choose', ok: 'Next',
                                            parameters: [choice(name: 'standbynode', choices: "${GET_LISTSTANDBY}", description: 'Select the option')]
                                            
                            node(nodechosen) {
                                echo "Running in Selected node for the choice prompt"
                            }
                           
                        }             
                } 

 
user5820327
  • 181
  • 1
  • 2
  • 15

1 Answers1

0

Use ${WORKSPACE} Jenkins environment variable in your getNodeNames() function instead of current directory.

fflores
  • 192
  • 1
  • 6
  • There is already an answer on how to get Jenkins environment variables in Groovy script https://stackoverflow.com/questions/21236268/access-to-build-environment-variables-from-a-groovy-script-in-a-jenkins-build-st. – fflores Aug 24 '22 at 09:42
  • I have done that again now. it does not work. I am trying to read file from a workspace here – user5820327 Aug 24 '22 at 09:49