4

I am using a multibranch pipeline with SVN. I currently don't use an explicit checkout scm command but I am using the declarative default checkout. My problem is, that this only seems to perform an svn update. I would however like to use the UpdateWithCleanUpdater update strategy.

I have seen here that there is a possibility to modify the scm object - but not without granting further permissions:

import hudson.scm.subversion.UpdateWithCleanUpdater
scm.setWorkspaceUpdater(new UpdateWithCleanUpdater())

Is there a simple way to only configure the updater while keeping all the other information from the multibranch configuration and having to set the branch, credentials, server, .... again manually?

I have tried checkout scm: [workspaceUpdater: [$class: 'UpdateWithCleanUpdater']] but this doesn't work

nogenius
  • 574
  • 1
  • 6
  • 18

2 Answers2

1

There is a built-in generator that comes with Jenkins at <jenkins-url>/pipeline-syntax e.g. http://localhost/pipeline-syntax

  1. Under Sample Step, use the "checkout" step instead of "svn".
  2. SCM: Subversion
  3. Enter the URL and Credentials to use
  4. Check-out Strategy: can be "Always check out fresh" or "svn revert before update". Up to you, the latter one is faster and has not yet failed in my experience. (see image below)
  5. Click "Generate Pipeline Script", and copy the output into your pipeline.

The sample output will look like this:

checkout(
    [$class: 'SubversionSCM',
    additionalCredentials: [], excludedCommitMessages: '',
    excludedRegions: '', excludedRevprop: '', excludedUsers: '',
    filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '',
    locations: [[cancelProcessOnExternalsFail: true,
    credentialsId: '********-********-*******',
    depthOption: 'infinity', ignoreExternalsOption: true,
    local: '.', remote: 'http://*****/*****/trunk']],
    quietOperation: true,
    workspaceUpdater: [$class: 'CheckoutUpdater']]
)

You can have as many of these lines as you want on your pipeline script, with the remote URL changing depending on the branch. In my case, I have two repositories that need to sit next to each other when checked out, so I have two checkout commands in my script.

Note: "Always fresh" uses the CheckoutUpdater class, and "revert before update" uses UpdateWithRevertUpdater. The generated output is usable in both Declarative and Scripted pipeline syntax.

jenkins snippet generator

bytehala
  • 665
  • 1
  • 10
  • 25
0

In a declarative pipeline stage, make sure that the options contain:

options {
  skipDefaultCheckout()
}

and the steps section starts with a script block like:

script {
  myScm = scm
  myScm.setWorkspaceUpdater(new hudson.scm.subversion.UpdateWithRevertUpdater())
  checkout myScm
}

More info on the different types of workspace updaters for Subversion is here: https://javadoc.jenkins.io/plugin/subversion/hudson/scm/subversion/WorkspaceUpdater.html

And yes, you may need to approve some scripts on the approval page:

http://<myjenkins>/scriptApproval/
Marnix
  • 1
  • 1