Jenkins Gitlab集成仅使用Gitlab API令牌。

huangapple go评论63阅读模式
英文:

Jenkins Gitlab Integration using only Gitlab API Token

问题

在Jenkins上安装了GitLab插件,并创建了一个GitLab API令牌。将其添加到Jenkins,但似乎无法在任何地方使用。

当我在Jenkins中设置构建作业时,它要求用户名/密码。即使我尝试在“配置作业”菜单中添加新凭据,它也无法保存,并默认使用我为测试输入的用户名/密码组合。

如果用户离开公司或其他情况,这就相当脆弱,现在我需要更改所有我的Jenkins流水线。有没有办法配置作业仅使用存储库的API令牌?

英文:

Installed the GitLab plugin on Jenkins, created a GitLab API token. Added it to Jenkins, but cannot seem to use it anywhere.

When I set up a build job in Jenkins, it wants a username/password. Even if I try to add new credentials in the Configure Job menu, it doesn't save and defaults to the username/password combo I entered for testing.

That's pretty brittle, if the user were to leave the company or something, then now I need to change all my Jenkins pipelines.

Is there a way to configure the job to only use an API token for a repository?

答案1

得分: 2

查看GitLab-Jenkins集成时,GitLab API令牌用于Jenkins与GitLab进行互操作,用于触发构建、报告构建状态以及其他类似的交互功能。这意味着GitLab API令牌是在Jenkins系统级别使用的,而不是直接在作业级别使用。

也就是说,GitLab API令牌是在"管理Jenkins > 配置系统"级别设置的:

Jenkins Gitlab集成仅使用Gitlab API令牌。

一旦在那里提供了GitLab API令牌,Jenkins将在与GitLab的所有交互中使用它,无论运行哪个具体的作业。

然而,当设置特定的Jenkins作业时,您需要指定该作业应如何与源代码存储库(在本例中为GitLab)交互。这是您通常会指定存储库URL和凭据(用户名和密码或SSH密钥)的地方。这些凭据由作业用于克隆存储库、获取更新并在必要时推送更改。

我建议使用一个SSH密钥,一旦创建(使用命令ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -P "" -f ~/.ssh/gitlab,在测试时不设置密码),您可以将公钥(~/.ssh/gitlab.pub)复制到GitLab的设置/SSH密钥页面中。

在"Key"框中,粘贴您的密钥。如果您使用不同的注释创建了密钥,"Title"字段可能会自动填充该注释。如果没有,给您的密钥取一个可识别的标题,比如'Jenkins服务器'。点击Add key

回到Jenkins,在设置作业时,您可以使用'git' SCM,并以git@gitlab.com:username/repo.git的形式指定存储库URL。在凭据的下拉菜单中,您可以添加与您在GitLab中添加的公钥对应的SSH私钥。Jenkins将使用此SSH密钥进行与GitLab的身份验证。

通过这种方式,您的Jenkins作业将使用SSH密钥进行与GitLab的身份验证,而不是用户名/密码。如果用户离开公司,您只需生成新的SSH密钥对,将公钥添加到GitLab中,并在Jenkins中更新私钥。您的现有作业将继续正常工作,无需修改。

David评论中建议

为此目的添加一个系统用户,由安全团队在Jenkins中注册凭据。
然后将存储库权限授予该用户,并使用该凭据通过SCM运行。

这样,系统用户将不会离开公司,未来凭据的维护将由安全团队负责。

这确实是一个不错的做法,这也是为什么我们在Jenkins中添加了一个名为"Build_Jenkins"的凭据,并在GitLab中声明了一个名为技术Build_Jenkins用户,具有对相关存储库的只读访问权限。

英文:

Looking at the GitLab-Jenkins integration, a GitLab API token for Jenkins integration is mainly for Jenkins to interact with GitLab for functionalities such as triggering builds, reporting build status, and other such interactions. This means that the GitLab API token is used at the Jenkins system level, and not directly on the job level.

IE, the GitLab API token is set at the "Manage Jenkins > Configure System." level:

Jenkins Gitlab集成仅使用Gitlab API令牌。

Once you provide the GitLab API token there, Jenkins uses it for all its interactions with GitLab, no matter which specific job is running.


However, when you're setting up a specific Jenkins job, you need to specify how that job should interact with the source control repository, in this case, GitLab.
This is where you would normally specify the repository URL and credentials (username and password or SSH key). These credentials are used by the job to clone the repository, fetch updates, and push changes if necessary.

I would recommend using an SSH key that, once created (ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -P "" -f ~/.ssh/gitlab, without passphrase for testing), you can copy the public key (~/.ssh/gitlab.pub) to your GitLab Settings/SSH keys page.

In the "Key" box, paste your key. If you created the key with a different comment, the "Title" field may autofill with that comment. If not, give your key an identifiable title, like 'Jenkins Server'. Click Add key.

Back in Jenkins, when setting up the job, you can use the 'git' SCM and specify your repository URL in the form of git@gitlab.com:username/repo.git.
In the credentials' dropdown, you can add the SSH private key that corresponds to the public key you added in GitLab. Jenkins will use this SSH key to authenticate with GitLab.

This way, your Jenkins jobs will authenticate with GitLab using SSH keys, not username/password. If a user leaves the company, you can simply generate a new SSH key pair, add the public key to GitLab, and update the private key in Jenkins. Your existing jobs will continue to work without modification.


David suggests in the comments:

> Add a systemic user for this purpose, where the security team registers credentials in jenkins.
Then give the repositories permission to that user, and run via SCM with that credential.
>
> In this way, the systemic user will not leave the company and future maintenance of credentials will be the responsibility of the security team.

That is a good practice indeed, and why we add a "Build_Jenkins" credentials in Jenkins, and declare a technical Build_Jenkins user in GitLab, with read-only access to the relevant repositories.

huangapple
  • 本文由 发表于 2023年6月13日 05:38:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定