Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Hudson to build a specific git commit

Tags:

git

hudson

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?

like image 858
Bernd Avatar asked Nov 17 '10 17:11

Bernd


People also ask

How do I deploy a specific commit in Jenkins?

Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.

Can I pull specific commit?

The short answer is: you cannot pull a specific commit from a remote. However, you may fetch new data from the remote and then use git-checkout COMMIT_ID to view the code at the COMMIT_ID .


7 Answers

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.

like image 80
izzekil Avatar answered Oct 08 '22 15:10

izzekil


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.

like image 38
lkimmel Avatar answered Oct 08 '22 17:10

lkimmel


Like the documentation says:

git plugin branch configure

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

like image 31
linkaipeng Avatar answered Oct 08 '22 17:10

linkaipeng


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

   git pull
   git checkout <commit version>
like image 35
Andrzej Jozwik Avatar answered Oct 08 '22 16:10

Andrzej Jozwik


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.

like image 32
VonC Avatar answered Oct 08 '22 16:10

VonC


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.

like image 33
Dave Bacher Avatar answered Oct 08 '22 16:10

Dave Bacher


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.

like image 33
sschuberth Avatar answered Oct 08 '22 17:10

sschuberth