Clean builds with Multibranch Workflow

2020-08-09 10:40发布

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.

5条回答
ゆ 、 Hurt°
2楼-- · 2020-08-09 11:14

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 :(

查看更多
虎瘦雄心在
3楼-- · 2020-08-09 11:16

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.

查看更多
我命由我不由天
4楼-- · 2020-08-09 11:17

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']]]
查看更多
Summer. ? 凉城
5楼-- · 2020-08-09 11:32

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').

查看更多
时光不老,我们不散
6楼-- · 2020-08-09 11:33

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 
}
查看更多
登录 后发表回答