Jenkins build after mercurial commit
Asked Answered
S

3

6

I've been working on this project for about a week now and I've been searching for 2 days without any clear explanation online. For a school assignment, we need to set up a buildserver with the following programs:

  • Maven, as our build tool.
  • Mercurial as our versioning system.
  • Java (JRE), javac and javadoc.
  • JUnit for unit testing.
  • Jenkins (with JDepend plugin).

We need to create 3 jobs, and 1 of those jobs is:

Every time the (local) repository of Mercurial gets edited, a build needs to start. (Meaning: Every Mercurial commit needs to be detected by Jenkins. This 'detection' needs to be triggered every 1 minute)

The tools that we need to use are: Javac, Maven as our build tool, and Mercurial of course.

This happens all in an Ubuntu environment. Since I am not familiar (at all) with Linux, I have no clue how to do this.

I'm not asking for a ready-made solution, but I am rather asking for some 'tips' to understand things like Mercurial 'Hooks', what I need (to do) to get this job working in Jenkins, etc. Everything I find online is rather vague and uses examples with python.

So concrete: A little guidance might be welcome :) Every helping hand is appreciated!

Stickney answered 7/11, 2012 at 21:48 Comment(0)
C
4

It sounds like your professor wants Jenkins to poll the Mercurial repository for changes ("triggered every 1 minute"). When you set up your Jenkins job, the section called "Build Triggers" will have a poll option. That's the one you want.

A better solution would be to have Mercurial tell Jenkins about changes, since polling is generally a bad idea (even if you don't check anything in for days, Jenkins still asks every minute if anything has changed). You can look at the book for examples on how to write Mercurial hooks, or you can use the Python script I wrote.

Cephalochordate answered 7/11, 2012 at 22:2 Comment(2)
Yes, thats the part I figured out already (* * * * * -> every minute), thanks for the info though. But how can I let jenkins detect that it was a commit, and that he needs to build the files that are that particular repository/folder?Stickney
The Jenkins Mercurial plug-in already knows how to detect the difference between "no change" and "has a change". All you have to do is tell Jenkins, "When you run this job, check the files out from this source control provider (Mercurial), and run this command (Maven). And check every minute with Mercurial to see if a job needs to be run."Cephalochordate
P
13

The most efficient way to trigger builds on Jenkins after someone pushed to your repo is using the changegroup hook in mecurial.

Jenkins on the other hand allows builds to be triggered remotely by calling the URL of your job with a special token. This trigger with the corresponding token has to be configured in your job on Jenkins:

Build trigger configuration

The mercurial hook that will be put in the repos .hg/hgrc file can be defined like:

[hooks]
changegroup = curl --silent http://your.jenkins.server.url/jenkins/job/<YOURJOBNAMEHERE>/build?token=Foo

This hook uses the curl command line tool to call the URL of your jenkins job with the defined token that allows to trigger the job remotely. Curl has to be installed on your linux box of course.

Pharyngitis answered 7/11, 2012 at 22:8 Comment(7)
so, every time a commit/push happens, the url will be fired and the job starts?Stickney
Yes. Every time a push "arrives".Pharyngitis
This might sound silly, (I normally work with git), but will this work for a commit (local change for example). We work in a very hypothetical context (no actual connections to our Mercurial repository, only local changes that simulate a possible push)? I ask this since in git push and commit are 2(very) different things. Thanks in advance! Edit: for commits, instead of [hooks] changegroup = ..., i change changegroup to 'commit' ?Stickney
Yes. Use can use the commit hook for that. To simulate a push, simply create a local clone in the filesystem and push from this repo: hg clone /path/to/your/repo; cd repo; vim foo; hg commit -m "bla"; hg push /path/to/your/repoPharyngitis
The concept of "push" is the same in git and hg. (Same with commit.)Cephalochordate
@Pharyngitis how can we authenticate for such a method?Lauritz
You could add credentials to the call to curl like curl --user yourUser:yourPassword --silent http://...Pharyngitis
C
4

It sounds like your professor wants Jenkins to poll the Mercurial repository for changes ("triggered every 1 minute"). When you set up your Jenkins job, the section called "Build Triggers" will have a poll option. That's the one you want.

A better solution would be to have Mercurial tell Jenkins about changes, since polling is generally a bad idea (even if you don't check anything in for days, Jenkins still asks every minute if anything has changed). You can look at the book for examples on how to write Mercurial hooks, or you can use the Python script I wrote.

Cephalochordate answered 7/11, 2012 at 22:2 Comment(2)
Yes, thats the part I figured out already (* * * * * -> every minute), thanks for the info though. But how can I let jenkins detect that it was a commit, and that he needs to build the files that are that particular repository/folder?Stickney
The Jenkins Mercurial plug-in already knows how to detect the difference between "no change" and "has a change". All you have to do is tell Jenkins, "When you run this job, check the files out from this source control provider (Mercurial), and run this command (Maven). And check every minute with Mercurial to see if a job needs to be run."Cephalochordate
K
0

To trigger Jenkins builds when pushing to a Mercurial repository on Bitbucket you can go to administration (click the cog wheel) for your repository and choose Services. There you can define a Jenkins service with four parameters:

  1. Endpoint: the URL of the Jenkins server.
    • For a secure Jenkins server you need to specify a username and an api token. The api token is generated by Jenkins for the specific user, on the "Manage Users" / "Configure" page.
  2. Module name: optional parameter, to trigger builds selectively depending on what has been pushed
  3. Project name: the name of the Jenkins job
  4. Token: optional parameter, the same as the Authentication Token defined on the Jenkins job when you select "Trigger builds remotely (e.g., from scripts)"

(I suppose that this is just a gui way to define a "changegroup hook" as mentioned by James in his reply from 7 November 2012.)

Kinnon answered 19/7, 2013 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.