19年團隊使用了 Gitlab-CI,做一些自動構建流程。最近團隊小夥伴自己嘗試搭建流程,參照了我之前發的文章 – Gitlab–CI。但過程中,遇到了用戶執行權限的問題。於是有了下面的內容…
問題描述
按照文章(https://ligang.blog.csdn.net/article/details/89785856)中說明,操作完成發現了權限問題。
問題覆盤
首先要明確,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 -
重新加載配置文件,並啓動
$ systemctl daemon-reload $ systemctl restart gitlab-runner
綜上,歡迎大家積極討論~~~