从Jenkins管道中的shell步骤中访问Groovy变量

时间:2016-10-11 16:43:51

标签: jenkins jenkins-pipeline

使用Pipeline plugin in Jenkins 2.x,如何在sh步骤中访问在阶段或节点级别定义的Groovy变量?

简单示例:

node {
    stage('Test Stage') {
        some_var = 'Hello World' // this is Groovy
        echo some_var // printing via Groovy works
        sh 'echo $some_var' // printing in shell does not work
    }
}

在Jenkins输出页面上给出以下内容:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test Stage)
[Pipeline] echo
Hello World
[Pipeline] sh
[test] Running shell script
+ echo

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

可以看出,echo步骤中的sh会打印一个空字符串。

解决方法是通过

在环境范围内定义变量
env.some_var = 'Hello World'

并通过

打印
sh 'echo ${env.some_var}'

然而,这种滥用这项任务的环境范围。

5 个答案:

答案 0 :(得分:30)

要使用可模板化的字符串,其中变量被替换为字符串,请使用双引号。

sh "echo $some_var"

答案 1 :(得分:6)

I am adding the comment from @Pedro as an answer because I think it is important.

For sh env vars we must use

sh "echo \$some_var" 

答案 2 :(得分:2)

我想在此讨论中添加另一种方案。 我在同一脚本中使用了shell环境变量和groovy变量。

 format='html'
    for file in *.txt; 
        do mv  -- "\$file" "\${file%.txt}.$format";
    done

因此,在这里,我要做的是仅将\ $用于外壳环境变量,将$用于常规变量。

答案 3 :(得分:1)

如果需要 bash 脚本,您需要执行以下操作:

在全局或本地(函数)级别设置此变量,sh 脚本可以访问这些变量:

Widget ListItem({
  String imageUrl,
  String text,
  String caption,
  double imageSize = 50,
  double horizontalSpacing = 15,
  double verticalspacing = 20,
  bool showDetailIndicatorIcon = true,
  GestureTapCallback onTap,
}) {
  return InkWell(
    onTap: onTap,
    child: Container(
      margin: EdgeInsets.only(top: 2),
      padding: EdgeInsets.symmetric(
          horizontal: horizontalSpacing, vertical: verticalspacing),
      child: Row(
        children: [
          ClipRRect(
            child: Image.network(
              imageUrl,
              fit: BoxFit.cover,
              height: imageSize,
              width: imageSize,
            ),
            borderRadius: BorderRadius.circular(imageSize / 2),
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    child: Text(
                      text,
                      style: TextStyle(
                        color: ColorRes.lightWhite,
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  SizedBox(height: 5),
 
                  Text(
                    caption,
                    style: TextStyle(
                      color: ColorRes.grey,
                      fontSize: 12,
                      fontWeight: FontWeight.normal,
                    ),
                    maxLines: 3,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    ),
  );
}

在shell脚本中像下面这样调用它们

def stageOneWorkSpace = "/path/test1"
def stageTwoWorkSpace = "/path/test2"

确保以三个冒号开始和结束 sh,例如 sh ''' echo ''' +stageOneWorkSpace+ ''' echo ''' +stageTwoWorkSpace+ ''' cp -r ''' +stageOneWorkSpace+'''/qa/folder1/* ''' +stageOneWorkSpace+'''/qa/folder2 '''

答案 4 :(得分:0)

为了将groovy参数传递到Jenkins管道中的bash脚本中,请使用此帖子here

相关问题