Jenkins scripted pipeline to trigger job only when commits happen in Github - jenkins

We use CloudBees-2.138.2.2 Jenkins and have had lot of challenges to trigger jenkins jobs only based on commits in our Github repo. I am still looking for the exact working script for scripted pipeline and not declarative one.
so For example :- pipelineTriggers([cron('0-59/2 * * * *')]), works
but pipelineTriggers([pollSCM('0-59/2 * * * *')]), never works despite new commits

was able to make it work using :-
pipelineTriggers([pollSCM('* * * * *')]) under properties
This way the job polls the repo every minute and will trigger build only when it detects a new commit.

You can try adding the following in your scripted pipeline
properties([pipelineTriggers([githubPush()])])

Related

commit based job using declarative pipeline

I am new Jenkins. I was able to configure commit-based job trigger using Freestyle job. This way, any new commit to GitHub was triggering the given job.
But when it comes to pipeline job, I am not able to achieve the same. Please help regarding the same.
In Build Triggers section of pipeline, I have enabled GitHub hook trigger for GITScm polling.
pipeline{
agent {
node 'npm-linux'
}
options {
timeout(time: 15, unit: 'MINUTES')
disableConcurrentBuilds()
}
stages {
stage('build') {
steps {
sh 'git clone link'
sh 'mvn clean install'
}
}
}
}
As you're able to successfully see the freestyle job getting triggered on commit, we know for sure that GitHub has been configured correctly. Now, to fix the declarative pipeline issue, you have to make use of triggers in your pipeline code.
For example,
pipeline {
agent any
triggers {
// Instead of '* * * * *', you may use 'H/2 * * * *' which will check for source code changes every two minutes
pollSCM '* * * * *'
}
stages {
stage ('Echo') {
steps {
echo 'Hello, World!'
}
}
}
}
Note:
In Build Triggers section of your job configuration, you still have to keep the check box enabled for GitHub hook trigger for GITScm polling
Even after the above setting, the declarative pipeline job did not trigger automatically when i pushed my commit. When i ran the job manually once, thereafter things went fine. All subsequent commits triggered the pipeline job. So, just manually trigger the job once and things should be fine.
Also remember that polling is resource intensive so it is generally not a good idea to use it. However, in this case, where you are using GitHub, i'm not sure whether post-commit functionality can be configured using any way other than GitHub Webhooks. And sadly, just with GitHub Webhooks enabled, pipeline job wasn't getting triggered without the help of triggers directive.

Run nightly jobs on multibranch pipeline with declarative Jenkinsfile

Jenkins version 2.121.2
I have a multi-branch pipeline set up. I am using a declarative Jenkinsfile.
I have a set of tests which take a long time to run. I want these to run over night for any branches which have changes.
I have tried a few things but my current failing attempt is:
Under the job > configure, I have enabled 'Suppress automatic SCM triggering'
Have 'Scan Multibranch Pipeline Triggers' > 'Periodically if not otherwise run' set to 1 minute (just for testing, I will increase this when it is working)
In my Jenkinsfile (example for a 4am run), I have also tried with pollSCM():
triggers {
cron('0 4 * * *')
}
In the 'Scan multibranch pipeline log' I see the following but no job runs at 4am (time in the trigger() in my Jenkinsfile):
Changes detected: my-feature-branch (1234567890abcdefgh → abcdefgh123456789)
Did not schedule build for branch: my-feature-branch
What am I doing wrong please?
Edit:
So I've tried this set up instead:
Set the cron to every 15 minutes
triggers {
cron('5,20,35,50 * * * *')
}
Removed the setting under configure in the UI 'Suppress automatic SCM triggering'
But it just starts running the minute polling has happened (16 minutes past the hour in this test).
What ever I do nothing seems to pay attention to my cron settings?
If I got to 'View configuration' under the branch job in the UI it shows the UI settings from my Jenkinsfile ok.
Edit (again!):
So with the last edit, it did actually run immediately and then again at the cron time.
Now enabled again in the UI the setting 'Suppress automatic SCM triggering'.
And I have it working! The main issue I realised (a) changes are not applied I do not think until the run after the first run with a change in the Jenkinsfile? (b) Also I installed the next execution plugin so I can see what it is planning on better.
The issue here was that trigger declared in multibranchPipelineJob is for scanning multibranch. To run job periodically declare trigger in pipeline like this:
pipeline {
triggers {
cron('45 6 * * 1-5')
}
agent {
...

Jenkins starts Duplicate builds from same commit Multibranch pipeline

We're seeing multiple builds of our Jenkins multi-branch pipeline being triggered by the same merge commit in Bitbucket.
One build is marked triggered by a: "Branch event at "
The other by a: "commit notification "
We have our Jenkins urls setup in a plugin
Bitbucket Server Webhook to Jenkins
and have a our trigger in the Jenkinsfile setup as follows:
triggers {
pollSCM ""
cron "H 0 * * *"
}
There seem to be a couple of old questions that unfortunately don't have concrete answers.
We were using Bitbucket with Jenkins integration and had this issue. Our problem was that on the Bitbucket webhook we had selected notifications from push and from PR. This configuration created a Job with the branch name and a second job called PR-XXX when the PR was created.
Maybe this is the reason?
It looks like both the: Branch API Plugin and the Git client plugin were sending events to Jenkins that triggered a build.
We solved the issue by suppressing automatic triggering.
This can be done either in the UI simply by going to your job, selecting configure from the left then adding the property
Suppress automatic SCM triggering.
Alternatively for a code solution (which I ended up using) add it to the seedjob.groovy as follows:
multibranchPipelineJob("${service.name}-build") {
// ... unrelated code omitted
configure { project ->
project.remove(project / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'strategy' / 'properties')
def s = project / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'strategy' {
properties(class: 'java.util.Arrays$ArrayList') {
a(class: 'jenkins.branch.NoTriggerBranchProperty') {
'jenkins.branch.NoTriggerBranchProperty' ''
}
}
}
}
}
UPDATE:
It seems like Julian's answer worked but had a bug where it didn't automatically trigger pushes to feature branches. In our Jenkinsfile we added:
properties([overrideIndexTriggers(true)])
Which ensures git commit still triggers a build despite NoTriggerBranchProperty in our seedjob.

How do I configure "Scan Multibranch Pipeline Triggers" in my jenkinsfile?

Right now I manually configure my all my multibranch pipeline jobs and set "Scan Multibranch Pipeline Triggers" to 3 minutes.
How do I put this in my Jenkinsfile? I can't find examples of this. Is "Scan Multibranch Pipeline Triggers" available in the triggers{} block?
The settings on the multibranch configuration page only configure the multibranch scan job itself, not the individual jobs created inside the multibranch "folder".
The option under "Scan Multibranch Pipeline Triggers" that says "Periodically if not otherwise run" is only a trigger for when the multibranch job will scan for new branches. If changes are found to existing branches, or if new branches are discovered with a Jenkinsfile that match your branch specifications, a new build will be triggered, but this is not intended to be the way a job is triggered.
Actually, you can disable the automatic build when changes are found by adding a property to the SCM configuration to "Disable automatic SCM Triggering". Then then you will see the multibranch scan trigger, but the jobs themselves won't build, even if there are changes found.
To trigger jobs, ideally you should use a webhook if you can. If you use a git hook using the git plugin (not the github plugin), then you need to enable the PollSCM trigger (though you can set it to only poll rarely, or not at all).
If you just want normal triggering options, as of 2.22, you can configure the either cron or pollSCM triggers.
pipeline {
triggers {
cron('H/4 * * * 1-5')
pollSCM('0 0 * * 0')
}
Then I believe you can configure webhooks to inform your multibranch job when to do a scan. I haven't tried that. I just tell it to scan every hour or a couple times per day using the "Periodically if not otherwise run".
Note, the same thing applies for the build discarder and other things you configure in your multibranch job. In the web UI, you can only configure the multibranch job itself, not the individual jobs created from it. You have to use Pipeline to configure the jobs.
If your're using the JobDSL Jenkins plugin for creating jobs, then you can add following lines to configure "Scan Multibranch Pipeline Triggers":
configure {
it / 'triggers' << 'com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger'{
spec '* * * * *'
interval "60000"
}
}
Using the JobDSL Jenkins Plugin for multibranch pipeline job, the periodic folder trigger can be configured as given below. In this example, the maximum amount of time since the last indexing that is allowed to elapse before an indexing is triggered will be seven days.
multibranchPipelineJob('my-awesome-job') {
triggers {
periodicFolderTrigger {
interval("7d")
}
}
}

Jenkins multi-branch pipeline and specifying upstream projects

We currently generate a lot of Jenkins jobs on a per Git branch basis using Jenkins job DSL; the multi-branch pipeline plugin looks like an interesting way to potentially get first-class job generation support using Jenkinsfiles and reduce the amount of Job DSL we maintain.
For example we have libwidget-server and widget-server develop branch projects. When the libwidget-server build finishes then the widget-server job is triggered (for the develop branch). This applies to other branches too.
This makes use of the Build after other projects are built to trigger upon completion of an upstream build (e.g. libwidget-server causes widget-server to be built).
It seems that the multi-branch pipeline plugin lacks the Build after other projects are built setting - how would we accomplish the above in the multi-branch pipeline build?
You should add the branch name to your upstream job (assuming you are using a multi-branch pipeline for the upstream job too).
Suppose you have a folder with two jobs, both multi-branch pipeline jobs: jobA and jobB; jobB should trigger after jobA's master.
You can add this code snippet to jobB's Jenkinsfile:
properties([
pipelineTriggers([
upstream(
threshold: 'SUCCESS',
upstreamProjects: '../jobA/master'
)
])
])
(Mind that any branch of jobB here will trigger after jobA's master!)
I'm currently trying to get this to work for our deployment.
The closest I've got is adding the following to the downstream Jenkinsfile;
properties([
pipelineTriggers([
triggers: [
[
$class: 'jenkins.triggers.ReverseBuildTrigger',
upstreamProjects: "some_project", result: hudson.model.Result.SUCCESS
]
]
]),
])
That at least gets Jenkins to acknowledge that it should be triggering when
'some_project' get's built i.e it appears in the "View Configuration" page.
However so far builds of 'some_project' still don't trigger the downstream
project as expected.
That being said maybe you'll have more luck.
Let me know if it works for you.
(Someone else has asked a similar question here -> Jenkins: Trigger Multi-branch pipeline on upstream change )

Resources