Jenkins, Build when a change is pushed to BitBucket, multiple job - jenkins

I have a problem with "Build when a change is pushed to BitBucket" setup on my Jenkins.
So I've configured two repositories, to start their own pipelines if someone push a commit and the push part is working, but when I create a push on repo1, it will start repo1_pipeline and repo2 pipeline.
Structure:
repo1 > repo1_pipeline
repo2 > repo2_pipeline
My url on bitbucket side (both cases): https://jenkins-url.com/bitbucket-hook/
Jenkins side:
Jenkins setup
Someone one can u explain this for me?
Thank you,

By default the Plugin will loosely match the Repository and the Job based on the SCM configurations you have. Following is from the plugin document.
The "loose matching" is based on the host name and paths of the
projects matching.
You can try specifying your full repository URL as the Override Repository URL or Path.
As an alternative, you can consider using the Generic Webhook Trigger Plugin. You can read this answer for more details.

Related

How to build Jenkins after each push in any branch?

Is there any way to make Jenkins builds after each commit in any branch ?
Because i found in my project's configuration that Jenkins run build only after detecting commits in specific branch or in the default ( eq to master in git ).
PS: i'm using mercurial and Jenkins file.
Should i change project type ( new item type in Jenkins ) or are there any modifications in configs.
There are two things that you should check for this (I haven't work with Mercurial)
Does Mercurial has the option to create webhooks?
There is a jenkins plugin for Mercurial? (I think there is)
You must configure on the mercurial site the webhook pointing to Jenkins and give the point to the job you want to run, and on which events does it will fire. On the Jenkins side you must configure on the job who it will behave.
For example, with GitLab, the plugin has an option configured on the "Build Trigger" section where you configure the events and the branches that fires the job. In GitLab, in the repository you create the webhook, that is only a URL pointing to the Jenkins job.
I got this solution and it worked for me.
with Mercurial, we can use the "tip" keyword.The tip revision is the most recent changeset in the repository. It is the most recently changed head.

Jenkins Gitlab integration - build on pushes to master branch only

I have the same question as this one:
Trigger build only on pushes to master
Only the answer there is not correct. Does anybody know a solution? What sense does it even make to build a certain branch on pushes to any other branch than itself? What I mean to say is that this should really be a very common thing...
In the advanced section of Build Triggers, there's this section, that really looks like the thing:
However, it doesn't work for me. Like this, it does not trigger the build at all. I also tried putting there just master (without the asterisk) but that doesn't help.
If I select the first option (Allow all branches...) then it works, but obviously all the pushes to any branch triggers the build on master which makes no sense.
Any help appreciated.
Thanks!
To integrate GitLab with Jenkins I'd suggest using Gitlab Plugin
Install Gitlab plugin on Jenkins and configure Gitlab Connection (Manage Jenkins -> Configure -> Gitlab), for this you need to create new credentials of type GitLab API token.
Select trigger in Jenkins job configuration Build when a change is pushed to GitLab (when you click on advanced options there is Secret token which should be used together with webhook URL in Gitlab Webhooks).
Add webhook (Push Events) in Gitlab project Settings -> Integrations, with url and token from Jenkins job and filter them by branch name.
Update: So my original point was to filter push events on GitLab so Jenkins doesn't need to do it but as you pointed out it should work with Jenkins filter as well.
I tried filtering branch in my job and Jenkins displayed dropdown list with suggested branches after I started typing, then when I choose brach a label appeared Matching 1 branch:
On the other hand in your screenshot there is some ERROR indicator which would suggest that your GitLab connection is improperly configured. I'd suggest to go to test your connection to GitLab and fix it.

how can i push code in to jenkins and it will automatically deploy code in to github in it possible?

I am new to jenkin's but what i am looking for is as below
1.How to create project or how to push your code in to jenkin's?
2.How it will directly push this code into github's branch?
I have followed some tutorial but nothing is clear.
Please help me.
Jenkins is not a repository, Jenkins is just a CI tool, basically build server, you cannot "store" codes in Jenkins, but yes, it is able to pull codes from repositories and eg. build it. (or push artifact- war/jar/ whatever to some machine via ssh eg.- look for publish over ssh plugin for this)
Create eg. freestyle project, and then take a look into "Source code management" section, there you can choose git, github, svn, whatever.. Will fill credentials and repo URL, then in "Build" step you will choose what you need to do.
For build check this
If you really mean how to push the changes back to repository, than I have found this- its done with git plugin.
I am not sure, but either you didnt understood what the jenkins is for, or you are using not proper name convetions. Hope that helps.
We found this solution works
It uses Add post-build action ⇒ Git Publisher

How to trigger a jenkins pipeline from a bitbucket repository

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.
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
I was struggling with the same problem. Following are the key points I followed.
In Jenkins pipeline job,
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,
Go to Settings > Services
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. :)
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.
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
Spend hours figuring out how to do this in 2017.10
Like #JPLemelin described, new a Jenkins item using a Bitbucket Team/project
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.
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
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.
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.

Github Jenkins plugin with Github Enterprise

I have a Jenkins server with the following github plugins:
github-api
github
github-pull-request
I want to trigger a build after a merge (from a PR). This is because I have some build chains that I don't execute when building a PR so after a merge I need to create new deployment packages.
In the enterprise github there doesn't seem to be the same interface (as regular github) for webhooks I don't seem to be able define what events the github plugin (webhook) should listen for so I think its just the default 'push' event. Is this done somewhere else or is it a limitation of the enterprise version?
UPDATE
I have the github plugin working now but its builds for every push if I check the box. This disucssion https://groups.google.com/forum/#!topic/jenkinsci-users/gew5gWVDxw8 suggests that I should be able to use the git scm and github plugin together. Github plugin will register a change, scm plugin will poll server to determine if this change was tho the watched branch. I also tried this suggestion How can I make Jenkins CI with git trigger on pushes to master?
When I try and configure this no branch is ever built!
At least for me it's not exactly clear from your question what you actually exactly want.
It sounds like you only want builds to be triggered if there are some new commits on github on a specific branch.
If this is all you're looking for, you can just setup a webhook (Jenkins (Github Plugin)).
In you Jenkins job, just specify the branch you're tracking (Branches to build).
Assuming you setup github's hoock to jenkins correctly, this should trigger your build job on each commit on github enterprise.
If your hook doesn't seem to work: To check if and what your github server actually would send to your jenkins server, you can temporarily replace the url to your jenkins server in github's webhook with something like requestb.in (this will let you inspect github's hook payload)

Resources