Jenkins 上的交互式 shell 脚本

6

我刚接触Jenkins,正在尝试使用“使用SSH在远程主机上执行shell脚本”选项来调用远程shell脚本。但是我在shell脚本中使用了read命令,在Jenkins上无法正常工作。请问有人能帮我解决如何在Jenkins上获取用户输入的问题吗?

script:
echo "hello"
 read -p  "Choose your option : " ch
echo "Hi"

Jenkins上的输出:

Started by user anonymous
[EnvInject] - Loading node environment variables.
Building in workspace C:\Users\pooja_ravi\.jenkins\workspace\freeStyle
[SSH] executing pre build script:    

[SSH] executing post build script:

./testing.sh
./testing.sh[2]: read: no query process
hello
Hi
[SSH] exit-status: 0
Finished: SUCCESS

提前感谢您,

Pooja


(注:无需翻译)

你期望发生什么?Jenkins应该如何猜测输入? - Matteo
需要进一步使用用户输入。像这样的脚本: echo "你好" read -p "请输入一个字符:" ch echo "输入的字符:$ch" 有没有办法让用户在过程中手动在控制台中输入详细信息? - Pooja R
2个回答

3
你可以在Jenkins pipeline中使用input step,然后将其传递给一个expect脚本,该脚本可以为您输入信息。
请参阅https://jenkins.io/doc/pipeline/steps/pipeline-input-step/#code-input-code-wait-for-interactive-input
以下是使用示例:
choice = input message: 'Choose your option', parameters: [string(defaultValue: 'Option 1', description: 'What am I choosing', name: 'Comment')]

现在结果将存储在“choice”变量中。这可以传递给expect脚本,以自动输入答案来回答查询。
示例expect脚本
make_choice.exp
#!/usr/bin/expect
set choice [lindex $argv 0]
spawn read -p  "Choose your option : " ch
expect "option :" { send "$choice\r" }
expect eof
wait

请注意,一些示例在结尾处使用了“interact”命令。我发现最后两行“expect eof ... wait”是必需的,在Jenkins中才能正常工作。
您可以从流水线中调用expect脚本,并传入从输入步骤获取的选择。

E.g:

make_choice.exp $choice

注意:最好不要将expect脚本放在Jenkins节点内。请参见(https://www.cloudbees.com/blog/top-10-best-practices-jenkins-pipeline-plugin第7点的解释)。
相反,您应该将其放在外面。因此,在管道中将其绑定在一起,您会有类似以下内容:
timeout(time:5, unit:'DAYS') {
 choice = input message: 'Choose your option', parameters: [string(defaultValue: 'Option 1', description: 'What am I choosing', name: 'Comment')]
 node {
   ./make_choice.exp $choice      
 }
}

0

1
欢迎来到Stack Overflow!虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。请参阅如何撰写良好的答案 - ByteHamster
在整个过程中,是否有办法让用户在控制台上手动输入详细信息呢? - Pooja R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接