简体   繁体   English

Bitbucket webhook 运行 jenkins 作业

[英]Bitbucket webhook to run jenkins job

Update:更新:

I succesfuly sent a post request from curl to trigger jenkins job:我成功地从curl发送了一个 post 请求来触发 jenkins 工作:

curl -I -X POST http://jenkinsAccountUserName:jenkinsAccountPassword@JenkinsIp:8080/job/projecty_ci/build -H "Jenkins-Crumb:a4fb99fbdb252fda3cc69ee575bedabc"

I don't understand how to convert this to a url: Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request我不明白如何将其转换为 url: Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request . Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request

http://jenkinsAccountUserName:jenkinsAccountPassword@JenkinsIp:8080/job/projecty_ci/build?Jenkins-Crumb:a4fb99fbdb252fda3cc69ee575bedabc

this works fine from chrome but not from bitbucket webhooks.这适用于 chrome,但不适用于 bitbucket webhooks。

What is the problem?问题是什么?


I created a job in jenkins which I can successfuly trigger by url.我在 jenkins 创建了一个工作,我可以通过 url 成功触发。

When I'm triggering the same job from bitbucket's webhook, I get the error: Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request当我从 bitbucket 的 webhook 触发相同的作业时,我收到错误: Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request . Problem accessing /job/projecty_ci/build. Reason: No valid crumb was included in the request

在此处输入图像描述

在此处输入图像描述

Try to generate a CSRF token for use in your in your API requests.尝试生成一个 CSRF 令牌以在您的 API 请求中使用。

  • GOTO: Jenkins > Manage Jenkins > Configure Global Security and enable Prevent Cross Site Request Forgery exploits.转到:Jenkins > 管理 Jenkins > 配置全局安全并启用防止跨站点请求伪造攻击。
  • Select Default Crumb Issuer from Crumb Algorithm and save to apply changes and enable. Select Crumb Algorithm 的默认 Crumb Issuer 并保存以应用更改并启用。

Remote access API 远程访问 API

You can get the crumb by calling the jenkins api and using it in your URL.您可以通过调用 jenkins api 并在您的 URL 中使用来获取面包屑。

For curl/wget you can obtain the header needed in the request from the URL JENKINS_URL/crumbIssuer/api/xml (or.../api/json).对于 curl/wget,您可以从 URL JENKINS_URL/crumbIssuer/api/xml(或.../api/json)获取请求中所需的 header。 Something like this:像这样的东西:

wget -q --auth-no-challenge --user USERNAME --password PASSWORD --output-document - \
'JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'

This will print something like ".crumb:1234abcd", which you should add to the subsequent request.这将打印类似“.crumb:1234abcd”的内容,您应该将其添加到后续请求中。


Administer a Build管理构建

NOTE: To prevent CSRF, Jenkins require POST requests to include a crumb, which is specific to each user.注意:为了防止 CSRF,Jenkins 要求 POST 请求包含特定于每个用户的碎屑。 The command to obtain the crumb is:获取面包屑的命令是:

SERVER=http://localhost:8080
    CRUMB=$(curl --user $USER:$APITOKEN \
        $SERVER/crumbIssuer/api/xml?xpath=concat\(//crumbRequestField,%22:%22,//crumb\)) 

Start a build开始构建

$ curl -H ".crumb:<crumb_string>" -X POST http://<jenkins_url>/job/<job_name>/build --user <user_name>:<api_token>

I may not be directly solving your issue here but wanted to share that we have managed to trigger a Jenkins job successfully by following the instructions on https://support.cloudbees.com/hc/en-us/articles/226568007-How-to-Trigger-Non-Multibranch-Jobs-from-BitBucket-Server-我可能不会在这里直接解决您的问题,但想分享一下,我们已经按照https://support.cloudbees.com/hc/en-us/articles/226568007-How-上的说明成功触发了 Jenkins 作业to-Trigger-Non-Multibranch-Jobs-from-BitBucket-Server-

The plugin that we use on BitBucket server is https://marketplace.atlassian.com/plugins/com.nerdwin15.stash-stash-webhook-jenkins/server/overview我们在 BitBucket 服务器上使用的插件是https://marketplace.atlassian.com/plugins/com.nerdwin15.stash-stash-webhook-jenkins/server/overview

I am a bit late here, I was facing the same problem and I configured the webhook URL like this below to work for me:我在这里有点晚了,我遇到了同样的问题,我像下面这样配置了 webhook URL 来为我工作:

http://jenkins-username:jenkins-password@jenkins-url:5555/job/job-name/build?crumb=crumb_token . http://jenkins-username:jenkins-password@jenkins-url:5555/job/job-name/build?crumb=crumb_token

Ref Question: How to pass crumb info via bitbucket-hook to jenkins?参考问题: 如何通过 bitbucket-hook 将 crumb 信息传递给 jenkins?

Hope it helps!希望能帮助到你!

With my Jenkins version 2.365 , and with the help of previous answers, I have created the request as follow, and it worked fine.使用我的 Jenkins 版本2.365 ,并在以前的答案的帮助下,我创建了如下请求,并且工作正常。

The URL you put in Bitbucket web hook URL field will be as follow:您放入 Bitbucket web 钩子 ZE6B391A8D2C4D45902A23A8B658570 中的 URL 将作为字段跟随

http://<jenkins-user>:API_TOKEN@<jenkins-url>:<jenkins-port>/job/<jenkins-job-id>/build

Generate API token from Jenkins从 Jenkins 生成 API 令牌

  • In Jenkins click on the user name upper right -> Configure在 Jenkins 点击右上角的用户名 -> 配置

  • Go to API Token section and generate API_TOKEN as shown below Go 到 API Token 部分并生成 API_TOKEN 如下图在此处输入图像描述

  • Copy this key, you need to use it in the request (replace in API_TOKEN field above)复制这个key,需要在请求中使用(在上面的API_TOKEN字段中替换)

I found this error differently.我发现这个错误不同。

Scenario:设想:

I have bitbucket repo which actually trigger jenkins pipeline in case of push, pull, Pull request creation and Pull Request merge.我有 bitbucket 回购协议,它在推送、拉取、创建拉取请求和拉取请求合并时实际触发 jenkins 管道。

Solution First step i created Personal Access Token in Jenkins Configure -> API Token (Create New API Token)解决方案第一步我在 Jenkins 配置中创建了个人访问令牌 -> API 令牌(创建新的 API 令牌)

Now goto Bitbucket Repository现在转到 Bitbucket 存储库

Repository Settings -> Webhooks -> Repository hooks (Create a new Hook) Repository Settings -> Webhooks -> Repository hooks(创建一个新的挂钩)

In the Hook we need to pass Jenkins Personal Access Token and along with Username在 Hook 中,我们需要传递 Jenkins Personal Access Token 以及用户名

Below is the sample URL.下面是示例 URL。

http://JenkinsUser:JenkinsPersonalAccessToken@Jenkins.localhost.com:8080/job/dockerapi-pipeline/build?token=PipelineToken http://JenkinsUser:JenkinsPersonalAccessToken@Jenkins.localhost.com:8080/job/dockerapi-pipeline/build?token=PipelineToken

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

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