我是 Jenkins 的新手。我正在尝试使用“使用 ssh 在远程主机上执行 shell 脚本”选项来调用 jenkins 中的远程 shell 脚本。但是我在 shell 脚本中使用 read 命令,它在 jenkins 上不起作用。任何人都可以帮助我吗如何在 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
提前致谢,
普迦
最佳答案
您可以使用 Jenkins 管道中的输入步骤,然后将其传递给 expect可以为您输入信息的脚本。
使用此功能的示例如下:
choice = input message: 'Choose your option', parameters: [string(defaultValue: 'Option 1', description: 'What am I choosing', name: 'Comment')]
现在结果将存储在“choice”变量中。这可以传递给 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脚本,传入从输入步骤获得的选择。
例如:
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
}
}
关于shell - jenkins 上的交互式 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29788810/