Skip to content

Setup Jenkins Multibranch Pipeline and Organization

ziggystar edited this page Sep 20, 2018 · 6 revisions

This page shows how to setup Jenkins Multibranch Pipeline jobs and Organization folders.

Prerequisite

  • GitBucket is running.
  • Jenkins is running.
  • Jenkins can access to GitBucket without port number and context path, such as http://gitbucket.example.com.
  • Jenkins Multibranch Pipeline is installed.

Creating a Multibranch Pipeline job

Configure Jenkins:

  1. Open Manage Jenkins - Configure System.
  2. Add a server in GitHub Enterprise Servers section. API endpoint should be like http://gitbucket.example.com/api/v3.

Add a job on Jenkins:

  1. Open New Item.
  2. Enter job name and mark as a Multibranch Pipeline. Then, click OK.
  3. Add a GitHub source in Branch Sources section.
    • API endpoint: select GitBucket (http://gitbucket.example.com/api/v3).
    • Credentials: required if anonymous access is denied on GitBucket.
    • Owner: user or group
    • Repository: select your repository
  4. Click Save.

Configure a webhook on GitBucket:

  1. Open your repository - Settings - Service Hooks.
  2. Click Add webhook button.
  3. Fill followings and save.
    • Payload URL: http://jenkins.example.com/github-webhook/
    • Which events would you like to trigger this webhook?: Push, Pull Request

Create a Jenkinsfile on your repository:

pipeline {
  agent any
  stages {
    stage('build') {
      steps {
        sh 'echo Building ${BRANCH_NAME}...'
      }
    }
  }
}

A new build should be started immediately on Jenkins.

Creating an Organization folder

Add a job on Jenkins:

  1. Open New Item.
  2. Enter job name and mark as a GitHub Organization. Then, click OK.
  3. Add a GitHub source in Branch Sources section.
    • API endpoint: select GitBucket (http://gitbucket.example.com/api/v3).
    • Credentials: required if anonymous access is denied on GitBucket.
    • Owner: group
  4. Click Save.

Configure a webhook on GitBucket:

  1. Open your group - Edit group - Service Hooks.
  2. Click Add webhook button.
  3. Fill followings and save.
    • Payload URL: http://jenkins.example.com/github-webhook/
    • Which events would you like to trigger this webhook?: Push, Pull Request

Then, your group and repositories should be shown in Jenkins.

For better security

Configure GitBucket:

  1. Open System settings and set Anonymous access to Deny.
  2. Create jenkins user and add it to your group.

Configure Jenkins:

  1. Open Credentials - Add Credentials.
  2. Fill followings and save.
    • Username: jenkins
    • Password: password of jenkins
    • Description: GitBucket access credential

Then, fix jobs to use credentials.

Pitfalls

Jenkins tries to clone from the wrong URL and uses an old Git client

If your build fails with the following message:

hudson.plugins.git.GitException: Command "git fetch --no-tags --progress http://servername/Owner/Repo.git +refs/heads/master:refs/remotes/origin/master" returned status code 128:
stdout:
stderr: error: RPC failed; result=22, HTTP code = 404
fatal: The remote end hung up unexpectedly

Then, this is caused by Jenkins trying to fetch from the Git repository under the URL http://servername/Owner/Repo.git instead of http://servername/git/Owner/Repo.git. GitBucket provides a redirect from the former to the latter URL, but this is not supported by older versions of Git.

A solution can be to install a newer Git client on the machine doing the checkout.

See also

You can’t perform that action at this time.