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

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.

Related

Jenkins being run on all branches in repo instead of only the branch changed

I am pretty new to Jenkins, and when I make a pull request, the Github webhook triggers a rebuild on all branches in the repo. However, I only want the branch associated with the pull request to be built. I suspect that all branches are built due to nothing specified in the "Branch specifier."
Is there a way to specify to build only the branch the pull request was made to?
Thanks!
To resolve your issue try to use multibranch pipeline job or use filter in simple pipeline job.
To use multibranch pipeline job you need to install Pipeline: Multibranch plugin.
Pipeline job example
In simple pipeline job choose "Build when a change is pushed to GitLab" in "Build trigger" section -> Advanced -> Filter branches by name

How to get the Generic Webhook Trigger Plugin to work with multibranch pipelines in Jenkins?

I'm trying to set up a scenario where a pull request is created on github that triggers a Jenkins multibranch pipeline, and where that multibranch pipeline uses the Generic Webhook Plugin to extract values from the POST request sent from github to jenkins to be used in the script.
Unfortunately, as described on the Generic Webhook Trigger Plugin wiki:
Note: When configuring from pipeline, that pipeline needs to run once, to apply the plugin trigger config, and after that this plugin will be able to trigger the job. This is how Jenkins works, not something implemented in this plugin. You can avoid this by using Job DSL and have Job DSL create pipeline jobs with the plugin configured in that DSL.
This would be OK using a normal pipeline since it would just be a one off on creation of the Jenkins job. The problem however is that a multibranch pipeline will create a new job whenever a new branch/PR is created, and that means that for each pull request I create on github (which triggers my multibranch pipeline script), I have to then run it twice to get the generic webhook functionality working. Having to resubmit for each PR would be tedious for long-run projects.
It seems to me like there are two possible approaches to solving/improving on this problem. One is to try and play around with DSL Jobs (as suggested by the wiki); but I tried this and couldn't get it to work (it was adding a huge amount of complexity to the set up, so I've abandoned it for now).
The second possible solution is as follows: when a PR is created in github, the Generic Webhook will cause a new job to be created in the multibranch pipeline corresponding to that PR; the first time the multibranch pipeline runs the first build of this newly created job will fail for the reason given in the quote above; but then a solution might involve testing that the first job failed and somehow telling Jenkins to try rebuilding for that job again.
So my question relates to this second approach: how can I most neatly run a rebuild for this multibranch pipeline upon the creation of a PR on github?
Any advice/suggestions would be appreciated!
For triggering multibranch pipline by webhook you can use this plugin:
"Multibranch Scan Webhook Trigger"
https://plugins.jenkins.io/multibranch-scan-webhook-trigger/
Actually that is not true for multibranch pipelines. Just ordinary pipelines needs to run twice.
I updated the docs like:
When configuring from pipeline (not multibranch pipeline)...

Trigger Jenkins Multibranch Pipeline Scan from another job

Is it possible to trigger Scan Multibranch Pipeline Now action for a Jenkins Multibranch Pipeline from another Job? I have certain issues integrating my old version of Gitlab with Pipeline Jobs in Jenkins and came upon such a workaround.
Alternatively, would it be possible to trigger Scan Multibranch Pipeline Now with a notifyCommit web hook?
The purpose of this would be to Scan Multibranch Pipeline whenever a change occurs in the Git repo, so that new branches are detected after such a change.
Another purpose, which I am unable to achieve otherwise (except for periodic polling per branch which is plain evil to me), is to trigger build of branches affected by given changeset. I use old GitLab (7.8) which does not work with the ordinary Gitlab plugin and the Gitlab Hook plugin does not work with pipeline jobs, so my workaround would be to have an non-Pipeline Job to be triggered by Gitlab Hook plugin and this job would in turn trigger Scan Multibranch Pipeline.
Turned out that simple notifyCommit web hook did the job

How to trigger Multibranch Pipeline build with github webhook

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!

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