詹金斯 stop/start 系统服务

Jenkins stop/start system service

大家好,在我的 Jenkins 管道中,我需要停止系统服务,复制最新的文件,然后重新启动服务。我卡在停止系统服务步骤了。

操作系统是 CentOS,Jenkins 是 运行 在用户 'jenkins' 下。

按照此 post (https://serverfault.com/questions/772778/allowing-a-non-root-user-to-restart-a-service) 中接受的答案,我创建了一个新组,将 'jenkins' 添加到该组中,然后通过 'visudo' 更新了 sudo 列表。现在用户 jenkins 可以像这样在 Putty 命令行上 stop/start 该服务:sudo systemctl stop <my-service>

当我像下面这样更新管道文件时

                sh "echo stop Linux service"
                sh "sudo systemctl stop <my-service>"

我收到以下错误:

+ echo stop Linux service
stop Linux service
[Pipeline] sh
+ sudo systemctl stop <my-service>

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

如果我在管道中删除 sudo,则会出现此错误:

+ echo stop Linux service
stop Linux service
[Pipeline] sh
+ systemctl stop <my-service>
Failed to stop <my-service>: Interactive authentication required.
See system logs and 'systemctl status <my-service>' for details.

请问我怎样才能实现我的目标?

将用户添加到 sudoers 组只允许他们调用 sudo。他们仍然需要使用密码进行身份验证。由于您不是 运行 通过 TTY 执行此命令,因此无法询问您的密码,因此 sudo 说 "Nuh uh."

然后,显而易见的解决方案是确保您的 Jenkins 代理用户根本不需要密码来调用 sudo。您可以通过在 /etc/sudoers.

中向该用户添加 NOPASSWD 来做到这一点

运行 visudo,并将行设置为:

jenkins ALL=(ALL) NOPASSWD: ALL