How to checkout git repo with an specific revision in Jenkins

18,758

Solution 1

As git treats any branch as a pointer to commit, you can specify the commit in branch specifier.

EDIT (reply to comment): What you call a branch, is in fact just a pointer to a particular commit. Git has no such branches as svn; thus if you want to deploy a specific commit, you just supply it.

I understand you don't want to change to job every time you need to build it. You can make the build parametrized and then use the parameter as branch specifier.

Solution 2

In Pre Steps add this script under the Execute shell:

# update the local git repository
git fetch 

# pull the desired branch
git pull origin <branch>

# checkout the specific commit you want.
git checkout <commit version>
Share:
18,758

Related videos on Youtube

Mitesh Jaiswal
Author by

Mitesh Jaiswal

Updated on June 04, 2022

Comments

  • Mitesh Jaiswal
    Mitesh Jaiswal almost 2 years

    I am running Jenkins with git, s3 and aws-codedeploy. for deploy build application over s3 and it trigger aws codedeploy for post deployment process. The above setup is running perfectly.

    I am getting issue whenever I run jenkins job, my job all time clone git repo then upload a zip of build/contents to s3 then aws-codedeploy deploy full zip on my servers.

    But I want when I run jenkin job it is take specific git revision clone only not for full contents and build deployment with that only.

    Please help me out on the above issue. thanks in advance...

  • Mitesh Jaiswal
    Mitesh Jaiswal about 8 years
    thanks for your ans, but I want to build particular commit from branch, like in "Branch Specifier" I given */master but I want deploy with specific commit/revision id.
  • Peter Kahn
    Peter Kahn almost 7 years
    Can you do this on a pipeline? I've tried providing the full sha and keep on getting no revisions found. I think the GitScm plugin just pulls refs for tag/branches and doesn't support all commit ids even though its javadoc says it can do this
  • trainmaster
    trainmaster over 6 years
    I didn't try actually but it should be possible. Do you have any code snippet you tried?
  • Ronald Holshausen
    Ronald Holshausen over 5 years
    @PeterKahn You need to specify a valid refspec, otherwise Jenkins will try use the parameter string without expanding it and it will fail. Specify something valid like ` +refs/heads/master:refs/remotes/origin/master` for the refspec (under Advanced) and then add "Advanced clone behaviors" under "Additional Behaviours" and check the "Honor refspec on initial clone" option. Then Jenkins will do a git fetch with the valid refspec, and then a checkout with the expanded parameter value.