7

I have the following setup active and working:

  • Jenkins with Git and Sonar plugins
  • One jenkins job ( project ) which polls Git each minute
  • One jenkins job ( project-sonar ) which polls git each 24 hours

Both jobs share the same git repository.

This allows me to build my project for each commit and then each day, only if the project has changed, run the Sonar analysis.

I've recently set up the git repository to send notifications to Jenkins when a project has changed, as per Push notifications from repository . This builds both projects immediately, but I want only the quick ( project ) job to build. If I move the project-sonar to be built periodically, the sonar analysis will be run even if there are no code changes, which is wasteful.

How can I retain

  • immediate build for the project build
  • daily build for for project-sonar build

?

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278

3 Answers3

3

I implemented something very much like what you're looking at using the "Run Condition" plugin to jenkins. https://wiki.jenkins-ci.org/display/JENKINS/Run+Condition+Plugin

I made the sonar job a follow-on to the polling/build/test job, with conditions so it only runs once a day.

Dennis S.
  • 2,081
  • 14
  • 14
  • Thanks for the pointer to the plugin. Can you share some details on how you implemented the run once per day condition? – Robert Munteanu Aug 14 '12 at 20:11
  • Sorry it took a couple days to get back to this. On review, I discovered I wasn't quite accurate on my answer above. We have a separately scheduled job to do a build once a day, and the sonar job is a follow-on to that. The Run Condition plugin is used to control the day of the week (we don't run Sonar over the weekend). – Dennis S. Aug 17 '12 at 20:50
  • Thinking about it, you could set up a separate daily job that polls for changes since the last run, and builds and tests if there are any. Make the sonar job a build step under the Run Condition plugin using the build status condition, and I think that gives you the control you're looking for. – Dennis S. Aug 17 '12 at 20:55
1

I've solved this using the following approach:

  1. All jenkins projects are named after their git repository ( I use gitolite )
  2. I have activated only Trigger builds remotely for the base builds
  3. I have added a post-receive hook in gitolite which does something like

    $CURL --silent --netrc --insecure --connect-timeout 2 "$GIT_REMOTE_TRIGGER_URL/$GL_REPO/build?token=$JENKINS_BUILD_TOKEN" > /dev/null

  4. Scheduled all sonar jobs to poll the SCM every 24 hours

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
0

I have my regular project and a second one called "project-daily" that runs daily.

"Project" takes a parameter named "RELEASE_TAG", which is passed as the branch specifier in the git section. The git plugin will also allow a tag to be specified here.

When "project" starts, it saves $COMMIT to a file named latest/$PROJECT.$BRANCH. ($BRANCH is set to $RELEASE_TAG, unless $RELEASE_TAG is a tag, in which case $BRANCH is set to "master".) This file is read by "project-daily".

"Project-daily" is a pipeline script. It does not have a repository. To save space, it runs "git clone --bare ...", and then "git log -n 1 --format=format:%H daily". It reads "latest/$PROJECT.daily" to get the latest build (attempt). If that commit is different than the "git log" output, then it calls "build job:'project'", with parameter "RELEASE_TAG" set to "daily".

We push our working code to the daily branch. When it's time for a release build, we merge to master, tag it, and then run "project" manually with the tag (which specifies the version).