首页 > 解决方案 > 如何使用其他用户的 ssh 密钥从 jenkins 推送到 master 分支

问题描述

我不知道这是否可以做到,但我需要在 jenkins 作业中将更改推送到 bitbucket 主分支存储库。让我们称之为 repo1 该存储库的主分支已配置分支权限以仅推送 2 个用户。用户 a 和用户 b。此用户使用用户名和密码连接到 bitbucket。同样从此 user_a 和用户 jenkins 已在 jenkins 设置凭据中配置,他们的 SSH 密钥以连接到 bitbucket。所以基本上当 Jenkins 需要从 BB 提取代码并构建一些应用程序时,使用这个凭证 ID。

但是现在,要求的新工作之一必须让 jenkins 可以推送到这个 repo 1 master 分支。当然,我首先收到了权限错误,因为只有 user_a 和 user_b 可以推送到 master。

所以我现在尝试配置 jenkis 使用 user_a 的凭据 ID(有权进入 master 分支推送)

但是当我运行这项工作时,我收到了令人沮丧的消息

 git commit -m test
[master c078d06] gulp file generated
 Committer: Jenkins <jenkins@xxxx@xxxxx.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
[Pipeline] sh
[workspace] Running shell script
+ git push origin master
remote: Permission denied to update branch master.To git@bitbucket.org:xxxxxx/repo1.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@bitbucket.org:xxxxxxx/repo1.git'

我使用 user_a 凭据 ID 配置此步骤如下。

stage ('Pulling code from Bitbucket') {


        git branch: 'master',
                credentialsId: '2bbbbbbb-aaaaaaaa--xxxx-eeeeeeeeeee', #user_a credential ID
                url: 'git@bitbucket.org:xxxxxx/repo1.git'

            sh 'sed -i "s/running/walking/g" README.md' #just changing something in readme for test
            sh 'git add .'
            sh 'git commit -m "test"'
            sh 'git push '


        }

那么我怎样才能做到这一点呢?我告诉 jenkins 使用 user_a 凭据 ID,但不工作。
我也不能在 repo1 主分支的 bitbucket 中添加用户 jenkins,因为它没有任何用户和密码,jenkins 只是使用 ssh 密钥连接到 bitbucket。与 user_a 相同的方式(也有用户和通行证)。如何使用 user_a 凭证 ID 推送到 master,即在 master 分支中具有权限的用户?显然我尝试的方法没有奏效。谢谢。-

标签: gitjenkinsbitbucket

解决方案


推荐阅读