Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-6856

Git builds with detached head no matter what

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Component/s: git-plugin
    • Labels:
      None
    • Environment:
      Ubuntu 10.04 amd64
    • Similar Issues:

      Description

      The Git SCM plugin has suffered from this problem since I started using it. Rather than merge the changes to be built into a branch which tracks the remote I want to push to, it checks out one rev and merges the others on, resulting in a detached HEAD. My build expects to be run on a specific branch, and it fails because the repo isn't in that state.

      Other people have encountered the same problem, and there doesn't seem to be any solution other than to fix things in the build steps, which is a big hassle.

      Can we please get this issue resolved, or if it's been fixed, clear instructions on how to make it work?

      I've attached the SCM portion of my job's config.xml. What I'm trying to do is build changes committed to branches in the origin repo (SG), merge those into SG-gold/master and push them back to SG-gold/master.

      Here are the commands where things seem to go wrong:

      [SG-experimental] $ git rev-parse gold/master
      [SG-experimental] $ git checkout -f ceb23d234b1b21006f049d310501082b77b70ae4
      [SG-experimental] $ git merge 172b2d433ee57509f8e886f5d734f226d49166dd
      [SG-experimental] $ git tag -a -f -m "Hudson Build #35" hudson-SG-experimental-35
      Warning : There are multiple branch changesets here
      

      Rather than "git checkout gold/master", it finds the hash of gold/master and checks out that instead. This is the root of the problem.

      In order to fix this, you have to put these in the first build step:

      git branch -D temp_merge || true
      git branch temp_merge ${BUILD_TAG}
      git checkout master
      git reset --hard gold/master
      git merge temp_merge
      

      This is error-prone and requires duplication of the merge target branch that's already in the Git configuration for the job.

        Attachments

          Issue Links

            Activity

            Hide
            markewaite Mark Waite added a comment -

            Daniel Bengtsson you could also use the "Local Branch" extension that was added to git plugin 2.4.4. Refer to the online help for the local branch extension.

            In the Local Branch extension, you set the branch name to "**" or the empty string, and that then causes the plugin to checkout to a branch name derived from the branch that was the origin of the checkout. That allows a branch specifier to select multiple branches, then the branch which is checked out is based on the specific branch selected for that run of the job.

            Show
            markewaite Mark Waite added a comment - Daniel Bengtsson you could also use the "Local Branch" extension that was added to git plugin 2.4.4. Refer to the online help for the local branch extension. In the Local Branch extension, you set the branch name to "**" or the empty string, and that then causes the plugin to checkout to a branch name derived from the branch that was the origin of the checkout. That allows a branch specifier to select multiple branches, then the branch which is checked out is based on the specific branch selected for that run of the job.
            Hide
            itaiganot Itai Ganot added a comment -

            I'm writing a pipeline which clones a git repository and supposed to checkout to a specific branch, then merge master to that branch and run a MSBuild.

            In reality, git checks out to the sha of the last commit in the branch instead of the branch name itself which is a detached head and the ${GIT_BRANCH} variable is evaluated to 'null'.

            // Started by user itai ganot
            [Pipeline] node
            Running on master in C:\Program Files (x86)\Jenkins\workspace\bbb
            [Pipeline] {
            [Pipeline] stage
            [Pipeline] { (Setup)
            [Pipeline] deleteDir
            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] stage
            [Pipeline] { (Checkout SCM & Merge master to feature branch)
            [Pipeline] checkout
            Cloning the remote Git repository
            Cloning repository http://pctfs1:8080/tfs/DefaultCollection/PC_International/_git/Ensure-pcs-intl
             > git.exe init C:\Program Files (x86)\Jenkins\workspace\bbb # timeout=10
            Fetching upstream changes from http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/Ensure-pcs-intl
             > git.exe --version # timeout=10
            using GIT_SSH to set credentials javab SSH file
             > git.exe fetch --tags --progress http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME +refs/heads/*:refs/remotes/origin/*
             > git.exe config remote.origin.url http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME # timeout=10
             > git.exe config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
             > git.exe config remote.origin.url http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME # timeout=10
            Fetching upstream changes from http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME
            using GIT_SSH to set credentials javab SSH file
             > git.exe fetch --tags --progress http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME +refs/heads/*:refs/remotes/origin/*
            Seen branch in repository origin/feature/merge_tfs
            Seen branch in repository origin/master
            Seen branch in repository origin/origin
            Seen 3 remote branches
             > git.exe tag -l # timeout=10
            Checking out Revision 97b3493db4f726e11e334e5ba34fa808b63edec5 (origin/feature/merge_tfs)
             > git.exe config core.sparsecheckout # timeout=10
             > git.exe checkout -f 97b3493db4f726e11e334e5ba34fa808b63edec5
            First time build. Skipping changelog.
            [Pipeline] bat
            [bbb] Running batch script
            
            C:\Program Files (x86)\Jenkins\workspace\bbb>cd C:\Program Files (x86)\Jenkins\workspace\bbb 
            
            C:\Program Files (x86)\Jenkins\workspace\bbb>git branch 
            * (HEAD detached at 97b3493)
            
            C:\Program Files (x86)\Jenkins\workspace\bbb>echo "git branch: null" 
            "git branch: null"
            
            C:\Program Files (x86)\Jenkins\workspace\bbb>echo "git branch: null" 
            "git branch: null"
            [Pipeline] }
            [Pipeline] // stage
            

            How can I make sure that I checkout to the relevant branch_name?

            Show
            itaiganot Itai Ganot added a comment - I'm writing a pipeline which clones a git repository and supposed to checkout to a specific branch, then merge master to that branch and run a MSBuild. In reality, git checks out to the sha of the last commit in the branch instead of the branch name itself which is a detached head and the ${GIT_BRANCH} variable is evaluated to 'null'. // Started by user itai ganot [Pipeline] node Running on master in C:\Program Files (x86)\Jenkins\workspace\bbb [Pipeline] { [Pipeline] stage [Pipeline] { (Setup) [Pipeline] deleteDir [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Checkout SCM & Merge master to feature branch) [Pipeline] checkout Cloning the remote Git repository Cloning repository http: //pctfs1:8080/tfs/DefaultCollection/PC_International/_git/Ensure-pcs-intl > git.exe init C:\Program Files (x86)\Jenkins\workspace\bbb # timeout=10 Fetching upstream changes from http: //TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/Ensure-pcs-intl > git.exe --version # timeout=10 using GIT_SSH to set credentials javab SSH file > git.exe fetch --tags --progress http: //TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME +refs/heads/*:refs/remotes/origin/* > git.exe config remote.origin.url http: //TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME # timeout=10 > git.exe config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10 > git.exe config remote.origin.url http: //TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME # timeout=10 Fetching upstream changes from http: //TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME using GIT_SSH to set credentials javab SSH file > git.exe fetch --tags --progress http: //TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME +refs/heads/*:refs/remotes/origin/* Seen branch in repository origin/feature/merge_tfs Seen branch in repository origin/master Seen branch in repository origin/origin Seen 3 remote branches > git.exe tag -l # timeout=10 Checking out Revision 97b3493db4f726e11e334e5ba34fa808b63edec5 (origin/feature/merge_tfs) > git.exe config core.sparsecheckout # timeout=10 > git.exe checkout -f 97b3493db4f726e11e334e5ba34fa808b63edec5 First time build. Skipping changelog. [Pipeline] bat [bbb] Running batch script C:\Program Files (x86)\Jenkins\workspace\bbb>cd C:\Program Files (x86)\Jenkins\workspace\bbb C:\Program Files (x86)\Jenkins\workspace\bbb>git branch * (HEAD detached at 97b3493) C:\Program Files (x86)\Jenkins\workspace\bbb>echo "git branch: null " "git branch: null " C:\Program Files (x86)\Jenkins\workspace\bbb>echo "git branch: null " "git branch: null " [Pipeline] } [Pipeline] // stage How can I make sure that I checkout to the relevant branch_name?
            Hide
            markewaite Mark Waite added a comment -

            Itai Ganot please refer to the comment prior to your question.

            The "pipeline syntax" link on the left of most pipeline job pages will open a page where you can prototype the code you need to perform a checkout to a specific branch. Use the "checkout" selection from that list (not the "git" selection), then select "Git" and add the "Additional Behaviours" for "Check out to specific local branch". You can then set a specific branch name, or you can use "**" to indicate that it should use the branch name from the repository where the clone was performed.

            Show
            markewaite Mark Waite added a comment - Itai Ganot please refer to the comment prior to your question . The "pipeline syntax" link on the left of most pipeline job pages will open a page where you can prototype the code you need to perform a checkout to a specific branch. Use the "checkout" selection from that list (not the "git" selection), then select "Git" and add the "Additional Behaviours" for "Check out to specific local branch". You can then set a specific branch name, or you can use "**" to indicate that it should use the branch name from the repository where the clone was performed.
            Hide
            itaiganot Itai Ganot added a comment -

            Thanks, it did solve my first issue.

            But now I have another issue which is also related, I'd like to set:

            // currentBuild.displayName = "#${BUILD_NUMBER}|${BRANCH_NAME}" 
            

            And it cannot be done in pipeline because of the same reason... what can be done to achieve that?

            Show
            itaiganot Itai Ganot added a comment - Thanks, it did solve my first issue. But now I have another issue which is also related, I'd like to set: // currentBuild.displayName = "#${BUILD_NUMBER}|${BRANCH_NAME}"   And it cannot be done in pipeline because of the same reason... what can be done to achieve that?
            Hide
            markewaite Mark Waite added a comment -

            Itai Ganot please don't use closed bug reports as a forum for questions. You're asking for time from the very few people who read git plugin bug reports, when there are much larger groups (like the Jenkins users mailing list) that can address your questions.

            Show
            markewaite Mark Waite added a comment - Itai Ganot please don't use closed bug reports as a forum for questions. You're asking for time from the very few people who read git plugin bug reports, when there are much larger groups (like the Jenkins users mailing list) that can address your questions.

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                ieure ieure
              • Votes:
                0 Vote for this issue
                Watchers:
                15 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: