我想在我的 Jenkins 管道中獲取我的 Git 遠程中的實際標簽。到目前為止我有
node("node1") {
def tags = null
stage("Get tags") {
sshagent(["my-ssh-key"]) {
tags = sh(script: "git ls-remote --tags origin",
returnStdout: true)
}
println tags
}
}
我得到了,正如預期的那樣
ad10e315b9be0503727e4f787ee5779caed1be0f refs/tags/st-2021-12-15-6
40e642c746852298513e80d4f778febf99578d64 refs/tags/st-2021-12-15-6^{}
64d64e515fd3a745e3119c5d318b242b1cfc3cef refs/tags/st-2021-12-15-7
63ff8407af9ecf38b6cb38ed7479fd1aab88dc8e refs/tags/st-2021-12-15-7^{}
56be4e5a1e131c56dcca4ae1ccf19cb542ad7590 refs/tags/st-2021-12-15-8
93b6cbdfa2bb95231a39b5407eb446bcaf7a7931 refs/tags/st-2021-12-15-8^{}
43d85ec58628e390296ec473ea982671be235759 refs/tags/st-2021-12-16-4
96fb5e45590957a3df14402362f3e7bcef839f67 refs/tags/st-2021-12-16-4^{}
但是,tags似乎只是一個長字串,而不是像我預期的那樣由 CR ("/n") 分隔的多行。我知道因為我能做到
println tags.split("/n").size()
1
即使用空格分割也不起作用
println tags.split(" ").size()
1
那么如何決議輸出來獲取標簽呢?
uj5u.com熱心網友回復:
tags.readLines()
tags此時應該有java.lang.String型別
你可以通過 println tags.getClass()
https://docs.groovy-lang.org/docs/latest/html/groovy-jdk/java/lang/String.html
String 從 CharSequence 繼承方法并且它有方法 readLines
https://docs.groovy-lang.org/docs/latest/html/groovy-jdk/java/lang/CharSequence.html#readLines()
順便說一句:您可以使用 split 但 readLines 負責處理 windows ( \r\n) 和 linux ( \n) 換行符。
tags.split("\n").size()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/388721.html
