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

How to set multiple git refspec in checkout step?

    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

          abcfy2 feng yu created issue -
          abcfy2 feng yu made changes -
          Field Original Value New Value
          Description Here is my pipeline code:

          {{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
          {{code}}

          But I find the exist branches not update. Here is the console output:
          {{code}}
          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
          {{code}}

          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:
          {{code}}
          $ 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
          {{code}}

          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
          Here is my pipeline code:

          {{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
          {{/code}}

          But I find the exist branches not update. Here is the console output:
          {{code}}
          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
          {{/code}}

          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:
          {{code}}
          $ 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
          {{/code}}

          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
          abcfy2 feng yu made changes -
          Description Here is my pipeline code:

          {{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
          {{/code}}

          But I find the exist branches not update. Here is the console output:
          {{code}}
          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
          {{/code}}

          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:
          {{code}}
          $ 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
          {{/code}}

          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
          Here is my pipeline code:

          {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
          {code}

          But I find the exist branches not update. Here is the console output:
          {code}
          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
          {code}

          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:
          {code}
          $ 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
          {code}

          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
          abcfy2 feng yu made changes -
          Summary git fetch --all not update exist branches How to set multiple git refspec in checkout step?
          abcfy2 feng yu made changes -
          Description Here is my pipeline code:

          {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
          {code}

          But I find the exist branches not update. Here is the console output:
          {code}
          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
          {code}

          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:
          {code}
          $ 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
          {code}

          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
          Here is my pipeline code:

          {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
          {code}

          But I find the exist branches not update. Here is the console output:
          {code}
          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
          {code}

          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:
          {code}
          $ 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
          {code}

          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
          abcfy2 feng yu made changes -
          Description Here is my pipeline code:

          {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
          {code}

          But I find the exist branches not update. Here is the console output:
          {code}
          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
          {code}

          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:
          {code}
          $ 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
          {code}

          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
          Here is my pipeline code:

          {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
          {code}

          But I find the exist branches not update. Here is the console output:
          {code}
          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
          {code}

          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:
          {code}
          $ 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
          {code}

          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?
          markewaite Mark Waite made changes -
          Status Open [ 1 ] Closed [ 6 ]
          Resolution Not A Defect [ 7 ]

            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: