如何在Jenkins管道脚本中将Shell脚本变量用作groovy变量

时间:2020-06-30 18:54:55

标签: bash shell jenkins groovy jenkins-pipeline

我正试图在groovy步骤中再次使用sh“”“”“”中的shell脚本中设置的VAR_NAME值,但出现以下错误。我只看到了有关如何在shell中使用groovy变量的问题,但没有其他问题。预先感谢。

groovy.lang.MissingPropertyException:没有此类属性:groovy.lang.Binding

的VAR_NAME
SizedBox(
  height: ... // an arbitrary height, needed because I have multiple of these in a column 
  child: ListView.builder(
    scrollDirection: Axis.horizontal,
    itemBuilder: (context, i) => Column(
      children: [
        Expanded(images[i]), // an image
        Text(captions[i]), // a caption
      ]
    ),
  ),
)

1 个答案:

答案 0 :(得分:4)

发出sh指令时,将创建一个新的Shell实例(很可能是bash)。与通常的Unix进程一样,它继承父级的环境变量。然后,您的bash实例正在运行您的脚本。当脚本设置环境变量时,bash的环境将更新。脚本结束后,运行脚本的bash进程将被销毁,并且其所有环境也将随之销毁。

如果要使用该shell实例设置的任何内容,则需要将其放入,例如:

    def script_output = sh(returnStdout: true, script: """
         #!/bin/bash
        set -e
        set +x
        VAR_NAME=10
        echo \$VAR_NAME
    """)
    script_output = script_output.trim()
    VAR_NAME = script_output
    echo "VAR_NAME is ${VAR_NAME}"