Skip to content

automatic builds for maven, jenkins and gitbucket

Matthieu Brouillard edited this page Feb 8, 2018 · 7 revisions

Automate maven builds using Jenkins and gitbucket

This setup demo a full automation setup using nginx, gitbucket, Jenkins, Pipeline multibranch, Github branch source, webhooks and build statuses.

In this setup the goal is to:

  • fire a build for each commit, on every branch or on any PR
  • have different project versions per branch and PR
  • to have build statuses set on git commits

Environnement

All the setup below is provided as an example, adapt with recent versions and with your platform.
I built this setup on windows 10.

The services will be availabe at:

hosts

We will fake DNS names for gitbucket & jenkins services. Edit your hosts file, mine is under C:\Windows\System32\drivers\etc, add entries for the 2 services

127.0.0.1 jenkins.my.pc
127.0.0.1 gitbucket.my.pc

nginx

We will serve both services on port 80 using the server names defined above. For that we will a nginx reverse proxy.

Having a nginx-1.12.1 running instance, mine is installed under D:\dev\tools\web\nginx\nginx-1.12.1

Create NGINX_HOME\conf\ci.conf file.

server {
    listen 80;
    server_name jenkins.my.pc;
     
    location / {
 
      proxy_set_header        Host $host:$server_port;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;
 
      # Fix the "It appears that your reverse proxy set up is broken" error.
      proxy_pass          http://127.0.0.1:8090;
      proxy_connect_timeout   150;
      proxy_send_timeout      100;
      proxy_read_timeout      100;
  
      # Required for new HTTP-based CLI
      proxy_http_version 1.1;
      proxy_request_buffering off;
    }
}
  
server {
    listen 80;
    server_name gitbucket.my.pc;

    location / {
        proxy_pass              http://127.0.0.1:8080;
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout   150;
        proxy_send_timeout      100;
        proxy_read_timeout      100;
        proxy_buffers           4 32k;
        client_max_body_size    500m; # Big number is we can post big commits.
        client_body_buffer_size 128k;
    }
}

edit NGINX_HOME\conf\nginx.conf, add an include to above file.

...
http {
   ...
   # Include CI specific setup for gitbucket & jenkins
   # put the include INSIDE the http tag
   include "D:/dev/tools/web/nginx/nginx-1.12.1/conf/ci.conf";
}
...

Restart/reload nginx: nginx -s reload

gitbucket

Here we will use a default installation of gitbucket using version 4.21.2.

Download gitbucket.war from 4.21.2 release.

  • start gitbucket: java -jar gitbucket.war
  • navigate to http://gitbucket.my.pc/
  • connect using root/root, then create users:
    • ci/ci: an administrator, will be used from jenkins
    • john/j: a simple user
    • bill/b: another simple user

jenkins

Download Jenkins and run it using java -jar jenkins.war --httpPort=8090

I personally skipped default installation and then manually added:

  • GitHub Branch Source Plugin 2.3.1
  • Pipeline: Basic Steps 2.6
  • Pipeline: Model API 1.2.5
  • Pipeline: Multibranch with defaults 1.1
  • Pipeline: Nodes and Processes 2.17
  • Pipeline: Shared Groovy Libraries 2.9
  • Pipeline: Stage Step 2.3

Configure jenkins:

Manage Jenkins > Configuration

  • Jenkins url: http://jenkins.my.pc/
  • Add a Github Enterprise Server:
    • API endpoint: http://gitbucket.my.pc/api/v3
    • Name: Gitbucket local jenkins configuration

Manage Jenkins > Global Tool Configuration

  • Add a git installation, point it to git.exe on your disk. A recent version of git.exe is required, because git.exe will have to follow http redirections ; my version is 2.14.2.windows.2
  • Add a JDK 1.8 installation, name it JDK 1.8
  • Add a Maven 3.3.x installation, name it Maven 3.3.x jenkins tools configuration

Setup

We will use the project demo-ci-jgitver-maven-plugin.

Gitbucket project creation and setup

Create placeholder project in John gitbucket space.

  • Connect as john/j
  • Create placeholder repository, call it demo-ci project creation step 1 project creation step 2
  • checkout github project and push it to john space
git clone https://github.com/jgitver/demo-ci-jgitver-maven-plugin.git demo-ci
cd demo-ci
git push http://john:[email protected]/git/john/demo-ci.git
  • Go to gitbucket project settings page, choose "Service Hooks" tab
  • Add a webhook to http://jenkins.my.pc/github-webhook/, select "Pull Request" & "Push" events webhook creation

Jenkins project setup

create job step 1

  • Add a github branch source

create job step 2

  • Choose "Gitbucket local" server
  • Add Username with password credentials for "ci" user

create job step 3

  • Enter "john" as owner
  • choose "demo-ci" as repository create job step 4

In order to have jgitver correctly naming versions for branches and pr, few additional behaviors are required for the git source

  • Add a "filter by name" behavior

create job step 5

  • enter ** as include filter
  • Add a "Check out to matching local branch" behavior

create job step 6

Configuration should be

create job step 7

  • Click save, et voilà ! Jenkins discovered the master branch and fired a build for it.

Playing with branches

Now as John, create a branch 'br', make a small change (in README.md for example) and commit.

Automatically, Jenkins (via the webhook) will be notified of the commit and will build the 'br' branch.

br branch build

By looking at the build result for the br branch, you will see that jgitver correctly calculated the project version.

br branch jgitver build

Handling Pull-Requests

As Bill, in gitbucket, fork John repository.

Do a small change and commit (here we added a LICENSE file).

bill fork

Go to "Pull Request" menu, and create a "Pull Request"

bill PR

In John view the PR appeared

john PR

Jenkins should have been informed of the PR, and should have built a version for it.

jenkins PR

jgitver should have named it accordingly

jenkins PR