36

I have a hudson build server. The source code is managed by a git repository. For each build the latest version is checked out and compiled. Now i'd like to tell hudson to use not the latest version, but an older version of the code (specified by me).

In hudson i have two parameters which can be set. First "name of repository", with default value "origin" and second refspec with value +refs/heads/*:refs/remotes/origin/*. I tried a bit around something like origin/[commitid] or +refs/heads/*:refs/remotes/origin/[commitid]. But nothing worked as expected.

I think i had to use a parameterized job, so that i can give the commit as parameter to the job.

How can i tell hudson to use a specific commit instead of the latest one?

Bernd
  • 361
  • 1
  • 3
  • 3

7 Answers7

57

I just want to this answer more clear. How to make your job to checkout a specific commit, step by step:

  1. Add string parameter to your job with name, let it be COMMIT in my example.
  2. Choose Git as SCM (provided by Jenkins Git plugin).
  3. In Git SCM properties set your repo properties.
  4. In Git SCM, in the paragraph Branches to build type ${COMMIT} which is the reference to the job parameter and will be resolved during the build.

That's it, launch the build and in the log you will see something like this:

Cloning the remote Git repository
Cloning repository ssh://your-repo.git
Fetching upstream changes from ssh://your-repo.git
using GIT_SSH to set credentials 
Fetching upstream changes from ssh://your-repo.git
using GIT_SSH to set credentials 
Checking out Revision af63e2102b65953316e512c0bb659578bb143a33 (detached)

Note, that there are other ways to set the environment variable before the SCM checkout, i.e. using Prepare environment for the run step from the EnvInject Plugin (you could even use Groovy for this).

Also, if you don't see the options I'm talking about or they don't work, ensure you have a new version of a Git plugin In my case it is 2.2.0.

izzekil
  • 5,781
  • 2
  • 36
  • 38
  • Thanks for the detailed steps. Worked like a charm! – Chris J May 29 '14 at 21:58
  • How do you "launch the build"? When I press "Build Now", only one branch is built (master). – user239558 Jan 27 '15 at 10:04
  • Check my answer again, I suggested introducing a parameter. When job is parameterized, the user sees BuildWithParameters form when hit "Build" link instead of immediate build start. – izzekil Feb 02 '15 at 17:46
  • 2
    For the lazy, this also works without using build parameters, you can simply paste the commit identifier into "Branches to build". – Ring Jan 05 '16 at 06:22
  • Upvoted as I found that some of my colleagues prefer to modify the job config directly, for them this is more straightforward and easier to understand. – izzekil Jan 12 '16 at 14:11
  • Just wanted to point out that simply pasting the commit hash into the "Branches to build" field is not compatible with "Advanced clone behaviours > Shallow clone". – Evgeny Chernyavskiy Aug 29 '18 at 17:30
  • **Remove** the "Lightweight checkout" checkbox selection. Then it worked for me. Otherwise not! – Codev Jan 28 '19 at 09:22
9

You can use the branch parameter of the jenkins-git-plugin to define a specific commit id.

Jenkins will then only checkout that commit and not the head of a branch.

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
lkimmel
  • 91
  • 1
  • 1
  • 5
    If you click the little question mark (?) next to the field it says: `...A specific revision can be checked out by specifying the SHA1 hash of that revision in this field.` This feels to me like the "right way to do it". – Ben Jun 26 '14 at 19:47
  • Which question mark? I cannot find any question mark anywhere. What format should I follow to specify the revision? What if I'm using a pipeline? The documentation is practically useless. – cauchi Jul 10 '18 at 12:22
7

Like the documentation says:

git plugin branch configure

Input your commit id to the "Branches to build" setting.

jscs
  • 63,694
  • 13
  • 151
  • 195
linkaipeng
  • 483
  • 7
  • 9
  • I would love to find that documentation. I cannot find anything like that screenshot on the official site. – cauchi Jul 10 '18 at 12:16
  • 2
    @cauchy, this documentation is not on the site, it's in your Jenkins instance, in the job configuration, under Source Code Management->Branches to Build->? – Akom Sep 21 '18 at 18:16
  • This worked perfectly for me. In order to work, the option "Lightweight checkout" must be **disabled**. – Codev Apr 04 '19 at 14:10
4

In "Pre Steps" try to add "Execute shell" and add:

   git pull
   git checkout <commit version>
Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68
  • 5
    Note: This means the GIT_COMMIT environment variable no longer matches the actual commit that is checked out. – Karl Jul 16 '13 at 10:01
4

One workaround would be to:

  • set the Git plugin to build a special "build_br" branch.
  • reset the branch build_br to the expected commit
  • push that branch build_br to remote repo monitor by Jenkins or Hudson (that would be a push --force, as illustrated in "git reset --hard and a remote repository")

That way, building that branch build_br would mean building a specific commit, and the GIT_COMMIT will be correctly set.
No development should take place on that special branch, since it is reset regularly to any commit you need to be build.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

You can configure your Hudson job to build a specific branch. Then you can push whatever changes you want Hudson to build onto that branch.

Dave Bacher
  • 15,652
  • 3
  • 63
  • 86
1

I'm not sure about Hudson, but Jenkins' Git Plugin has an "Advanced..." button at the right just above the "Repository browser" field. Clicking there reveals a lot of additional options, one of them being "Checkout/merge to local branch (optional)". Its help text says "If given, checkout the revision to build as HEAD on this branch. Please note that this has not been tested with submodules", so that seems to be what you have in mind.

sschuberth
  • 28,386
  • 6
  • 101
  • 146