groovy if语句与变量中的正则表达式 [英] groovy if statement with regex in variable
问题描述
我想删除以AAA开头的Jenkins作业列表(例如)
当我使用显式字符串进行操作时,它工作正常
if(item.name ==〜/ AAA。* /){
item.delete()
}
但是当我尝试从Jenkins构建属性中获取正则表达式时,我失败了,并且if没有被调用。
import hudson.model。*
import hudson.node_monitors。*
import hudson.slaves。 *
import java.util.concurrent。*
jenkins = Hudson.instance
def thr = Thread.currentThread()
def build = thr?.executable
String REGEX = build.environment.get(Include_Regex)
printlnREGEX是+ REGEX
(jenkins.items中的项目){
println(\ t $ {item.name});
println(\ t $ {item.name.getClass()});
if(item.name ==〜REGEX){
item.delete()
println(TEST);
}
}
当我运行它时,所有作业的列表:
在工作区中建立/ var / lib / jenkins / jobs / Bulk_Delete_Job / workspace
REGEX是/AAA.*/
AAA_test_1
类java.lang.String
AAA_test_2
类java.lang.String
AAA_test_3
class java.lang。 String
Bulk_Delete_Job
class java.lang.String
ConfiguredJob
class java.lang.String
JobZ
class java.lang.String
TEST_SCM
class java.lang.String
但是没有东西被删除/没有TEST打印 - >如果是从来没有真正的....
需要援助,谢谢!
字符串不应包含斜线 - 使用AAA。*而不是/ AAA。* /。
I want to delete list of Jenkins jobs that begins with AAA (for example)
When i do it with explicit string it works fine
if (item.name ==~ /AAA.*/){
item.delete()
}
but when i try to get the regex from a Jenkins Build property, i fail and the if is not called...
import hudson.model.*
import hudson.node_monitors.*
import hudson.slaves.*
import java.util.concurrent.*
jenkins = Hudson.instance
def thr = Thread.currentThread()
def build = thr?.executable
String REGEX = build.environment.get("Include_Regex")
println "REGEX is " + REGEX
for (item in jenkins.items){
println("\t ${item.name}");
println("\t ${item.name.getClass()}");
if (item.name ==~ REGEX){
item.delete()
println("TEST");
}
}
When I run it, i get a list of all jobs: Building in workspace /var/lib/jenkins/jobs/Bulk_Delete_Job/workspace
REGEX is /AAA.*/
AAA_test_1
class java.lang.String
AAA_test_2
class java.lang.String
AAA_test_3
class java.lang.String
Bulk_Delete_Job
class java.lang.String
ConfiguredJob
class java.lang.String
JobZ
class java.lang.String
TEST_SCM
class java.lang.String
But nothing is deleted / no "TEST" is printed -> the if is never true....
Assistance is required, Thanks!
The String should not include the slashes - use AAA.* instead of /AAA.*/.
这篇关于groovy if语句与变量中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!