我正在使用 jenkins 来运行我的构建。
我的步骤之一是复制某些文件并使用 sudo 将它们放在/etc/init 中
<exec command="ssh -A ${host-used} '/etc/init.d/ConvertDaemon stop'" outputProperty="result" escape="false">
</exec>
<echo msg="${result}" />
<exec command="ssh -A ${host-used} 'sudo Console/cake init_runners copy_runners_into_initd'" outputProperty="result" escape="false" />
<echo msg="${result}" />
${host-used} returns www-data@ip-address
问题是第二个命令。
当我运行第一个命令时,我可以在 jenkins 的控制台日志中看到我的结果打印出来。
对于第二个命令,我收到一条no tty present
消息
所以我将第二条命令改为
<exec command="ssh -t ${host-used} -A ${host-used} 'sudo Console/cake init_runners copy_runners_into_initd'" outputProperty="result" escape="false" />
<echo msg="${result}" />
我得到一个
Pseudo-terminal will not be allocated because stdin is not a terminal
我该如何克服这个问题?
更新:
我也试过了。
<exec command="ssh -t -t ${host-used} -A ${host-used} 'sudo Console/cake init_runners copy_runners_into_initd'" outputProperty="result" escape="false" />
<echo msg="${result}" />
我得到了:
bash: www-data@ipaddress: command not found
Connection to ipaddress closed.
我要强调的是命令使用正确。但是我无法阅读消息。
更新 2:
有时候您问了一个问题,但随后您意识到您要问的问题并不是您要解决的真实问题。
这道题就是这样。
我只想执行一些由 jenkins 运行的命令并查看命令的输出。这些命令恰好需要 sudo。
Jenkins 基本上通过 ssh 作为另一个用户 www-data 来执行这些命令。
我在 jenkins 中打印出需要 sudo 的命令的结果时遇到了问题。
我花了一个愉快的星期六来解决这个问题,并提供了一些非常有用的 SO 答案,我意识到我可以简单地尝试执行这些命令 而无需 sudo,而是将 jenkins ssh 作为 root。
成功了。
我打算用那种方法来回答这个问题,因为这就是我想要的——在 jenkins 中执行一些命令并查看它们的打印结果。
最佳答案
我认为您不需要两次指定主机。
<exec command="ssh -t -t -A ${host-used} 'sudo Console/cake init_runners copy_runners_into_initd'" outputProperty="result" escape="false" />
<echo msg="${result}" />
关于linux - 如何在 jenkins exec 命令中使用 sudo 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18922564/