HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Enabling Github webhooks for multibranch jobs?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
multibranchgithubjobsforwebhooksenabling

Problem

I'm setting up a CI scheme for a few projects, on Jenkins, using the multibranch plugin.
Everything is working pretty nicely, but the obsessive in me always wants a bit more.

Current situation:

The multibranch thingy is working: picks up and builds new branches and PRs. It does so by polling: Every N minutes it polls Github for new branches and branch updates.

I want:

I would very much like to build new pushes on Github Webhook push events and avoid polling. When not dealing with multibranch, it was as simple as checking the GitHub hook trigger for GITScm polling box.
In this context, I would have to add a new trigger to the Jenkinsfile. I assume something like:

triggers {
    githubWebhook()
}


Currently, my Jenkinsfile is something like this:

pipeline {
    agent {
        docker {
            image '${REDACTED}/builder:sbt-0.13-jdk-oracle-8'
            args '-v sbt-cache:/root/.sbt/ -v ivy-cache:/root/.ivy2/'
            customWorkspace '/src/'
        }
    }
    options {
        buildDiscarder(logRotator(numToKeepStr: '10'))
        timestamps()
        timeout(time: 1, unit: 'HOURS')
    }
    stages {
        stage('Testing') {
            steps {
                sh "sbt clean coverage test coverageReport coverageAggregate sonar"
            }
        }

        stage('Push snapshot'){
            when {
                not {
                    branch 'master'
                }
            }
            steps {
                echo "[DRYRUN] sbt publishSnapshot"
            }
        }

        stage('Push release') {
            when {
                branch 'master'
            }
            steps {
                echo "[DRYRUN] sbt publish"
            }
        }
    }
    post {
        changed {
            echo "[DRYRUN] changed build"
        }
    }
}


Has anyone dealt with something like this? Any other approach?

Solution

According to the official GitHub Branch Source Plugin documentation, the plugin can automatically configure webhooks for you if you have your GitHub API token configured in Jenkins global settings:


You can change the build configuration to perform the folder
computation at different times. The default setting will scan your
GitHub repository for changes if it has not received any change
notifications from GitHub within the past day. This scan will catch
the repositories you’ve added or removed from the managed GitHub
repository. You can always force the Folder Computation to run from
the GitHub Organization page.


While the build triggers are often enough, you can set up webhooks to
automatically trigger builds when changes are pushed to your GitHub
repositories. To do this you must have a GitHub login with a token.



-
Go to the main configuration settings page, Manage Jenkins > Configure System

-
In the GitHub Plugin Configuration section, add a server with your credentials

-
If you need a token, generate one with the Additional Actions > Convert login to password and token



You can also configure this manually through GitHub itself by
registering the URL provided in the help section of the server config.

Context

StackExchange DevOps Q#4044, answer score: 1

Revisions (0)

No revisions yet.