如何根据 Jenkins 管道中的参数有条件地 运行 下游作业?
How to run downstream job conditionally based on a parameter in Jenkins pipeline?
接着我的问题
我想要调用下游项目,但前提是布尔参数设置为 true。这可能吗?我的管道如下所示:
node {
try {
echo "ConfigFilePath: ${ConfigFilePath}"
echo "Delete VM on Successful Build: ${DeleteOnSuccess}"
stage('checkout') {
deleteDir()
git 'http://my.git.lab/repo.git'
}
stage('deploy') {
bat 'powershell -nologo -file BuildMyVM.ps1 -ConfigFilePath "%ConfigFilePath%" -Verbose'
}
}
stage('test') {
// functional tests go here
}
}
catch (e) {
// exception code
} finally {
// finally code
}
} //node
stage('delete') {
if(DeleteOnSuccess)
{
bat 'SET /p VM_NAME=<DeleteVM.txt'
echo "Deleting VM_NAME: %VM_NAME%"
def job = build job: 'remove-vm', parameters: [[$class: 'StringParameterValue', name: 'VM_NAME', value: '${VM_NAME}']]
}
}
我在删除阶段遇到这个错误
Required context class hudson.FilePath is missing.
Perhaps you forgot to surround the code with a step that provides this, such as: node
如果我将上面的内容包装在一个节点中,那么参数值就会丢失。如果我将删除阶段放在主节点中,那么我会占用两个执行程序,我试图避免这种情况,因为它会导致一些死锁情况。
问题是脚本的 运行ning 实际上需要一个节点到 运行,所以在你的情况下,错误的原因是你试图 运行在 node
上下文
之外的 bat
命令
node {
...
}
stage('delete') {
if(DeleteOnSuccess)
{
bat 'SET /p VM_NAME=<DeleteVM.txt' // <- this is actually causing the error
echo "Deleting VM_NAME: %VM_NAME%"
def job = build job: 'remove-vm', parameters: [[$class: 'StringParameterValue', name: 'VM_NAME', value: '${VM_NAME}']]
}
}
您可以通过将此部分也包含在节点中来解决此问题,方法是将其放入第一个 node
或添加一个新节点,具体取决于您的需要
除此之外,如果 DeleteOnSuccess
变量是构建参数,它将是一个字符串。我不确定,但我认为这是因为它是作为环境变量注入的,它也是字符串(即使它是 BooleanParameter 类型。我想这只是一个 UI 的东西,所以它会显示为复选框)。
您可以通过回显 DeleteOnSuccess.class
来检查。这会告诉你它的 class
if(DeleteOnSuccess) { ... }
将始终 运行 条件块。您可以通过使用 toBoolean()
扩展方法将其转换为 bool
或像您所做的那样对照字符串 true
: DeleteOnSuccess == "true"
检查来解决此问题。
扩展方法的优点是它还允许值 "1"
和 "True"
作为 true
接着我的问题
我想要调用下游项目,但前提是布尔参数设置为 true。这可能吗?我的管道如下所示:
node {
try {
echo "ConfigFilePath: ${ConfigFilePath}"
echo "Delete VM on Successful Build: ${DeleteOnSuccess}"
stage('checkout') {
deleteDir()
git 'http://my.git.lab/repo.git'
}
stage('deploy') {
bat 'powershell -nologo -file BuildMyVM.ps1 -ConfigFilePath "%ConfigFilePath%" -Verbose'
}
}
stage('test') {
// functional tests go here
}
}
catch (e) {
// exception code
} finally {
// finally code
}
} //node
stage('delete') {
if(DeleteOnSuccess)
{
bat 'SET /p VM_NAME=<DeleteVM.txt'
echo "Deleting VM_NAME: %VM_NAME%"
def job = build job: 'remove-vm', parameters: [[$class: 'StringParameterValue', name: 'VM_NAME', value: '${VM_NAME}']]
}
}
我在删除阶段遇到这个错误
Required context class hudson.FilePath is missing.
Perhaps you forgot to surround the code with a step that provides this, such as: node
如果我将上面的内容包装在一个节点中,那么参数值就会丢失。如果我将删除阶段放在主节点中,那么我会占用两个执行程序,我试图避免这种情况,因为它会导致一些死锁情况。
问题是脚本的 运行ning 实际上需要一个节点到 运行,所以在你的情况下,错误的原因是你试图 运行在 node
上下文
bat
命令
node {
...
}
stage('delete') {
if(DeleteOnSuccess)
{
bat 'SET /p VM_NAME=<DeleteVM.txt' // <- this is actually causing the error
echo "Deleting VM_NAME: %VM_NAME%"
def job = build job: 'remove-vm', parameters: [[$class: 'StringParameterValue', name: 'VM_NAME', value: '${VM_NAME}']]
}
}
您可以通过将此部分也包含在节点中来解决此问题,方法是将其放入第一个 node
或添加一个新节点,具体取决于您的需要
除此之外,如果 DeleteOnSuccess
变量是构建参数,它将是一个字符串。我不确定,但我认为这是因为它是作为环境变量注入的,它也是字符串(即使它是 BooleanParameter 类型。我想这只是一个 UI 的东西,所以它会显示为复选框)。
您可以通过回显 DeleteOnSuccess.class
来检查。这会告诉你它的 class
if(DeleteOnSuccess) { ... }
将始终 运行 条件块。您可以通过使用 toBoolean()
扩展方法将其转换为 bool
或像您所做的那样对照字符串 true
: DeleteOnSuccess == "true"
检查来解决此问题。
扩展方法的优点是它还允许值 "1"
和 "True"
作为 true