[英]Why does this groovy code work differently in jenkins pipeline script
[英]How does this jenkins groovy script work?
這是我在 Jenkins 上的 groovy 腳本
def gitURL = "http://bitbucket.webapp.intern.de/scm/myproject.git"
def command = "git ls-remote -h $gitURL"
def proc = command.execute() // line 3
proc.waitFor()
if ( proc.exitValue() != 0 ) {
println "Error, ${proc.err.text}"
System.exit(-1)
}
def branches = proc.in.text.readLines().collect { // line 9
it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '') // line 10
}
return branches
首先我什么都不明白。 如何在第 3 行調用 execute on 命令,它應該是一個字符串?
第 9 行中的變量分支是什么類型??? 分支是字符串嗎??
第 10 行中的 it.replaceAll 是什么??? 我知道 replaceAll 是 String 中的方法。 但它有2個參數。 我在這里沒有看到 2 個參數。
現在我以某種方式理解分支包含所有分支。 我想做的事。 我只想擁有包含“-REST”的分支,它們的名稱。 我怎樣才能做到這一點 ??
我的意圖是使用java。 但它不起作用。
List<String> branchesNew = new ArrayList<String>();
for(String branch : branchesNew)
{
if(branch.contains("-REST"))
branchesNew.add(branch);
}
第 3 行:這是 Groovy 功能,請參閱http://groovy-lang.org/groovy-dev-kit.html#process-management
第 9 行:它是一個集合(例如一個列表),作為collect
方法的輸出給出。 另見http://groovy-lang.org/groovy-dev-kit.html#_working_with_collections
第 10 行:第二個參數是 ''。 它用空字符串替換正則表達式匹配(分支前綴)。
您可以使用集合上的findAll
方法過濾所有想要的分支。 按照給定的示例:
def branchesNew = proc.in.text.readLines().findAll{ it.contains('-REST') }
查看過濾和搜索所有相關操作方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.