繁体   English   中英

如何更改用户使用 jenkins 在 Bitbucket 中发出拉取请求

[英]How to change the user to make pull requests in Bitbucket using jenkins

  • 我有一个管道可以创建两个新分支并将它们推送到 BitBucket,然后创建一个新的拉取请求。
  • jenkins 管道在 linux 从站上运行。
  • 我已经在 jenkins 凭据中将凭据设置为 bitbucket 回购。

到目前为止,创建新分支并将它们推送到 BitBucket 可以很好地使用我的凭据。

sh script: "git init"
sh script: "git add *"
sh script: 'git commit -m "new branch created"'
sh script: "git checkout -b ${new_branch}"
withCredentials([[$class: 'UsernamePasswordMultiBinding',
                   credentialsId: 'mycred',
                   usernameVariable: 'username',
                   passwordVariable: 'pass']]){
sh('git push https://${username}:${pass}@bitbucket.example.com:8080/scm/user/reponame ${new_branch})

像这样,我创建并推送了两个新分支 ${new_branch}、${new_branch2}。 即使只有我的凭据mycred可以访问回购协议,两个分支也会被云用户推送。 在提交部分它显示“云用户”

然后我尝试创建一个从 ${new_branch} 到 ${new_branch2} 的拉取请求。

当我尝试使用 curl 使用我的凭据创建拉取请求时,它说

{"errors":[{"context": null,"message":"Authentication failed. please check your credentials and try again.","exceptionName":"com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"}]}

我的 curl 命令使用 bitbucket api 是,

withCredentials([[$class: 'UsernamePasswordMultiBinding',
                   credentialsId: 'mycred',
                   usernameVariable: 'username',
                   passwordVariable: 'pass']]){
sh """curl --insecure -X POST https://${username}:${pass}@bitbucket.example.com:8080/rest/api/1.0/repos/myrepo/pull-request?create\
-H 'Content-Type:application/jason'\
-u ${username}:${pass}\
-d '{"title":"testpullreq",\
        "description":"sampledescription",\
        "state":"open",\
        "open":true,\
        "closed":false,\
        "fromRef":{\
            "id":"refs/heads/${new_branch}",\
            "repository":{\
            "slug":"branch-to-pr",\
            "name":"branch-To-PR",\
            }\
         },\
        "toRef":{\
           "id":"refs/heads/${new_branch2}",\
            "repository":{\
            "slug":"branch-to-pr2",\
            "name":"branch-To-PR2",\
           }\
       },\
       "locked":false,\
       "reviewers":[\
                   {\
                   "user":{\
                   "name":"myemail@example.com"\
                    }\
                 }\
              ]\
         }' """

所以 curl 命令也可以正常工作,错误是它说我没有身份验证权限,那是因为 PR 试图使用我的奴隶来制作。 我需要使用我的用户帐户 (mycred) 执行此操作。

我假设您的问题发生是因为curl是在 Jenkins 主机中运行的命令。 因此,它在运行命令时使用主机中已经存在的凭据。

您可以考虑改用HttpRequest插件。 该插件允许您使用指定的凭据发送带有正文的发布请求。 不同之处在于不使用系统命令,因此命令的发送独立于主机中已有的凭据。 这应该会强制 Jenkins 使用您的mycred凭据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM