首页 > 解决方案 > 詹金斯:关于 ubuntu“詹金斯”用户的混淆和 gitlab 拒绝的 SSH 权限

问题描述

我正在我的 ubuntu 服务器中设置 Jenkins 并尝试创建一个自动构建作业。我的计划是在我的本地计算机上,在我对应用程序仓库中的主分支执行 git push 后,我的 ubuntu 服务器中的 Jenkins 将拉出最新的主分支并开始运行应用程序。

我已经成功设置了 Jenkins 并使用 Gitlab 配置了大​​多数步骤:

  1. 我在 Gitlab 中创建了个人访问令牌并在 Jenkins 服务器中配置了令牌。
  2. 我已经设置了自动构建作业并选择了Build when a change is pushed to GitLab. GitLab webhook URL: http://35.222.111.55:8080/project/test;我已经创建了 Secret Token 并使用 webhook 集成配置了 Gitlab Repo。
  3. 在 Jenkins Auto-build job configurationSource Code Management部分中,我提供了 gitlab repo url 和凭证(带有私钥的 SSH 用户名),以便 jenkins 可以从 repo 中提取。

当我推送到本地计算机中的 master 分支时,Jenkins 成功启动了构建。git pull但是,它在 repo的步骤中给出了错误:

+ cd /home/John/jenkins_test/
+ git pull
git@code.myapp.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Build step 'Execute shell' marked build as failure
Finished: FAILURE

这是因为在上面第 3 点中,我提供的 SSH 私钥属于默认用户。当 Jenkins 运行构建时,它通过不同的用户执行操作。我做了一些研究,发现:

  1. jenkinsJenkins在安装时会在 ubuntu 中创建一个用户。
  2. 当 Jenkins 尝试构建和执行 shell(例如git pull步骤)时,它使用jenkins用户来执行此操作。

这意味着当我在 ubuntu 服务器中使用 Jenkins 和 gitlab 设置 CI/CD 时,我需要:

  1. su - jenkins切换到jenkins用户并为用户ssh-keygen -o -t rsa -b 4096创建 SSH 密钥对。然后将这个 SSH 密钥保存在 gitlab 中。
  2. 在我第一次git clone my-repo在ubuntu服务器上之后,我需要进入my-repo/.git并做sudo chown -R jenkins .git/jenkins用户权限做git pull
  3. 对于构建中的任何其他步骤,例如创建目录或删除文件/目录,我需要手动进入 ubuntu 服务器并jenkins首先为用户授予相关权限。

为什么没有关于这些步骤的官方文档?

另外,当我su - jenkins切换到用户时,它会要求输入密码。在Jenkins安装中创建这个用户时如何找到它给用户的密码???

所有这些似乎都过于复杂了,我想我一定是在这里做错了什么。我错过了什么?

标签: jenkinssshgitlab

解决方案


推荐阅读