17

Using Multibranch Workflow, the command to check out looks like

checkout scm

I can't find a way to tell Jenkins to perform a clean checkout. By "clean," I mean it should remove all files from the workspace that aren't under version control.

Chris Jones
  • 4,815
  • 6
  • 34
  • 28
  • 4
    I don't know whether `checkout` can take further parameters in multibranch. I imagine there is a more elegant solution, but if you're using Git, you should be able to run `sh 'git clean -fdx'` after the `checkout` step. – Christopher Orr Dec 10 '15 at 23:29

6 Answers6

13

I'm not sure if this answers the original question or not (I couldn't tell if the intention was to leave some files in the workspace) but why not just remove the workspace first, this would allow a clean checkout:

stage ('Clean') {
    deleteDir()
}

stage ('Checkout') {
    checkout scm 
}
Brad Albright
  • 686
  • 7
  • 12
  • 1
    Might be worth noting that you could just do a `sh 'git clean -fdx'` after checkout instead of deleting everything as @christopher-orr suggested – Clintm May 04 '21 at 14:50
12

I run into the same problem and here is my workaround. I created a new scm object for the checkout and extended the extensions with the CleanBeforeCheckout. But i kept the other configurations like branches and userRemoteConfigs.

checkout([
    $class: 'GitSCM',
    branches: scm.branches,
    extensions: scm.extensions + [[$class: 'CleanBeforeCheckout']],
    userRemoteConfigs: scm.userRemoteConfigs
])

It's still not perfect because you have to create a new object :(

hEngi
  • 865
  • 10
  • 23
3

First, you can not assume that a workflow job has a workspace as it was for freestyle jobs. Actually, a workflow job can use more than one workspace (one for each node or ws block).

Said that, what I'm going to propose is a kind of hacky: modify the scm object before checkout to set up a CleanCheckout extension (you will have to approve some calls there).

import hudson.plugins.git.extensions.impl.CleanCheckout
scm.extensions.replace(new CleanCheckout())
checkout scm

But I'd prefer Christopher Orr's proposal, use a shell step after checkout (sh 'git clean -fdx').

amuniz
  • 3,292
  • 2
  • 20
  • 22
  • 1
    Thanks for that. The problem with both suggestions is that it only works with Git. – Chris Jones Dec 17 '15 at 21:54
  • 1
    There are equivalents for most SCM implementations. For example, for subversion you can use `scm.setWorkspaceUpdater(new UpdateWithCleanUpdater())` – amuniz Dec 21 '15 at 14:31
  • Where can I find the documentation on these things? (I'm using Hg.) – Chris Jones Dec 22 '15 at 15:53
  • There is no documentation for that, they are low level details and you have to check the source code to get it. – amuniz Jan 08 '16 at 11:37
  • what do you think about @TomDotTom's proposal in the other answer? – Janus Troelsen Jul 13 '16 at 14:59
  • 2
    Well, that's basically not correct for Multibranch Pipelines, where you already have the `scm` object created. Adding that new `checkout` step would add another `scm` object to the build and it has side effects (like duplicated changelog entries in the build page). – amuniz Jul 13 '16 at 15:10
1

workflow behavior

Behaviors can be added when configuring the source. clean before checkout, clean after checkout and Wipe out repository and force clone. This removes the need to add logic to the declarative / scripted pipelines.

bruce szalwinski
  • 724
  • 1
  • 8
  • 27
0

Adding Christopher-Orr's comment as an answer to just do:

stage('Checkout') {
  checkout scm
  sh 'git clean -fdx'
}
Clintm
  • 4,505
  • 3
  • 41
  • 54
-1

Jenkins currently contains a page to generate groovy pipeline syntax. Selecting the checkout step you should be able to add all the additional options that you're used to.

I generated the following which should do what you want:

checkout poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://repo/location.git']]]
TomDotTom
  • 6,238
  • 3
  • 41
  • 39