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

How to set multiple git refspec in checkout step?

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed (View Workflow)
    • Priority: Minor
    • Resolution: Not A Defect
    • Component/s: git-plugin
    • Labels:
      None
    • Environment:
      git plugin version: 3.0.5
      git version: 2.11.0
    • Similar Issues:

      Description

      Here is my pipeline code:

          Map scm = [
              $class: 'GitSCM',
              extensions: [
                  [$class: 'RelativeTargetDirectory', relativeTargetDir: conf.dir],
                  [$class: 'CleanCheckout'],
                  [$class: 'PruneStaleBranch']
              ],
              browser: [
                  $class: 'GitLab',
                  repoUrl: "https://hide/${conf.name}", version: '8.16'
              ],
              userRemoteConfigs: [[
                  credentialsId: 'Gitlab-CI-SSH-KEY',
                  refspec: '+refs/merge-requests/*/head' +
                           ':refs/remotes/origin/merge-requests/*',
                  url: "git@hide:${conf.name}.git"
              ]]
          ]
      
          switch(env.gitlabActionType) {
              case 'PUSH':
                  scm.branches = [[name: "${env.gitlabSourceBranch}"]]
                  break
              case 'MERGE':
                  scm.branches = [[name: "merge-requests/${env.gitlabMergeRequestIid}/head"]]
                  break
              default:
                  scm.branches = [[name: conf.refs ?: 'master' ]]
          }
      
          echo "Using this scm branches config: ${scm.branches}"
          checkout scm
      

      But I find the exist branches not update. Here is the console output:

      Using this scm branches config: [[name:dev]]
      [Pipeline] checkout
       > git rev-parse --is-inside-work-tree # timeout=10
      Fetching changes from the remote Git repository
       > git config remote.origin.url git@hide:devops/Jenkinsfiles.git # timeout=10
      Pruning obsolete local branches
      Fetching upstream changes from git@hide:devops/Jenkinsfiles.git
       > git --version # timeout=10
      using GIT_SSH to set credentials gitlab ci用户使用的ssh key
       > git fetch --tags --progress git@hide:devops/Jenkinsfiles.git +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/* --prune
       > git rev-parse origin/dev^{commit} # timeout=10
      Checking out Revision 1065454f788597d370a34de5f05570cc9074b618 (origin/dev)
       > git config core.sparsecheckout # timeout=10
       > git checkout -f 1065454f788597d370a34de5f05570cc9074b618
       > git rev-list 26d6a193b0b56f67d526cefee5d5c3199d168e75 # timeout=10
      Cleaning workspace
       > git rev-parse --verify HEAD # timeout=10
      Resetting working tree
       > git reset --hard # timeout=10
       > git clean -fdx # timeout=10
      

      Jenkins think origin/dev sha-1 is 1065454f788597d370a34de5f05570cc9074b618, but in fact it's 26d6a19. I try to access to jenkins workspace and exec git fetch, the branches updated:

      $ git fetch 
      remote: Counting objects: 4, done.
      remote: Compressing objects: 100% (4/4), done.
      remote: Total 4 (delta 0), reused 0 (delta 0)
      Unpacking objects: 100% (4/4), done.
      From hide:devops/Jenkinsfiles
         1065454..26d6a19  dev        -> origin/dev
         f6b526b..e70443c  master     -> origin/master
      

      So git plugin does not update the exist branches, because git fetch --tags does not fetch the exist branches, see: http://stackoverflow.com/questions/1204190/does-git-fetch-tags-include-git-fetch

      EDIT:
      It's my fault, because I set the wrong refspec. I've edited my title. According to gitlab docs, to get merge request must set another git refspec: https://docs.gitlab.com/ee/user/project/merge_requests/index.html#checkout-locally-by-modifying-git-config-for-a-given-repository

      How to do this?

        Attachments

          Activity

          Hide
          markewaite Mark Waite added a comment - - edited

          Please ask questions about usage on the jenkins users mailing list.

          Asking a question in a bug report requires time from the very few people who read bug reports on the git plugin, while questions on the jenkins users mailing list spreads the time across many more people.

          Since the online help doesn't describe how to do that, I've added online help which describes how to do it.

          Show
          markewaite Mark Waite added a comment - - edited Please ask questions about usage on the jenkins users mailing list. Asking a question in a bug report requires time from the very few people who read bug reports on the git plugin, while questions on the jenkins users mailing list spreads the time across many more people. Since the online help doesn't describe how to do that, I've added online help which describes how to do it.
          Hide
          scm_issue_link SCM/JIRA link daemon added a comment -

          Code changed in jenkins
          User: Mark Waite
          Path:
          src/main/resources/hudson/plugins/git/UserRemoteConfig/help-refspec.html
          http://jenkins-ci.org/commit/git-plugin/d2c36cf91698ca1e890e291b824cc976058e596e
          Log:
          JENKINS-42050 Add help for multiple refspecs

          Show
          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Mark Waite Path: src/main/resources/hudson/plugins/git/UserRemoteConfig/help-refspec.html http://jenkins-ci.org/commit/git-plugin/d2c36cf91698ca1e890e291b824cc976058e596e Log: JENKINS-42050 Add help for multiple refspecs
          Hide
          markewaite Mark Waite added a comment -

          Help was added in Git plugin 3.1.0 released on 4 Mar 2017.

          Show
          markewaite Mark Waite added a comment - Help was added in Git plugin 3.1.0 released on 4 Mar 2017.

            People

            • Assignee:
              markewaite Mark Waite
              Reporter:
              abcfy2 feng yu
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: