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

Jenkins uses the same directory for Jenkinsfile pull and project workspace, branches are mixed up

    Details

    • Similar Issues:

      Description

      Jenkins 2.89.3
      Git client plugin 2.7.1
      Git plugin 3.8.0
      Pipeline 2.5


      Create pipeline from SCM, use wrong as local branch name.

      Jenkinsfile:

      
      pipeline {
          agent any
      
          stages {
              stage ('SCM') {
                  steps {
                      script {
                          echo '1'
                          bat script: 'git remote -v'
                          bat script: 'git branch'
      
                          checkout([$class: 'GitSCM', branches: [[name: 'develop']],
                              extensions: [[$class: 'LocalBranch', localBranch: 'expected']],
                              //extensions: [[$class: 'LocalBranch', localBranch: 'expected'], [$class: 'WipeWorkspace']],
                              userRemoteConfigs: [[url: 'ssh://gitblit.example.com/anton/jenkins-project.git',
                                  credentialsId: 'jenkins-ssh-credential']]])
      
                          echo '2'
                          bat script: 'git remote -v'
                          bat script: 'git branch'
                      }
                  }
              }
          }
      }

      First build (Log1.txt)

      Branches after Jenkinsfile checkout:

      * wrong
      

      Branches after project repo checkout:

      * expected
        wrong
      

      An unexpected branch wrong exists in the list.

      Second build (Log2.txt)

      Branches after Jenkinsfile checkout:

        expected
      * wrong
      

      An unexpected branch expected exists in the list.

      Branches after project repo checkout:

      * expected
        wrong
      

      An unexpected branch wrong exists in the list.

      Build with WipeWorkspace extension (LogWipe.txt)

      Branches after Jenkinsfile checkout:

      * wrong
      
      * expected
      

      Everything is good, but I don't need to clone the repository every time.

      PruneStaleBranch extension does not help (I guess it deletes only tracked branches).


      1. Why does Jenkins use my workspace to checkout Jenkisfile? I expect it to use a temp dir, like libLoader does.
      2. Why does it mix branches for unrelated repositories? Does it manually change .git/ content somehow?

      Similar issue: JENKINS-30619

        Attachments

        1. 2018-02-28_1145.png
          2018-02-28_1145.png
          51 kB
        2. Log1.txt
          5 kB
        3. Log2.txt
          4 kB
        4. LogWipe.txt
          5 kB

          Activity

          Hide
          markewaite Mark Waite added a comment -

          This is behaving as expected.

          Declarative Pipeline (which is what you are using) begins with an implicit checkout of the repository using the repository definition from the Jenkins job.

          In your case, the implicit checkout will create a branch named "wrong" in the workspace at the most recent commit on the origin/develop branch. Refer to skipDefaultCheckout in the Declarative Pipeline options if you do not need the default checkout.

          The default checkout does not seem useful in your example since the checkout in the Jenkinsfile is reusing the workspace directory. Most Declarative Pipeline users use the default checkout to avoid cluttering the Jenkinsfile with a checkout definition. If additional checkout calls are needed, they are typically performed into new subdirectories of the workspace.

          If you want the branch name in the implicit checkout to match the name provided in "Branches to build", the online help says to either leave the field blank or use the value '**'.

          Show
          markewaite Mark Waite added a comment - This is behaving as expected. Declarative Pipeline (which is what you are using) begins with an implicit checkout of the repository using the repository definition from the Jenkins job. In your case, the implicit checkout will create a branch named "wrong" in the workspace at the most recent commit on the origin/develop branch. Refer to skipDefaultCheckout in the Declarative Pipeline options if you do not need the default checkout. The default checkout does not seem useful in your example since the checkout in the Jenkinsfile is reusing the workspace directory. Most Declarative Pipeline users use the default checkout to avoid cluttering the Jenkinsfile with a checkout definition. If additional checkout calls are needed, they are typically performed into new subdirectories of the workspace. If you want the branch name in the implicit checkout to match the name provided in "Branches to build", the online help says to either leave the field blank or use the value ' ** '.
          Hide
          antonzimin Anton Zimin added a comment -

          The default checkout does not seem useful in your example since the checkout in the Jenkinsfile is reusing the workspace directory.

          I want to use Pipeline script from SCM option. That's why I need the default checkout.

          If additional checkout calls are needed, they are typically performed into new subdirectories of the workspace.

          Thanks, that solves the problem. Need to use ws statement in all stages that work with the second checkout.

          ws ('another-workspace') {
          checkout ...
          }
          
          Show
          antonzimin Anton Zimin added a comment - The default checkout does not seem useful in your example since the checkout in the Jenkinsfile is reusing the workspace directory. I want to use Pipeline script from SCM option. That's why I need the default checkout. If additional checkout calls are needed, they are typically performed into new subdirectories of the workspace. Thanks, that solves the problem. Need to use ws statement in all stages that work with the second checkout. ws ( 'another-workspace' ) { checkout ... }

            People

            • Assignee:
              Unassigned
              Reporter:
              antonzimin Anton Zimin
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: