首页 > 解决方案 > 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()

标签: groovyjenkins-groovy

解决方案


sh returns you a string, readLines() will split it to an array, join will put it back to string with some delimiter

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

推荐阅读