Jenkins 管道 - 从 bat 命令到标签的变量,输出显示完整的命令提示符
Jenkins pipeline - variable from a bat command to label, output show full command prompt
詹金斯版本。 2.176.1 声明式管道,试图从一个阶段获取bat 命令输出以设置另一个阶段的代理标签,
当从 bat 命令(“echo %computername%”)设置变量时,我可以从步骤中很好地读取变量(我得到:V-COMP-S6-CI3,这是我需要的。)
我无法在代理标签中设置它。当我尝试在另一个阶段设置 agent { label "${winCompName}" } 时,我得到: 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’
我似乎得到了完整的命令提示符 + 输出,而不仅仅是标准输出,
(“c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3”而不仅仅是“V-COMP-S6-CI3”)
我怎样才能只得到代理标签的输出?
我的管道和输出:
#!/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
输出(截断):
[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’
添加 .readLines().drop(1).join(" ")(来自 https://issues.jenkins-ci.org/browse/JENKINS-44569 的回答)有效,但我仍然想知道是否有更简单的方法:
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}"
}
}
詹金斯版本。 2.176.1 声明式管道,试图从一个阶段获取bat 命令输出以设置另一个阶段的代理标签,
当从 bat 命令(“echo %computername%”)设置变量时,我可以从步骤中很好地读取变量(我得到:V-COMP-S6-CI3,这是我需要的。)
我无法在代理标签中设置它。当我尝试在另一个阶段设置 agent { label "${winCompName}" } 时,我得到: 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’
我似乎得到了完整的命令提示符 + 输出,而不仅仅是标准输出, (“c:\jenkins\build\workspace\nashpaz@2>echo V-COMP-S6-CI3”而不仅仅是“V-COMP-S6-CI3”)
我怎样才能只得到代理标签的输出?
我的管道和输出:
#!/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
输出(截断):
[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’
添加 .readLines().drop(1).join(" ")(来自 https://issues.jenkins-ci.org/browse/JENKINS-44569 的回答)有效,但我仍然想知道是否有更简单的方法:
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}"
}
}