jenkins上的交互式shell脚本

时间:2015-04-22 05:51:25

标签: shell ssh jenkins

我是詹金斯的新手。我正试图通过使用'在远程主机上使用ssh'选项执行shell脚本来调用jenkins中的远程shell脚本。但是我在shell脚本中使用了read命令而且它不能用于jenkins.Can任何人都可以帮助我如何在jenkins上获取shell脚本的用户输入。

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

提前致谢,

普加

2 个答案:

答案 0 :(得分:2)

您可以在Jenkins管道中使用输入步骤,然后将其传递给可以为您输入信息的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')]

现在结果将存储在'选项'变量。这可以传递给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

请注意,有些示例使用了“互动”。在末尾。我发现最后两行'期待eof ...等待'在詹金斯工作需要它。

您可以通过从输入步骤获得的选项中传递来自管道的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      
 }
}

答案 1 :(得分:0)

AFAIK不支持交互式shell,您可以使用输入参数。

https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build

干杯

相关问题