groovy 中的 Jenkins 关系运算符

Jenkins Relational operators in 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'
      }
  }
}
}

您只需将 string 参数转换为 int :

if (Integer.parseInt(params.COUNT) > 10)
{
   println ("Count > 10")
}

您完成管道:

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 ( Integer.parseInt(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'
      }
  }