Jenkins SSH管道步骤-需要终端读取密码

wkyowqbh  于 2022-12-03  发布在  Jenkins
关注(0)|答案(2)|浏览(320)

最近我一直在研究Docker/Jenkins,以了解更多关于CI/CD工作流的信息。我的目标是创建一个从github获取推送、构建代码并将JAR文件发布到远程服务器以供进一步部署的过程。
我一直在使用Jenkins SSH Pipeline Steps Plugin,它允许我SSH到我的远程服务器来执行命令。
下面是部署阶段,其中包含我想要执行的命令:

stage("Deploying") {
            steps {
                script {
                    withCredentials([sshUserPrivateKey(credentialsId: 'e48b15ad-0f5e-4f07-8706-635c5250fa29', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'jenkins')]) {
                      remote.user = jenkins
                      remote.identityFile = identity

                      sshCommand remote: remote, command: 'cd Winston-Bot/; sudo ./kill_winston.sh'
                      sshCommand remote: remote, command: 'rm Winston-Bot/*.jar', failOnError:'false'
                      sshCommand remote: remote, command: 'rm -rf Winston-Bot/src', failOnError:'false'
                      sshPut remote: remote, from: "target/Winston-Bot-${VERSION}-jar-with-dependencies.jar", into: 'Winston-Bot/'
                      sshPut remote: remote, from: "src", into: 'Winston-Bot/'
                      sshCommand remote: remote, command: "echo ${VERSION} > Winston-Bot/version.txt"
                    }
                }
            }
        }

执行cd Winston-Bot/; sudo ./kill_winston.sh时,收到以下错误:

Executing command on ****[51.159.152.230]: cd Winston-Bot/; sudo ./kill_winston.sh sudo: false

sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
Failed command ****#15 with status 1: cd Winston-Bot/; sudo ./kill_winston.sh

我已经使用以下代码将jenkins用户组添加到etc/sudoers中:

jenkins ALL=(ALL) NOPASSWD: /var/lib/jenkins/Winston-Bot/.kill_winston.sh

当通过终端以jenkins用户身份登录到我的远程服务器时,脚本执行得非常好,并且不需要密码。有人能帮我吗?

dgiusagp

dgiusagp1#

我不确定它是否与您相关,但我在macOS Big Sur上遇到了完全相同的问题。无论我如何使用sudo visudo编辑sudoers文件,它都无法工作。
当我创建并编辑正确的文件时,它立即开始工作:sudo visudo -f /etc/sudoers.d/jenkins

jenkins ALL=(ALL) NOPASSWD: ALL

我从这条评论和this answer中得到了这个想法。

kwvwclae

kwvwclae2#

我遇到了同样的问题:工作正常,并且在通过终端使用SSH时未提示输入sudo密码,但在使用SSH密钥通过Jenkins作业运行时,出现错误:
sudo:需要终端读取密码;使用-S选项从标准输入读取,或配置askpass帮助器sudo:需要密码
@farkasseb提出的修复方案对我来说也很有效,可能是最好的解决方案,特别是考虑到sudoers文件顶部的这条注解,它鼓励这样做:

# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.

但我仍然想了解更多为什么会发生这种情况。我还发现,在visudo / sudoers文件中,我放置的位置很重要:

jenkins ALL=(ALL) NOPASSWD: ALL

如果直接放在root之后,则通过jenkins SSH而不是终端SSH重现问题。如果放在%admin%sudo组的定义之后,则通过jenkins运行时不再提示输入密码。
因此,在sudoers文件中将jenkins用户声明向下移动会导致在使用此命令列出jenkins用户的权限时出现以下权限顺序:

sudo -l -U jenkins

从这里:

(ALL) NOPASSWD: ALL
(ALL : ALL) ALL

对此:

(ALL : ALL) ALL
(ALL) NOPASSWD: ALL

我假设因为NOPASSWD是最后一个,所以它具有优先级?下面是sudoers文件的完整片段,该文件不提示通过jenkins输入密码:

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# add in jenkins user AFTER the sudo and admin group
jenkins ALL=(ALL) NOPASSWD: ALL

# See sudoers(5) for more information on "@include" directives:
@includedir /etc/sudoers.d

最后,我可以得出这样的结论:通过使用sudo visudo -f /etc/sudoers.d/jenkins命令创建一个单独的文件,并将其包含在@includedir /etc/sudoers.d中,jenkins ALL=(ALL) NOPASSWD: ALL权限也会排在最后,这与上面的效果相同。
我仍然不明白的是,当通过Jenkins SSH与终端SSH运行时,为什么或如何会导致不同的行为?
在任何情况下,如果您不想直接修改sudoers文件,请使用@farkasseb的方法(在升级操作系统时,它显然有其他优点)。如果您可以直接修改sudoers文件,那么您可以使用此方法,只要确保NOPASSWD部分位于最后,在组定义之后。

相关问题