jenkins - 詹金斯:关于 ubuntu“詹金斯”用户的混淆和 gitlab 拒绝的 SSH 权限
问题描述
我正在我的 ubuntu 服务器中设置 Jenkins 并尝试创建一个自动构建作业。我的计划是在我的本地计算机上,在我对应用程序仓库中的主分支执行 git push 后,我的 ubuntu 服务器中的 Jenkins 将拉出最新的主分支并开始运行应用程序。
我已经成功设置了 Jenkins 并使用 Gitlab 配置了大多数步骤:
- 我在 Gitlab 中创建了个人访问令牌并在 Jenkins 服务器中配置了令牌。
- 我已经设置了自动构建作业并选择了
Build when a change is pushed to GitLab. GitLab webhook URL: http://35.222.111.55:8080/project/test
;我已经创建了 Secret Token 并使用 webhook 集成配置了 Gitlab Repo。 - 在 Jenkins Auto-build job configuration
Source 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 运行构建时,它通过不同的用户执行操作。我做了一些研究,发现:
jenkins
Jenkins在安装时会在 ubuntu 中创建一个用户。- 当 Jenkins 尝试构建和执行 shell(例如
git pull
步骤)时,它使用jenkins
用户来执行此操作。
这意味着当我在 ubuntu 服务器中使用 Jenkins 和 gitlab 设置 CI/CD 时,我需要:
su - jenkins
切换到jenkins
用户并为用户ssh-keygen -o -t rsa -b 4096
创建 SSH 密钥对。然后将这个 SSH 密钥保存在 gitlab 中。- 在我第一次
git clone my-repo
在ubuntu服务器上之后,我需要进入my-repo/.git
并做sudo chown -R jenkins .git/
给jenkins
用户权限做git pull
- 对于构建中的任何其他步骤,例如创建目录或删除文件/目录,我需要手动进入 ubuntu 服务器并
jenkins
首先为用户授予相关权限。
为什么没有关于这些步骤的官方文档?
另外,当我su - jenkins
切换到用户时,它会要求输入密码。在Jenkins安装中创建这个用户时如何找到它给用户的密码???
所有这些似乎都过于复杂了,我想我一定是在这里做错了什么。我错过了什么?
解决方案
推荐阅读
- python - 如何将字符分配给Python中的类
- android - 错误:迁移到 android x 后包 android.support.v7.app 不存在
- python - numpy的累积点积
- r - 在 Rmarkdown 中生成动态 .tabset
- r - 如何用 R (papaja) 写一个方程?
- android - 与 Android Studio 中的计数器对话
- amazon-web-services - Terraform 中的数据源
- javascript - 如果字段返回 TypeError,则忽略 JSON 解析错误
- javascript - 当 ob1.prototype 未定义时,Object.create() 应该如何从 ob1.prototype 创建 obj2.__proto__?
- python - Pandas - 根据与数据框中某些值匹配的系列索引将值从系列添加到数据框列