如何在jenkins管道的shell脚本中访问groovy变量?

时间:2020-07-13 06:16:39

标签: bash shell jenkins groovy jenkins-pipeline

尝试创建具有以下代码段的jenkins管道以从用户那里获取输入并在shell脚本中打印该输入:

script{
        def user_input= input id: 'user_input', message: 'Choose a service which you want to deploy', ok: 'Confirm', parameters: [string(defaultValue: 'default', description: 'Entered value must be within range', name: 'selection', trim: true)]
        echo user_input //works fine
    
    sh '''
        #!/bin/bash
        set +x
        echo Step 2
        echo "${user_input}" //Printing blank space
        '''
     }

如果我使用这样的外壳,它也可以正常工作:

sh "echo $user_input"

1 个答案:

答案 0 :(得分:0)

在shell脚本sh "echo $user_input"中对groovy变量使用双引号 而且我认为另一个问题可能是您在脚本外声明了sh变量的脚本之外使用了user_input