19年团队使用了 Gitlab-CI,做一些自动构建流程。最近团队小伙伴自己尝试搭建流程,参照了我之前发的文章 – Gitlab–CI。但过程中,遇到了用户执行权限的问题。于是有了下面的内容…
按照文章(https://cloud.tencent.com/developer/article/1487180)中说明,操作完成发现了权限问题。
首先要明确,CI 默认执行用户为 gitlab-runner
$ ps aux | grep gitlab
/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
文章中包括 gitlab-runner 服务启动在内的,所有操作都是使用 sudo。这也是为什么,文章中,将 gitlab-runner 免密使用sudo命令,并在脚本的命令前加上 sudo 的要求。
# 切换到root账号下
$ su
# 添加sudo文件的写权限
$ chmod u+w /etc/sudoers
# 编辑sudoers文件
$ vi /etc/sudoers
# 添加如下内容 允许用户gitlab-runner执行sudo命令,并且在执行的时候不输入密码
gitlab-runner ALL=(ALL) NOPASSWD: ALL
# 撤销sudo文件写权限
$ chmod u-w /etc/sudoers
官方关于权限的说明,可参照:Super-user permission
上述内容,是团队小伙伴所忽略的。当然,小伙伴提出了另外一种方式,使用 root 执行。
/etc/systemd/system/gitlab-runner.service
Service ExecStart=/usr/bin/gitlab-ci-multi-runner "run" "--working-directory" "/工作目录" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "root"
arameter Default Description –service gitlab-runner Specify service name to use –config See the configuration file Specify a custom configuration file to use –syslog true Specify if the service should integrate with system logging service –working-directory the current directory Specify the root directory where all data will be stored when builds will be run with the shell executor –user root Specify the user which will be used to execute builds –password none Specify the password for the user that will be used to execute the builds 综上,欢迎大家积极讨论~~~