Polling from multiple repositories in Jenkins - jenkins

I couldn't find an answer for my question on stack, sorry if it is a duplicate.
Let's say I have repo A on Github, where my Jenkinsfile is, and repo B on Github, where all my source code is.
For my job, I've set 'Pipeline script from SCM' from repo 'A', added 'Polling ignores commit in certain paths' with included region containing only this Jenkinsfile.
In Jenkinsfile, I'm using checkout plugin to pull repo 'B' with added PathRestriction extension.
Also I've added pollSCM to 'triggers' with value 'H/5 * * * *'.
But whenever I commit something to repo 'A' without any changes to this Jenkinsfile, build is triggered anyway.
My question is: How can I setup my build to trigger only, when Jenkinsfile in repo 'A' or my source code is changed?

There are two ways to trigger a build only when specific file changes.
 1. script hook: On git server side, create a hook that will check if the last commit changed the Jenkinsfile, if true, send a post to jenkins job configured with 'Trigger build remotely'.
 2. Commit: Create a repo only with the Jenkinsfile, so every commit on this repo will be a change on Jenkinsfile.
I strong advise you to change your approach and use Jenkins shared library, this is a much better way to share and manager pipeline across multiple projects / jobs

Related

Jenkins respect HEAD or default branch when cloning repo to workspace

Jenkins provides lengthy documentation regarding how to specify which branches it will build from. https://plugins.jenkins.io/git/
None of that though answers this specific question, or if it does, I cannot seem to achieve this.
How to specify that Jenkins will use whichever branch is normally checked out when cloning, such as the default branch specified in Github
Does this even make sense or is this even possible?
EDIT: specifically what should go in the red circle to checkout the default branch in Github
Jenkins respects the default branch over HEAD. Also, it depends how you configure you Jenkins job if it's a mulit-branch pipeline then you don't need to specify which branch it should build, by default it will scan you GitHub repo and build the changes made on branch and it will trigger the build.
If it's pipeline job then you can specify which branch it should built by editing the job configuration on Jenkins job and specifying the particular branch name in the section Branches to build
I hope this is what you are looking for.

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.

When Multibranch pipeline when job is trigged, what is stored in Jenkins workspace?

I am not sure if I am asking the question in right way.
When I am triggering the multi branch pipeline job and I do "checkout scm" in Jenkinsfile, does it checkout the pull request or Master + pull request?
It depends of your settings.
As I remember, multibranch pipeline builds pull requests in separated view and it can build as PR source branch and result of PR. It cloned your repo, checkouts target branch, merges source branch into it and runs your stages.
If you look at the logs you will see that it checks out the hash of the commit
example: git checkout 1fb651dfdcbea276627de0247bbf5047840cae4c
As for workspace, if you log in to the Jenkins machine where this is running you will see that each branch has a separated workspace. Here you will find the source code of the checked out branch. The separated workspace ensures that there is no conflict when working with multiple concurrent branch builds.

Jenkins Multi-Branch Project Plugin select two branches

I want to use the Jenkins Multi-Branch Project Plugin to select just two Git branches to build consistently. My branches are named development and SIT. I cannot guarantee the names of any other branch. In the downstream job created, I would like the branch name in the Git configuration to be:
*/development or */SIT
so that the Github trigger can work. There may be another configuration here that can make the Github trigger work, but this is the one I use for now.
I have tried:
:(development|SIT), (development|SIT), development,SIT development|SIT
all to no avail.

Jenkins and GitLab: How to setup SCM aware job which is not triggered by the hook?

To give some context the question is about GitLab and Jenkins setup.
I know how to setup a web hook, I know how to setup a job to be triggered by the hook. The problem is that I need to have multiple jobs and only a single entry-point (parent job) trigger for them.
The downstream jobs at the same time need to be git repo aware so I have to set repo url for them. This causes them to be triggered independently by the hook and I don't want that as this means that they are triggered twice.
On the other hand if I don't configure repo url on a downstream job and the parent job triggers it, it fails as it is not able to do a checkout.
I may try to hack around with some 'execute shell' build step, I believe it's not a valid way to go. Has anybody a good tip how to solve that?
For the reference here is the GitLab Jenkins plugin documentation according to which:
Plugin will parse the GitLab payload and extract the branch for which
the commit is being pushed and changes made. It will then scan all Git
projects in Jenkins and start the build for those that:
match url of the GitLab repo
match the configured refspec pattern if any
and match committed GitLab branch
I tried playing around with different settings, without a great result though.
For the project you want to get only local triggers, just enable Don't trigger a built on commit notification in the Additional behaviours of git plugin.
(https://github.com/elvanja/jenkins-gitlab-hook-plugin/issues/11#issuecomment-35385032, as you actually have discovered).
But a better solution could be to make your downstream jobs reference the repository locally cloned by main job (not sure if actually possible), so the plugin will never consider them for schedule a build, as the git url don't match.

Resources