groovy内联shell脚本

flseospp  于 7个月前  发布在  Shell
关注(0)|答案(1)|浏览(75)

我有下面的共享库,它在Jenkins管道中使用。在遍历列表时,我希望得到以下输出

**firstDestination

secondDestination**
相反,我得到了

**[firstDestination,

secondDestination]**
我不明白为什么会这样。我的错误在哪里?

List<String> pushDestination = new ArrayList()
   
    String suffix = ''
    if (config.tagSuffix) {
        suffix = "-$config.tagSuffix"
    }

    if (config.imageTag) {
        List tagList = ParameterUtils.convertParameterToList(this, config.imageTag)
        tagList.each { tag ->
            String fullTag = "$tag$suffix"
            fullTag = fullTag.replace('{commitId}', commitId)

            pushDestination.add("firstDestination")

        }
    }

    if (config.get('createCommitTag', false).toBoolean()) {
        pushDestination.add("secondDestionation")
    }

    container('buildah') {

        sh """

            for currentDestination in ${pushDestination}; do

             echo "currentDestination: \$currentDestination"

            done

        """
    }

}
insrf1ej

insrf1ej1#

从你的shell代码中,你有:

container('buildah') {
  sh """
      for currentDestination in ${pushDestination}; do
         echo "currentDestination: \$currentDestination"
      done
  """

但是${pushDestination}将被渲染为字符串[ firstDestination, secondDestination ],但是bash需要不同的数组定义语法(即(a b c d)),所以你想渲染如下:

for currentDestination in (${pushDestination.join(" ")}); do
      echo "currentDestination: \$currentDestination"
   done

相关问题