jenkins - Jenkins groovy 中的关系运算符
问题描述
我正在尝试在 Jenkins 中进行数值比较并基于此执行操作。COUNT
是一个字符串类型参数。但它给出了以下错误。
WorkflowScript: 24: Expected a step @ line 24, column 14.
if ( params.COUNT > 10 ) {
^
1 error
pipeline {
agent {
label "${node_label}"
}
parameters {
string(defaultValue: "1", description: "Number of VM's to be Added", name: "COUNT")
}
stages {
stage('Parameter Validation') {
steps {
if ( params.COUNT > 10 ) {
error("Instance count should be less than 10 and it is ${COUNT}")
}
}
}
stage('Clone Latest Repo') {
steps {
echo "Pull latest code"
build 'gitpull'
}
}
我尝试了不同的方法,但没有运气
steps {
script {
sh '''#!/bin/bash
if [[ "${COUNT}" -gt "10" ]]; then
error("Instance count should be less than 10")
fi
'''
}
}
解决方案
我找到了一种将字符串变量类型转换为 INT 的方法def intValue = COUNT as int
。此解决方案按预期工作
def intValue = COUNT as int
pipeline {
stages {
stage('Parameter Validation') {
steps {
script {
if ( intValue > 10 ) {
error("Instance count should be less than 10 and it is ${COUNT}")
}
}
}
}
stage('Clone Latest Repo') {
steps {
echo "Pull latest code"
build 'gitpull'
}
}
}
}
推荐阅读
- r - R代码中的无效下标类型“列表”错误
- html - CSS 效果在反应应用程序中不起作用
- docker - 使用 Docker for windows 将 windows 驱动器卷挂载到 Linux 容器中
- reactjs - 将我的 React Web App 发布到 Apache 生产服务器后,路由不起作用
- java - 如何在对象数组列表中找到对象的一个元素?
- android - 我的安卓应用程序崩溃了。下面是堆栈跟踪
- r - 如何在闪亮的应用程序中显示绘图可视化
- python - 用于处理大整数的多个数字数组
- ios - 如何将处理程序存储在目标 c 模型类中?
- excel - SUMIFS 应同时考虑空白值和非空白值。使用单一公式