GitSCM.extensions such as CleanBeforeCheckout cannot currently be configured on GitSCMSource.

          [JENKINS-31924] GitSCMSource should offer extensions

          Colin Bennett added a comment - - edited

          Workaround for "Clean Before Checkout" Equivalent

          I tried a few ways to get the equivalent of a Git Clean Before Checkout behavior in a Multibranch Pipeline job. Here is what is working for me now: using a manual call to git clean, falling back on deleteDir() pipeline statement if there is no existing Git repository in the workspace:

          node {
              gitClean()
              checkout scm
              /* ... rest of the build ... */
          }
          
          /**
           * Clean a Git project workspace.
           * Uses 'git clean' if there is a repository found.
           * Uses Pipeline 'deleteDir()' function if no .git directory is found.
           */
          def gitClean() {
              timeout(time: 60, unit: 'SECONDS') {
                  if (fileExists('.git')) {
                      echo 'Found Git repository: using Git to clean the tree.'
                      // The sequence of reset --hard and clean -fdx first
                      // in the root and then using submodule foreach
                      // is based on how the Jenkins Git SCM clean before checkout
                      // feature works.
                      bat 'git reset --hard'
                      // Note: -e is necessary to exclude the temp directory
                      // .jenkins-XXXXX in the workspace where Pipeline puts the
                      // batch file for the 'bat' command.
                      bat 'git clean -ffdx -e ".jenkins-*/"'
                      bat 'git submodule foreach --recursive git reset --hard'
                      bat 'git submodule foreach --recursive git clean -ffdx'
                  }
                  else
                  {
                      echo 'No Git repository found: using deleteDir() to wipe clean'
                      deleteDir()
                  }
              }
          }
          
          

          Colin Bennett added a comment - - edited Workaround for "Clean Before Checkout" Equivalent I tried a few ways to get the equivalent of a Git Clean Before Checkout behavior in a Multibranch Pipeline job. Here is what is working for me now: using a manual call to git clean , falling back on deleteDir() pipeline statement if there is no existing Git repository in the workspace: node { gitClean() checkout scm /* ... rest of the build ... */ } /** * Clean a Git project workspace. * Uses 'git clean' if there is a repository found. * Uses Pipeline 'deleteDir()' function if no .git directory is found. */ def gitClean() { timeout(time: 60, unit: 'SECONDS' ) { if (fileExists( '.git' )) { echo 'Found Git repository: using Git to clean the tree.' // The sequence of reset --hard and clean -fdx first // in the root and then using submodule foreach // is based on how the Jenkins Git SCM clean before checkout // feature works. bat 'git reset --hard' // Note: -e is necessary to exclude the temp directory // .jenkins-XXXXX in the workspace where Pipeline puts the // batch file for the 'bat' command. bat 'git clean -ffdx -e ".jenkins-*/" ' bat 'git submodule foreach --recursive git reset --hard' bat 'git submodule foreach --recursive git clean -ffdx' } else { echo 'No Git repository found: using deleteDir() to wipe clean' deleteDir() } } }

          Jesse Glick added a comment -

          Probably implemented in PR 350; needs to be verified.

          Jesse Glick added a comment - Probably implemented in PR 350; needs to be verified.

          Mark Waite added a comment -

          Included in git plugin 2.5.2, released July 2016

          Mark Waite added a comment - Included in git plugin 2.5.2, released July 2016

          Adi Sutanto added a comment -

          markewaite, you mentioned "Included in git plugin 2.5.2, released July 2016". But the Git Plugin changelog doesn't mention anything about Git Clean Before Checkout in a Multibranch Pipeline job. Would you please teach us how to do it?

          Adi Sutanto added a comment - markewaite , you mentioned "Included in git plugin 2.5.2, released July 2016". But the Git Plugin changelog doesn't mention anything about Git Clean Before Checkout in a Multibranch Pipeline job. Would you please teach us how to do it?

          Mark Waite added a comment - - edited

          adisutanto the changelog doesn't mention specific extensions because the change made them all available.

          Refer to the attached picture for the job configuration screen

          Mark Waite added a comment - - edited adisutanto the changelog doesn't mention specific extensions because the change made them all available. Refer to the attached picture for the job configuration screen

          Adi Sutanto added a comment -

          markewaite, thank you very much!

          Adi Sutanto added a comment - markewaite , thank you very much!

          trejkaz added a comment -

          Trying to get into the configure page to configure this, but it's taking an awfully long time to load.

          trejkaz added a comment - Trying to get into the configure page to configure this, but it's taking an awfully long time to load.

            Unassigned Unassigned
            jglick Jesse Glick
            Votes:
            16 Vote for this issue
            Watchers:
            23 Start watching this issue

              Created:
              Updated:
              Resolved: