Skip to content

Jenkins and GitHub Pull Requests integration

Archived (pre-2022)

Preserved for reference only -- likely outdated. View original | Last updated: September 2020

Prerequisites

  1. Jenkins server must have credentials with appropriate access to the github repo you want to integrate with. Currently we use fyber-ci Jenkins user. Other creds can be added. More on credentials: ghprb-plugin (Github)

Job configuration

The following is example of Job integrated with GitHub PRs:

def configFile = readFileFromWorkspace('config/jenkins/dsl/config/parameters.groovy')
def env = System.getenv("JAC_ENVIRONMENT")
def config = new ConfigSlurper(env.toLowerCase()).parse(configFile)

pipelineJob(config.projectName + '-test-job') {

    properties {
        githubProjectUrl('https://github.com/SponsorPay/chef/')
    }

    definition {
        cpsScm {
            scriptPath('Jenkinsfile-test')
            scm {
                git {
                    branch('${sha1}')
                    remote {
                        name('origin')
                        url(config.gitUrl)
                        credentials(config.credentialsId)
                        refspec('+refs/pull/${ghprbPullId}/*:refs/remotes/origin/pr/${ghprbPullId}/*')
                    }
                }
            }
        }
    }

    triggers {
        githubPullRequest {
            admins(['mykhailoguk', 'ddenyshchenko', 'slobodyanyuk'])
            cron('H/5 * * * *')
            triggerPhrase('.*(re)?run tests.*')
            useGitHubHooks()
            extensions {
                commitStatus {
                    context('Jenkins')
                }
                buildStatus {
                    completedStatus('SUCCESS', 'Chef tests passed.')
                    completedStatus('FAILURE', 'Chef tests have failed.')
                    completedStatus('ERROR', 'An error occurred in Chef tests.')
                }
            }
        }
    }
}

You can find this job in our Jenkins here:  Jenkins - testsuite-test-job

Webhooks

If Jenkins pooling not an option you can use GitHub web hooks for triggering Jenkins builds.

Web hooks must be created manually since Jenkins system user that we use for GitHub integrations doesn't have Admin rights.

GiHub project web hooks configuration:

Screenshot 2020-09-17 at 15.00.38.png

Please reach out BLN DevOps to obtain shared secret for web hooks.

The web hook must have the following events enabled:

Screenshot 2020-09-17 at 15.03.54.png

Once enabled GitHub will produce web hook logs that you can find at the very bottom of the web hook config page:

Screenshot 2020-09-17 at 15.08.10.png

If job is configured properly you will see Jenkins job triggering in PR status:

Screenshot 2020-09-17 at 11.48.25.png

References

For more detailed information please visit  GitHub Pull Request Builder Plugin project: ghprb-plugin (Github)