How to trigger a jenkins pipeline from a bitbucket repository

流过昼夜 提交于 2019-12-04 15:16:57

问题


I have successfully setup a webhook trigger in bitbucket for a Jenkins freestyle project, for test purposes.

Unfortunately my Jenkins project is using the Pipeline format, and I am unable to get Bitbucket to trigger that kind of project; the problem seems to be that there is no Jenkins project registered to pull from the repo that the Bitbucket webhook is coming from, and Jenkins replies with:

Error: Jenkins response: No git jobs using repository: ssh://git@myhost:7999/xxx/testing-jenkins.git and branches: master No Git consumers using SCM API plugin for: ssh://git@myhost:7999/xxx/testing-jenkins.git

The pipeline project is setup in a way that the Jenkinsfile is to be found in the given repository (ssh://git@myhost:7999/xxx/testing-jenkins.git), by using the "Pipeline script from SCM" option.

Therefore there is actually a kind of "git consumer" for the Pipeline, but this does not seem to be taken into account by Jenkins, probably because this is not a real project source, but a pipeline source.

Are there any examples of integration of Bitbucket and Jenkins Pipeline projects? I have been unable to find any.


回答1:


If your are looking for a full Bitbucket and Jenkins Pipeline, I highly recommend to use the Bitbucket Branch Source Plugin. The plugin will discover all Branches and Pull Requests and build all who have a JenkinsFile in the root of repo.

You can also use create a project as Bitbucket Team, who will scan all repo of your organization:

See the official doc of CloudBees




回答2:


I was struggling with the same problem. Following are the key points I followed.

In Jenkins pipeline job,

  1. Under Build Triggers, check 'Trigger builds remotely (e.g., from scripts)' and fill in the 'Authentication Token' with some random and unique token.

In BitBucket repository,

  1. Go to Settings > Services

  2. Select 'Jenkins' from the drop down and 'Add service'.

    • Check 'Csrf Enabled'

    • Endpoint : http://username:apitoken@yourjenkinsurl.com/

      You can find username and apitoken at Jenkins home > People Select the user and click on configure. Under 'API Token' click on the 'Show API Token' button and you see the username and apitoken

    • Module name : This is optional. It can be any particular file or folder which is to be watched.

    • Project name : The project name in Jenkins.

      If the job is in some folder structure, say I have 'MyTestFolder/MyTestPipelineJob', Project name to be mentioned is 'MyTestFolder/job/MyTestPipelineJob'

    • Token : 'Authentication Token' created in Jenkins job.

You are ready to go!!

I referred http://felixleong.com/blog/2012/02/hooking-bitbucket-up-with-jenkins/ and some of my instincts. :)




回答3:


I have this same issue. My workaround was just to create a freestyle project that can be triggered by the WebHook, and have the the Pipeline triggered by that project's completion.

In the mean time, here's the Jenkins bug you can watch for a fix: https://issues.jenkins-ci.org/browse/JENKINS-38447




回答4:


A simple solution is to use Generic Webhook Trigger Plugin in Jenkins.

You would need to

  • Enable it in a free style or pipeline job.
  • Configure a token string
  • Construct JSONPath:s to gather whatever you need from the Bitbucket Webhook.
  • Add the plugin endpoint in Bitbucket. JENKINS_URL/generic-webhook-trigger/invoke?token=whatever_you_picked

The plugin will give you clear feedback when it is invoked so that troubleshooting is made easy.

It is up to you to pick whatever values you need from the webhook in order to clone the correct repository or whatever it is you want to do when the it is invoked.




回答5:


Spend hours figuring out how to do this in 2017.10

  1. Like @JPLemelin described, new a Jenkins item using a Bitbucket Team/project
  2. ref to the doc: https://support.cloudbees.com/hc/en-us/articles/115000051132-How-to-Trigger-Multibranch-Jobs-from-BitBucket-Cloud- , install the plugin: The BitBucket Branch Source plugin.
  3. go to bitbucket, and add webhook: ${your-jenkins-url}/bitbucket-scmsource-hook/notify

after these 3 steps, I finally make the pipeline jobs run after new commit into bitbucket




回答6:


I had the same exact issue...

The cause was using */master for branch specifier. I needed to spell it out: origin/master (no wildcards).

It works well now.




回答7:


I was finally able to make this work with Jenkinsfile in Multi Branch Pipeline:

In Bitbucket i created a webhook with my Jenkins-URL, my clone-URL and in the webhook i put the following URL (exact the url in the project of Jenkins):

http://<jenkins>/git/notifyCommit?url=http://<user>@<bitbucket>/scm/<project>/<repo>.git

When i test the trigger the result is the following:

No git jobs using repository: http://<user>@<bitbucket>/scm/<project>/<repo>.git and branches: 
Scheduled indexing of <repo>

So it didn't trigger any jobs, but it triggered the multi branch scanning, so my changed branches are build.



来源:https://stackoverflow.com/questions/39768919/how-to-trigger-a-jenkins-pipeline-from-a-bitbucket-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!