Jenkins skip automatic triggering for job - jenkins

I have a project which has multiple jenkins files and a jenkins with the bitbucket team plugin. The whole point is to separate staging from release.
When I push on bitbucket a change on a branch that both jobs monitor ( lets say master ) then both jobs will trigger as expected. I would like to disable this trigger on one of the jobs and force it to be manual only (lets say on job A which is the release. The jenkinsfile defined the release process but should be run only manually ) .
so far in the team configuration I cannot stop the trigger but neither in a jenkinsfile I can set a property ( this#ignorePostCommitHooks ) and make it work. ( I do setup it. But it does nothing ). Is there a way to configure the job triggering?
Also a side question, Jenkins offers this option
But even after setting it when I save the configurations and jenkins starts looking in the repository for matching projects / branches it will start building any branch it finds. Is there a way for this option to actually work?
Thank you in advance.

Wrapping it into an all match make it work.

Related

What is the difference between using the pollSCM and the Scan Multibranch Pipeline Triggers in Jenkins?

I've been using Jenkins and I've seen a lot of Pipeline examples (declarative ones) and I've seen some using the pollSCM property in the Jenkinsfile to trigger a build, like this:
triggers {
pollSCM('H/5 * * * *')
}
However, I've seen this Scan Multibranch Pipeline Triggers option when configuring a Multibranch pipeline. I'm not sure what's the difference between those.
All this problem came to me because I'm facing some cases where two builds are being triggered for the same job, and I thought it was because I have both these options configured.
Can anyone please help me understand this difference?
Thank you!
Scan Multibranch Pipeline Trigger
The ‘Scan Multibranch Pipeline’ trigger will scan the repository for new branches and changes in existing branches. By default it will trigger a new build for all branches which have been updated. However in the multibranch job configuration you can disable this automatic trigger for specific - or all - branches. However, please note that you have to set up the web hook for your repository so that Jenkins will be notified of any changes. As the hook setup will depend both on the Jenkins plugin used to checkout the Jenkinsfile as well as on the Git server, you will have to look this up accordingly.
Poll SCM
The pollSCM trigger is branch-specific. Within a Jenkinsfile you may configure different options for different branches. This option will never be able to trigger the very first build for a branch as it would need at least one build so the properties step gets executed and the pollSCM option set. That is: Any change here will only get effective AFTER the next build.
You can use pollSCM trigger in two ways:
Real polling. That is: Tell Jenkins to check the repository for changes in certain time intervals.
Waiting for notification by some repository hook. For this you'd need to
Keep the string passed to pollSCM empty:
triggers {
pollSCM('')
}
Set up a hook on your git server to notify Jenkins (See How to configure Git post commit hook)
Recommendation
Therefore I’d recommend to stick to the trigger based on the Multibranch branch scan - if possible. However in some special cases (e.g. if the first build on a new branch should never be built automatically) it still might be useful to use the poll SCM feature. In that case you might want to disable the automatic trigger as required.
Last but not least the poll SCM feature may use a different plugin than the Scan Multibranch Pipeline, e.g. for Bitbucket.
AFAIK for Bitbucket the multibranch trigger is little bit more flexible, allows to trigger a build on more events compared the the plain Bitbucket trigger.
I think pollSCM must be the jenkins plugin
https://wiki.jenkins.io/display/JENKINS/PollSCM+Plugin
Multibranch pipeline : This is the type of pipeline, where jenkins scan and pull from all the branch within the repository , so the build will trigger automatically when some code is checked in within the branch (if you have configured it)

Jenkins Multibranch Config: How to Filter branches based on variable string?

We have Jenkins set up with 7 multibranch pipeline projects, each building off the same git repo, but for different target platforms. Each of these multibranch pipelines builds a number of branches. We currently set which branches each multibranch pipeline builds by using the following property in the multibranch project configuration:
Branch Sources -> Git -> Behaviors -> Filter by name (with wildcards)
Currently, each multibranch pipeline has the same string of branches in this Filter by name (with wildcards) field. Each time we want Jenkins to start building a new branch, we go through all 7 multibranch project configurations and update this field to include the new branch.
It's a bit of a pain to go through each configuration and change this field every time, since we always want each configuration to have the same list of branches. Is it possible to simply use some type of a variable in this field? This way we only would need to change one location instead of trying to keep 7 different configurations in sync with each other, which is prone to error and also a bit of a pain.
Thanks for your help!
Allen
Rather than filtering with wildcards, you could try filtering branches with regular expression. In our case, pattern like:
(master|develop|release.*|feature.*|bugfix.*)
has been working well to cover the repository. That is, assuming that you follow Git Flow or similar methodology. Unfortunately, there is no simple way to sync the configuration between MultiBranch Pipelines build from one repository. Neither Multibranch Pipeline, nor Organization plugins are designed to work with Multiple Jenkinsfiles.
Also, you can try to sync only the branch configuration between Projects using Jenkins script console. Most of the Job configuration does not have to be set on Project level. For instance, you can create shared script (or shared library) to would be sourced by other jobs, to set the same job properties on each of them. See How do you load a groovy file and execute it for details.
if you want to use the wildcard you can provide like below:
In this example it will discover only qa and dev branch.
NOTE: You have to use "Discover branches" also with "filter by name (with wildcards)" behaviour.

jenkins build with bitbucket

I Have been working on bitbucket and jenkins for android applications. I am having many branches in my repository and i want to track just my master branch in jenkins where it meets the following criteria. 1) When we push any code with name 'A' into master it should automatically trigger a build.2) when we push a code as name 'B' into the same master branch it shouldn't trigger the build. Is there a way to do it. I tried excluding branch by using :^(?!.release).*$ but it is picking all other branches too.
Can anyone help?
You can specify which branch to be built in your job like this:
If you don't want the build to occur for specific codes then you can add them in to the Excluded Regions
Go to Additional Behaviors under Git in your job configuration and select Polling ignores commits in certain pathsand add the paths to the files for those you want to ignore builds if any changes happen to them:
This should work!

Should branch indexing for Multibranch Pipeline jobs be triggered automatically by webhooks?

I've set up a number of Multibranch Pipeline jobs in Jenkins (running 2.46.2 LTS, Branch API 2.0.8, GitHub Branch Source 2.0.5, and Pipeline Multibranch 2.14) and have just noted that branch indexing -- and thus any cleanup of old branches -- does not appear to be triggered by the webhook calls from GitHub. It only appears to be triggered if someone manually clicks the "Scan Repository Now" link, or if the job configuration in Jenkins is re-saved. I'm using the timestamp shown in the "Scan Repository Log" page as an indication of when the branch indexing occurs.
It seems that new branches or changes to existing ones are being detected correctly and built, so the webhooks from source control (GitHub) are working, but was surprised that this wasn't also triggering the branch indexing and thus the old branch cleanup. I just can't tell from the documentation whether this is correct and expected behavior or if something is incorrect in my setup.
I note that the help text for the "Periodically if not otherwise run" setting says:
Some kinds of folders are reindexed automatically and immediately upon receipt of an external event. For example, a multi-branch project will recheck its SCM repository for new or removed or modified branches when it receives an SCM change notification. (Push notification may be configured as per the SCM plugin used for each respective branch source.) Such notifications can occasionally be unreliable, however, or Jenkins might not even be running to receive them. In some cases no immediate notification is even possible, for example because Jenkins is behind a firewall and can only poll an external system.
This trigger allows for a periodic fallback, but when necessary. If no indexing has been performed in the specified interval, then an indexing will be scheduled. For example, in the case of a multi-branch project, if the source control system is not configured for push notification, set a short interval (most people will pick between 15 minutes and 1 hour). If the source control system is configured for push notification, set an interval that corresponds to the maximum acceptable delay in the event of a lost push notification as the last commit of the day. (Subsequent commits should trigger indexing anyway and result in the commit being picked up, so most people will pick between 4 hours and 1 day.)
This certainly implies that indexing of a Multibranch Pipeline job should be re-triggered by branch events (e.g., pushes from GitHub via webhook), but the timestamp on my indexing log seems to belie that.
So, is what I'm observing the intended behavior? If so, and I want a regular cleanup of old branches, do I need to select the "Periodically if not otherwise run" checkbox under "Scan repository triggers"? Or is there something wrong with my setup, which is preventing it from working as intended?
According to the official documentation:
By default, Jenkins will not automatically re-index the repository for branch additions or deletions (unless using an Organization Folder), so it is often useful to configure a Multibranch Pipeline to periodically re-index in the configuration.
I depend on "Periodically if not otherwise run" for 1) cleanup of branches and 2) creation of container jobs for brand new repos (i use "Bitbucket Team/Project", the bitbucket version of "Github Organization", which basically creates a multibranch pipeline for every repo in your organization). I have "Periodically if not otherwise run" set to run once a day for each project.
It does seem like these things could work via webhook, but they do not in my experience.

Multibranch Pipeline - configure branch projects

When I create a plain pipeline project I have the option to periodically poll the scm and if changes are detected, the build is run. that worked well for me.
Now I created a multibranch pipeline and added 2 branches. However, in the configuration I can not set the same as in the normal pipeline project, because it tells me I can only view the configurations of the sub-branch-projects.
Maybe I'm also doing it wrong, so I try to tell you what I actually want to achieve.
I have a PHP project inside of a Git repository. There are two branches that I want to be built on new commits (when pushed to the main repository)
The main repository resides on a self hosted version of Bitbucket Server. If possible, I want to avoid hooks and let Jenkins poll for changes on the bitbucket server. So how can I achieve that?
You just need to check the option Periodically if not otherwise run trigger at the multibranch level. This replaces per-job polling, because it also detects new branches and the like.
If I got you right, all you want to do is to build ONLY these 2 branches?
If so, under "Branch Sources" just click the "Advanced" and fill in the textfield "Include branches" your branches e.g. test test2 (note the space between the branch names)
Actually you'll have configure SCM pollig (the way you expect it) in the Jenkinsfile itself. It's the properties DSL elelement that handles the configuration:
properties([
pipelineTriggers([pollSCM('H 20 * * 1-5')])
])
Anyways I highly suggest to have a closer look at the Pipeline Snippet Generator:
<your-jenkins-url>/pipeline-syntax/
it s easy to miss but extremely helpful and it s populated based on your currently installed plugins. There you will also find a comprehensible set of options available for properties

Resources