简体   繁体   中英

In Groovy - How to add comma in git tag list?

I am doing git tag on a repo and getting below output.

command

def tagversion = sh(script:""" git tag --sort=v:refname """, returnStdout:true).trim()

Output:-

1.0
1.10.0
1.11.0
1.6
1.7
1.7.1

how can i add comma after each version?

expected output

1.0,
1.10.0,
1.11.0,
1.6,
1.7,
1.7.1

I have tried below code which works but add comma at last version as well and the entire list is showing as single string.

sh(script:""" git tag --sort=v:refname | tr '\n' ',' """, returnStdout:true).trim()

sh返回一个字符串, readLines()会将它拆分为一个数组, join会将它放回带有一些分隔符的字符串

sh(...).trim().readLines().join(',\n')

if you are a fan of shell script:

stage('Test') {
        steps { script {
            def tagversion = sh(script:''' git tag --sort=v:refname | awk '{printf "%s%s",SEP,$0;SEP=", "}END{print ""}' | tr ' ' '\\n' ''', returnStdout:true).trim()
            echo tagversion
        }}
    }

taking from answer from @daggett which has a slight escape char mistake:

stage('Test') {
        steps { script {
            def tagversion = sh(script:''' git tag --sort=v:refname ''', returnStdout:true).trim().readLines().join(',\n')
            echo tagversion
        }}
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM