Jenkins and Gerrit integration with google repo scm

1.8k views Asked by At

I am writing because there is something not clear to me on how to configure jenkins to fetch from gerrit the changes for building my project.

There is a lot of tutorial / examples on the net related to how to connect all those piece of code. But I didn't found any that explain what I want to do. I think it's possible but... well. I'm not sure.

My setup: I have a repo manifest that contains a lot of different modules of the final application. The app was written in this way because I can change a module and substitute it with another with similar code that do a different job. For example they can be personalization for different customers.

I setup a job in jenkins that catch every commit in each submodule.

I connected Jenkins to Gerrit and I can start the build with gerrit trigger.

It works and I can build my master and the current master.

My problem is that when I send a patch for review jenkins build the master, not the patch I sent. I would like to test the last patch before integrating it on master.

Modules cannot be built standalone because they are all connected in some way so I cannot check each part alone.

Someone was able to accomplish something like this?

3

There are 3 answers

2
Marcelo Ávila de Oliveira On BEST ANSWER

When the Gerrit Trigger start the Jenkins job, you could execute the following to prepare the workspace before you execute the build:

  1. Use "repo sync" to checkout all repositories
  2. Use the GERRIT_PROJECT and GERRIT_REFSPEC Gerrit Trigger env variables and execute the following commands to checkout the patchset of the changed repository:

    cd $GERRIT_PROJECT

    git fetch https://USER@SERVER/a/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout FETCH_HEAD

    OR

    git fetch ssh://USER@SERVER:29418/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout FETCH_HEAD

2
Marcelo Ávila de Oliveira On

The key is to set accordingly the "Repositories" > "Advanced.." > "Refspec" and the "Branches to build" > "Branch Specifier" fields in: Job > Configure > Source Code Management > Git

If you want to build the "master" branch then set:

Refspec = +refs/heads/master:refs/remotes/origin/master

Branch Specifier = refs/heads/master

If you want to build the patchset then set:

Refspec = $GERRIT_REFSPEC:$GERRIT_REFSPEC

Branch Specifieer = $GERRIT_REFSPEC

Click on the "?" at the right of the field to see more details.

0
Chris On

See the gerrit-trigger-plugin man page:

repo init -u git://gerrit.mycompany.net/mymanifest.git
repo sync
repo download $GERRIT_PROJECT $GERRIT_CHANGE_NUMBER/$GERRIT_PATCHSET_NUMBER

... does what you're looking for.