简体   繁体   English

更改 Gitlab CI Runner 用户

[英]Change Gitlab CI Runner user

Currently when I start a build in GitlabCI it is running under gitlab-runner user.目前,当我在 GitlabCI 中开始构建时,它在 gitlab-runner 用户下运行。 I want to change it the company's internal user.我想把它改成公司的内部用户。 I didn't find any parameter to the /etc/gitlab-runner/config.toml which is solve that.我没有找到解决这个问题的 /etc/gitlab-runner/config.toml 的任何参数。

My current configuration:我目前的配置:

concurrent = 1
[[runners]]
  name = "deploy"
  url = ""
  token = ""
  executor = "shell"

Running ps aux you can see:运行ps aux可以看到:

/usr/bin/gitlab-ci-multi-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runner

Service is running with option --user .服务正在使用选项--user运行。

So let's change this, it depends on what distro.所以让我们改变这一点,这取决于发行版。 you are running it.你正在运行它。 If systemd, there is a file:如果是systemd,有一个文件:

/etc/systemd/system/gitlab-runner.service: /etc/systemd/system/gitlab-runner.service:

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/bin/gitlab-ci-multi-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--se

Bingo, let's change this file now: Bingo,让我们现在改变这个文件:

gitlab-runner uninstall

gitlab-runner install --working-directory /home/ubuntu --user ubuntu

reboot the machine or reload the service (ie systemctl daemon-reload ), et voilà!重新启动机器或重新加载服务(即systemctl daemon-reload ),等等!

Note that when installing with a specific user (--user), whenever you update, it will revert back to the original systemd script and so, back to using gitlab-runner user.请注意,当使用特定用户 (--user) 安装时,无论何时更新,它都会恢复到原始 systemd 脚本,因此,回到使用 gitlab-runner 用户。

in order to keep the user change across updates, using systemd overrides (centos7) you can use these steps (assuming service is at /etc/systemd/system/gitlab-runner.service ):为了在更新中保持用户更改,使用 systemd 覆盖(centos7),您可以使用以下步骤(假设服务位于/etc/systemd/system/gitlab-runner.service ):

  1. Create a /etc/systemd/system/gitlab-runner.service.d directory.创建一个/etc/systemd/system/gitlab-runner.service.d目录。
  2. Create a /etc/systemd/system/gitlab-runner.service.d/exec_start.conf file, with content:创建一个/etc/systemd/system/gitlab-runner.service.d/exec_start.conf文件,内容为:

     [Service] ExecStart= ExecStart=/usr/lib/gitlab-runner/gitlab-runner "run" "--working-directory" "/home/ubuntu" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "ubuntu"
  3. Execute systemctl daemon-reload执行systemctl daemon-reload


Now to check this is working, you can do this:现在要检查这是否有效,您可以执行以下操作:

  1. Reinstall GitLab Runner package gitlab-runner uninstall and then gitlab-runner install重新安装 GitLab Runner 包gitlab-runner uninstall然后gitlab-runner install

  2. Check ps aux | grep gitlab检查ps aux | grep gitlab ps aux | grep gitlab and confirm the right user is being used ps aux | grep gitlab并确认正在使用正确的用户

source: https://gitlab.com/gitlab-org/gitlab-runner/issues/3675来源: https : //gitlab.com/gitlab-org/gitlab-runner/issues/3675

[DEPRECATED ANSWER] [已弃用的答案]

I found a solution, which is not best pactrice but solved it.我找到了一个解决方案,这不是最好的方法,但解决了它。 I need to use the ssh executer and ssh to localhost.我需要使用 ssh 执行器和 ssh 到本地主机。 It is require to add gitlab-runner id_rsa.pub to the user's authorized_keys what you want to use.需要将gitlab-runner id_rsa.pub 添加到用户的authorized_keys 中你想使用什么。 There is my extended code:有我的扩展代码:

concurrent = 1

[[runners]]
  name = "deploy"
  url = ""
  token = ""
  executor = "ssh"
  [runners.ssh]
    user = "user"
    host = "localhost"
    port = "22"
    identity_file = "/home/gitlab-runner/.ssh/id_rsa"

Once the gitlab-runner is registered (yes, it will be installed under the user gitlab-runner and working directory /home/gitlab-runner ) you can execute the following to change the runner's user注册gitlab-runner (是的,它将安装在用户gitlab-runner和工作目录/home/gitlab-runner )您可以执行以下操作来更改 runner 的用户

gitlab-runner uninstall
gitlab-runner install --working-directory <existing-path> --user <any-existing-user>

# eg: gitlab-runner install --working-directory /home/ec2-user --user ec2-user

then restart the service然后重启服务

service gitlab-runner restart

NOTE: you don't need to edit /etc/systemd/system/gitlab-runner.service for this, as it is being updated once the service is restarted as above注意:您不需要为此编辑/etc/systemd/system/gitlab-runner.service ,因为它会在服务如上所述重新启动后更新

to check if the configurations are reflecting, run检查配置是否反映,运行

ps aux | grep gitlab

Just for future reference, I was doing a test with a cloned version of my setup, if the domainname is not pointing to the server you are working with, gitlab might consider your runners offline.仅供将来参考,我正在使用我的设置的克隆版本进行测试,如果域名未指向您正在使用的服务器,gitlab 可能会考虑您的跑步者离线。 If you have another (copied) instance running at the ip the domain is pointing at and there is no firewall blocking, the gitlab-runner verify command will say your runners are alive.如果您有另一个(复制的)实例在域指向的 ip 上运行,并且没有防火墙阻止,则 gitlab-runner verify 命令会说您的跑步者还活着。

a solution could be adding your domain pointing to 127.0.0.1 to your hosts file.解决方案可能是将指向 127.0.0.1 的域添加到主机文件中。 you'll have to restart your gitlab instance and runners.您必须重新启动 gitlab 实例和运行程序。

对于最新版本的 gitlab-runner,您应该修改/etc/default/gitlab-runner文件中的系统参数。

Here example for docker gitlab-runner:这里搬运工gitlab亚军例如:

Build your own runner image based on Dockerfile with following content使用以下内容基于 Dockerfile 构建您自己的运行器映像

FROM gitlab/gitlab-runner
# add new user (if needed)
RUN useradd -u 998 gitlab-www && mkdir /home/gitlab-www && \
    chown gitlab-www /home/gitlab-www && chmod u+rwx /home/gitlab-www
# need to replace entrypoint to force new created user over gitlab-runner
ENTRYPOINT /usr/bin/dumb-init /entrypoint run --user=gitlab-www --working-directory=/home/gitlab-www

(update -u 998 and gitlab-www as you need) (根据需要更新-u 998gitlab-www

.gitlab-ci.yml scripts are running as user gitlab-www now. .gitlab-ci.yml 脚本现在以用户gitlab-www运行。 If this one has same uid as host mounts, you are also able to deploy directly to host folders.如果这个 uid 与主机挂载相同,您也可以直接部署到主机文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM