【问题标题】:Access a Groovy variable within shell step in Jenkins pipeline在 Jenkins 管道的 shell 步骤中访问 Groovy 变量
【发布时间】:2021-06-28 16:26:48
【问题描述】:

SSHUER in stage one 根据需要打印。但在stage two 中,它不会打印并给我一个空值。 stage two 中的所有三个尝试语句都不起作用。我不能使用脚本块,因为我在实际代码中有一些特殊字符。关于如何在stage two 中获取 SSHUSER 值的任何建议。

def SSHUSER

environment {
if(params.CLOUD == 'google') {
   SSHUSER = "test1"
} else if (params.CLOUD == 'azure') {
   SSHUSER = "test2"
}
}

pipeline {
    stage('stage one') {
      steps {
        echo "first attempt '${SSHUSER}'"
      }
    }

    stage('stage two') {
      steps {
      sh '''#!/bin/bash
          echo "first attempt '${SSHUSER}'"
          echo "second attempt \${SSHUSER}"
          echo "thrid attempt \$SSHUSER"

          if ssh  \${SSHUSER}@$i 'test -e /tmp/test'";
          then
              echo "$i file exists "
          fi
         '''
      }
    }
  }

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    您对environment 块的使用有点奇怪,它应该位于pipeline 块内,变量应该在environment 块内初始化。
    如果是在外面定义的变量将不会作为环境变量传递给shell脚本。

    如果你想在 `pipeline' 块中使用environment 块,只需定义你想要的参数(作为字符串),它们将可用于所有阶段,它们将作为 shell 的环境变量传递。
    例如:

    pipeline {
        agent any
        parameters{
            string(name: 'CLOUD', defaultValue: 'google', description: '')
        }
        environment {
            SSHUSER = "${params.CLOUD == 'google' ? 'test1' : 'test2'}"
        }
        stages {
            stage('stage one') {
                steps {
                    echo "first attempt '${SSHUSER}'"
                }
            }
    
            stage('stage two') {
                steps {
                    sh '''#!/bin/bash
              echo "first attempt '${SSHUSER}'"
              echo "second attempt \${SSHUSER}"
              echo "thrid attempt \$SSHUSER"
    
              if ssh  \${SSHUSER}@$i 'test -e /tmp/test'";
              then
                  echo "$i file exists "
              fi
             '''
                }
            }
        }
    }
    

    如果您不想使用environment 块或自己管理参数,您可以在pipeline 块之外使用全局变量进行操作,但它们不会被传递环境变量,您需要使用字符串插值(""" 而不是 ''')来计算将命令传递给 shell 时的值:

    SSHUSER = ''
    
    if(params.CLOUD == 'google') {
        SSHUSER = "test1"
    } else if (params.CLOUD == 'azure') {
        SSHUSER = "test2"
    }
    
    pipeline {
        agent any
        parameters{
            string(name: 'CLOUD', defaultValue: 'google', description: 'Bitbucket Payload', trim: true)
        }
        stages {
            stage('stage one') {
                steps {
                    echo "first attempt '${SSHUSER}'"
                }
            }
    
            stage('stage two') {
                steps {
                    sh """#!/bin/bash
              echo "first attempt '${SSHUSER}'"
              echo "second attempt ${SSHUSER}"
              echo "thrid attempt $SSHUSER"
    
              if ssh  ${SSHUSER}@\$i 'test -e /tmp/test'";
              then
                  echo "\$i file exists "
              fi
             """
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢,我能够从你的例子中实现我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2021-10-23
    相关资源
    最近更新 更多