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

How to set multiple git refspec in checkout step?


    • Icon: Improvement Improvement
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • git-plugin
    • None
    • git plugin version: 3.0.5
      git version: 2.11.0

      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' +
                  url: "git@hide:${conf.name}.git"
          switch(env.gitlabActionType) {
              case 'PUSH':
                  scm.branches = [[name: "${env.gitlabSourceBranch}"]]
              case 'MERGE':
                  scm.branches = [[name: "merge-requests/${env.gitlabMergeRequestIid}/head"]]
                  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

      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?

            markewaite Mark Waite
            abcfy2 feng yu
            0 Vote for this issue
            3 Start watching this issue