3

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?

1

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.

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

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

  3. 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.

1

First, you have to create a new job in Jenkins ( Github organization ) and configure it to scan all repos and all branches, you can do it by following this URL second, press scan organization from Jenkins, now you will see all repos and all braches which contain Jenkinsfile which contains all your stages and all pipeline stages, check also this URL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.