How to trigger Multibranch Pipeline build with github webhook - jenkins

In freestyle job there is an option named "GitHub hook trigger for GITScm polling" on stage Build Trigger.
screen capture here:
.
Together with webhook in gitlab config "http://myjenkins/gitlab/notify_commit" it works fine, meaning that the build will be triggered automatically when something is pushed to the repository.
But why in Multibranch Pipeline there is only one option named "Periodically if not otherwise run"? Is there some plug-in not installed? How to trigger Multibranch Pipeline build with github webhook like freestyle job

This page described how to configure pipeline-as-code on multibranch workflow in jenkins.
This is the quote from the description inside:
The Workflow Multibranch feature (provided by the workflow plugin) provides the following key abilities:
Automatic Workflow (job) creation in Jenkins per new branch in the
repo (assuming webhooks are registered from GH to Jenkins).
Build specific to that child-branch and its unique scm change and build history.
Automatic job pruning/deletion for branches deleted from the repository, according to the settings.
Flexibility to individually configure branch properties, by overriding the parent properties, if required.
To configure the webhooks, refer to this page.
To check if the events notify your jenkins you can use this feature below:
PS: Watch the URL target that you configured, if you miss the "/" at the end of url it might not be able to reach the jenkins.
I hope this helps!

Related

How to retrigger GitHub PR build in Jenkins multibranch pipeline with comment?

I'm evaluating multibranch pipelines. I created a repo, with a jenkinsfile. The branches were detected, builds were triggered on PR.
Now I'd like to rebuild on command via a comment on the GitHub PR, I installed the Multibranch Scan Webhook Trigger Plugin and for now set the regex to .*.
When commenting, in the jenkins log, I get 2 lines:
2019-11-25 16:30:37.128+0000 [id=1503] INFO c.i.j.p.m.ComputedFolderWebHookRequestReceiver#doInvoke: Triggering FMS
2019-11-25 16:30:39.712+0000 [id=1587] INFO j.b.MultiBranchProject$BranchIndexing#run: FMS #20191125.163037 branch indexing action completed: SUCCESS in 2.5 sec
Great, it seems that the repository was scanned but it doesn't start a new build, I guess because it doesn't detect a modification.
Any idea how to do or if it's actually possible?
You can trigger a multibranch pipeline build in Jenkins by using the GitHub PR Comment Build Plugin:
This plugin listens for comments on pull requests and will trigger a
GitHub multibranch job if a comment body matches the configured value,
such as "rerun the build". This is implemented as a branch property on
multibranch jobs.
To enable this behavior, simply add one or more of the branch
properties from this plugin to the multibranch job and configure the
regular expression to match against the comment body.
You also need to configure the GitHub webhook to fire on the Pull Request comment, which it sounds like you've already done.

webhooks for jenkins multibranch pipeline jobs

I configured webhooks in my github repository with events "Send me everything.". If i create any new branch in repository, github is delivering the webhooks perfectly, but build is not triggered in jeknins job .
Configured jenkins multibranch pipeline job with this repository, but i am unable to find options like "GitHub hook trigger for GITScm polling" and "GitHub Pull Request Builder" like in freestyle/maven jobs. do i need to install any plugins? any help appreciated.
attaching screenshot of my job's configuration.
I went through similar queries in stackoverflow, but i didn't find any clue. all answers are based on freestyle/maven jobs.
Ensure your Jenkinsfile contains a check for "GitHub hook trigger for GITScm polling". This can be done by declaring the following in your Jenkinsfile:
pipeline {
triggers {
githubPush()
}
}
Link to documentation:
https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.triggers.TriggerContext.githubPush
You just have to add the below Url :
http://yourjenkins/project/*yourprojectname* on "Add Webhook" in git service provider.
No configuration required in Jenkins multibranch job it will automatically start the branch indexing.

Jenkins edit config in multi-branch pipeline

I cannot find a way to edit the configuration of multibranch pipeline projects. When I change to the branch level, there is only a "view configuration" menu item, as opposed to ordinary pipeline projects.
I am looking for a method to configure build triggers from outside Jenkins. My current workaround (define a pipeline per branch) is not feasible beyond initial testing.
You can edit the overall configuration of a multi-branch pipeline project, such as the repo it will read from and any credentials it will need, triggers for scanning the branch to check for changes (Note: not the same as build triggers),orphaned item strategy, build configuration, properties, and a few other things. This can be viewed by clicking "Configuration" on the page that shows all the branches, or it will say it is empty if there are none.
Please note this only applies to multibranch pipelines that are not part of an organizational folder (not a "Bitbucket Team/Project" or "GitHub Organization" type project.)
If you click a branch name (ex: master), clicking "View Configuration" will only let you view it. You have to edit the file defining that pipeline on the correct branch in order to change it. In the case of these screen shots, you need to edit the Jenkinsfile in the Test-Multibranch-Job repo on the master branch to edit the configuration.
To add a trigger mechanism, you could add triggers { gitlab(<config-here>) } to your Jenkinsfile if your repo is in Gitlab. Also note whether you are using a declarative pipeline or a scripted pipeline because the syntaxes are different.
NOTE: I am running Jenkins ver. 2.92
https://issues.jenkins-ci.org/browse/JENKINS-33900
"As designed, these jobs are view-only." -- Jesse Glick
you should be able to define the config in the pipeline itself, e.g. https://jenkins.io/doc/book/pipeline/syntax/#triggers
If you are trying to edit a Multibranch configuration, for example to configure Multibranch Action Triggers which have no Jenkinsfile configuration, you can go edit the configuration file directly on your server, in /var/lib/jenkins/jobs/NAMEOFPROJECT/config.xml

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.

Jenkins pipeline automatic branch detection

I am migrating my old Jenkins free-style job to multi-branch pipeline. I also want to use GitLab hook with them.
My problem is the branch detection. I am doing it manually but I want it to be automatic: when a new branch is pushed to git, GitLab trigger a Jenkins job that trigger the branch detection if the branch parameter from GitLab is not known for Jenkins at the moment. Is this possible to do it or doesn't this exist?
FYI: I tried to launch the multi-branch pipeline job but Jenkins says:
ERROR: No parameterized job named XXX found.
Enable "Build Periodcally" in your multibranch job configuration and the branch indexing will automatically started.
What you really need is a branch source plugin for GitLab with webhook integration, which is tracked as an RFE in JIRA.
Failing that, use a plain Git branch source and configure GitLab to send Jenkins notifications to /git/notifyCommit (IIRC) as documented on the Git plugin wiki. Need specify only a url, no other details. The branch indexing this triggers should both detect new or removed branches, and changes to the head of an existing branch, and schedule builds accordingly.
You can set webhook in GitLab for push events and URL like http://<yourserver>/git/notifyCommit?url=<URL of the Git repository>.
See https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository
GitLab notifies Jenkins on push events which should trigger branch detection also for multibranch pipeline.
I didn't receive the answer I wanted and I ran into this issue today that answered the question :
https://github.com/jenkinsci/gitlab-plugin/issues/298
TLDR: Multi-branch pipeline are not supported yet to be triggered by gitlab commit easily. There is a workaround. Look at the link above.

Resources