0
votes

Jenkins ver. 2.176.1 declarative pipeline, trying to get a bat command output from one stage to set the agent label for another stage,

When setting a variable from a bat command ("echo %computername%"), I can read the variable quite nicely from steps (I get: V-COMP-S6-CI3 which is what I need.)

I can't manage to set it in an agent label. When I try setting agent { label "${winCompName}" } in another stage, I get: There are no nodes with the label ‘c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3 12:34:29 V-COMP-S6-CI3’

I seem to get a full command prompt + output instead of just the standard output, ("c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3" instead of just "V-COMP-S6-CI3")

How can I get just the output to the agent label?

my pipeline and output:

    #!/usr/bin/env groovy


import org.apache.commons.lang.StringUtils
currentBuild.description = "${params.EMAIL}"
currentBuild.displayName = "${params.BRANCH_NAME}"

def call(String filter_string, int occurrence) {
    def logs = currentBuild.rawBuild.getLog(10000).join('\n')
    int count = StringUtils.countMatches(logs, filter_string);
    if (count > occurrence -1) {
        currentBuild.result='UNSTABLE'
    }
}


pipeline {
    agent {
        label "winServerGroup"
    }
    options { 
       timestamps()
       timeout(time: 2, unit: 'HOURS')   // timeout on whole pipeline job
  
    }

    stages {
        stage ('Print Environment') {            
            steps {
                echo "Print Environment" 
                bat '''
                echo userprofile=%USERPROFILE%
                SET
                '''
            }
        }   
        stage ('1. set comutername to var') {
            agent { label 'winServerGroup' }
            steps {
                script {
                    winCompName = bat(script: "echo %COMPUTERNAME%", returnStdout: true).trim()
                }
                echo "${winCompName}"
            }       
        }
        stage ('1.5.print var') {
            agent { label 'winServerGroup' }
            steps {
                echo "${winCompName}"
            }
        }
        stage ('2. run on comutername') {
            agent { label "${winCompName}" }
            steps {
                bat """echo %COMPUTERNAME%"""
                echo "${winCompName}"
            }
        }

    } // end of stages
} // end of pipeline

output (truncted):

    [Pipeline] { (1. set comutername to var)
[Pipeline] node (hide)
...
[Pipeline] echo
12:34:12  c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3 
12:34:12  V-COMP-S6-CI3
[Pipeline] }
...
[Pipeline] stage
[Pipeline] { (1.5.print var)
[Pipeline] node
12:34:12  Running on v-comp-s6-ci3 in c:\jenkins\build\workspace\nashpaz@2
[Pipeline] {
...
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
12:34:13  c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3 
12:34:13  V-COMP-S6-CI3
[Pipeline] }
....
[Pipeline] stage
[Pipeline] { (2. run on comutername)
[Pipeline] node
12:34:29  Still waiting to schedule task
12:34:29  There are no nodes with the label ‘c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3 
12:34:29  V-COMP-S6-CI3’
1

1 Answers

0
votes

adding .readLines().drop(1).join(" ") (answer from https://issues.jenkins-ci.org/browse/JENKINS-44569 ) works, but I still wonder if there isn't a simpler way:

stage ('1. set comutername to var') {
        agent { label 'winServerGroup' }
        steps {
            script {
                winCompName = bat(script: "echo %COMPUTERNAME%", returnStdout: true).trim().readLines().drop(1).join(" ")
            }
            echo "${winCompName}"
        }       
    }

    stage ('2. run on comutername') {
        agent { label "${winCompName}" }
        steps {
            bat """echo %COMPUTERNAME%"""
            echo "${winCompName}"
        }
    }