在子进程中更改 Gitlab CI Runner 用户

在子进程中更改 Gitlab CI Runner 用户

我的 CentOS VM 上遇到以下问题。

在虚拟机中,我以 root 身份运行进程 gitlab-runner - 查看我的 systemd 服务。

root@runner1:~$ cat /etc/systemd/system/gitlab-runner.service | sed 's/^/    /'
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/lib/gitlab-runner/gitlab-runner

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/lib/gitlab-runner/gitlab-runner "run" "--working-directory" "/home/gitlab-runner/builds/" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "root"

Restart=always
RestartSec=120

[Install]
WantedBy=multi-user.target

某些构建需要 root 权限才能在某些位置创建或删除文件。现在 gitlab-runner 总是抛出错误。原因是进程 gitlab-runner 虽然以 root 身份启动,但会以用户 gitlab-runner 身份运行子进程。以下是我的管道日志文件的片段

$ id -u
997
$ whoami
gitlab-runner
$ rm -rf /tmp/*
rm: cannot remove ‘/tmp/ks-script-1yMUS_’: Operation not permitted
rm: cannot remove ‘/tmp/ks-script-jNgpQ5’: Operation not permitted
rm: cannot remove ‘/tmp/systemd-private-1838297d1ab047a99c3628869e77fd18-chronyd.service-z1RO0u’: Operation not permitted
rm: cannot remove ‘/tmp/tmp.THBemf0N5O’: Operation not permitted
rm: cannot remove ‘/tmp/yum.log’: Operation not permitted

如何防止 gitlab-runner 在其他用户下启动子进程?

沃尔克

答案1

我找到了解决方案,如果 gitlab-runner 以 root 身份执行,则工作区不能位于另一个主目录中。

我现在对系统做了如下修改:

[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/lib/gitlab-runner/gitlab-runner

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/lib/gitlab-runner/gitlab-runner "run" "--working-directory" "/root/builds/" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "root"

Restart=always
RestartSec=120

[Install]
WantedBy=multi-user.target

现在,该进程终于以真实用户身份运行。对于开发人员来说,至少提供一些信息来解释为什么该服务不以 root 身份运行,而是以 gitlab-runner 身份运行,这将很有用。

相关内容