26

We are using the pipeline plugin with multibranch configuration for our CD. We have checked in the Jenkinsfile which works off git.

git url: "$url",credentialsId:'$credentials'

The job works fine, but does not auto trigger when a change is pushed to github. I have set up the GIT web hooks correctly.

Interestingly, when I go into a branch of the multibranch job and I click "View Configuration", I see that the "Build when a change is pushed to Github" is unchecked. There is no way to check it since I can not modify the configuration of the job (since it takes from parent) and the same option is not there in parent.

Any ideas how to fix this?

2
  • What GH webhook event have you registered? push event would be the only required. Is your Jenkins visible from internet? Is GH webhook status showing return values from Jenkins webhook receiver? Commented Apr 18, 2016 at 8:11
  • 1
    Did you ever find a solution to this without polling? Commented Jan 25, 2017 at 3:43

7 Answers 7

7

For declarative pipelines try:

pipeline {
    ...
    triggers {
        githubPush()
    }
    ...
}

For me this enables the checkbox "GitHub hook trigger for GITScm polling", but polling is not actually required. This requires the GitHub plugin.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this documented anywhere?
@trebor not that I can find. I can't remember where I got this from now.
5

I found a way to check the checkbox "Build when a change is pushed to Github".

This line did the trick:

properties([pipelineTriggers([[$class: 'GitHubPushTrigger'], pollSCM('H/15 * * * *')])])

I think the polling is needed to make it work. Would be nice if no polling is needed.

Here's a Jenkinsfile example with this implemented:

#!/usr/bin/env groovy

node ('master'){
    stage('Build and Test') {
        properties([pipelineTriggers([[$class: 'GitHubPushTrigger'], pollSCM('H/15 * * * *')])])
        checkout scm
        env.PATH = "${tool 'Maven 3'}/bin:${env.PATH}"
        sh 'mvn clean package'
    }
}

2 Comments

I wonder if it's possible to achieve the same thing for bitbucket?
@Lake - This change should work for BitBucket: properties([pipelineTriggers([[$class: 'BitBucketTrigger'], pollSCM('H/15 * * * *')])])
2

For declarative pipelines, try this:

pipeline {
    agent any
    triggers {
        pollSCM('') //Empty quotes tells it to build on a push
    }
}

1 Comment

this needs to setup github hook first?
1

If you use Stash for example you can register a Post-Receive WebHook where you have to insert your URL form Jenkins like : http://jenkinsHost:9090/git/notifyCommit?url=ssh://git@gitHost:1234/test.git

In your jenkins Job you have to set at least the Build trigger "Poll SCM". And set a polling time of e.g 5 mins. This enables also the automatic branch indexing for your multibranch project configuration.

Comments

1

Resorting to polling adds latency - time that it takes for a build to start and hence finish giving back a result.

It seemed to me that the basic plugins have a low level of abstraction, so I switched to the Github Organization Folder plugin, which depends on all of them and sets up an organization hook for triggering builds branches and/or pull requests.

Comments

0

Before I start, I would like to emphasize that I had no previous experience with Jenkins so far, so there might be a bunch of better solutions out there.

What I wanted to achieve in a nutshell:

  • After every push made to a Bitbucket repo(test2), on every branch,
    pull and build another Bitbucket repo(test1), from an identical
    branch name and right after that, build test2 using test1 as a
    dependency.

How I managed to achieve that?

  • I started a new job with type 'Multibranch Pipeline'
  • I added the following Jenkinsfile to test2:

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                dir('test1') {
                    git branch: BRANCH_NAME, url: '[email protected]:user/test1.git', credentialsId: 'credentials_id'
                }
                sh('build_process')
            }
        }
    }
}

  • I come across the issue that you can't set up a Bitbucket hook for pipelines

  • I added Bitbucket Branch Source Plugin to Jenkins

  • I selected Bitbucket at 'Branch Sources' when setting up the job

  • I added credentials and put a checkmark to Auto-register webhook

  • Under 'Scan Multibranch Pipeline Triggers' I put a checkmark to Periodically if not otherwise run, with an interval of 1 min

  • I added a webhook to my Bitbucket repo

  • I updated all my plugins, restarted Jenkins and it's ready to go

Other plugins I have installed: Bitbucket Plugin, Pipeline plugin. Hope this helps for somebody, I did manage to solve it this way after hours of struggling without the Bitbucket Branch Source Plugin.

Comments

-3
node{
stage('Build and Test') {
    properties([pipelineTriggers([[$class: 'GitHubPushTrigger'], pollSCM('* * * * *')])])
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxx-xxx-xxxx[your credentails Id]', url: 'https://github.com/git']]])
    echo 'Build and Test has been done'
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.