Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkout SVN with credentials in Jenkins pipeline?

Tags:

How can I check out a Subversion repository that requires user credentials, via a Jenkins pipeline groovy script? It appears that the built-in svn command doesn't support credentials, so I tried code like this:

node {     stage 'checkout'     withCredentials([[$class: 'UsernamePasswordMultiBinding',                       credentialsId: '34761a89-1402-47d7-96e2-aec22ffdc50b',                       usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {         sh "svn co https://trac.nci.org.au/svn/cable/branches/$SVN_BRANCH --username $USERNAME --password $PASSWORD cable_branch"     } } 

But this fails with

groovy.lang.MissingPropertyException: No such property: USERNAME for class: groovy.lang.Binding     at groovy.lang.Binding.getVariable(Binding.java:63)     at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)     at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)     at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)     at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)     at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)     at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:23)     at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17)     at WorkflowScript.run(WorkflowScript:5)     at ___cps.transform___(Native Method)     ... 

What am I missing?

like image 821
naught101 Avatar asked Apr 27 '16 05:04

naught101


People also ask

How do I add SVN credentials to Jenkins?

Next to the Repository URL text box click the question mark. In that box will be the following text: "click this link and specify different credential". Click on that link to open the Subversion Authentication page. On that page enter the root of the repository URL, enter the login and password to use and click OK.

How does Jenkins integrate with SVN?

Now create a Jenkins build job. Select New Item, give the build project a name such as svn-tomcat-demo, select Maven project, and click OK. Under source code management, select Subversion and enter your SVN repository URL and credential. Please download the sample code and check the code into your SVN server.

Can Jenkins pull code from SVN?

Essentially, what you can do is setup a svn commit hook that sends a signal to Jenkins to kick off a build. Jenkins will then (given the right build setup) pull the freshly-committed code and run the build.

What does checkout scm mean?

1. The checkout step will checkout code from source control; scm is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline run.


2 Answers

You can use the Snippet Generator for General SCM step. This displays the familiar Subversion configuration options, and takes credentials as parameter as usual.

The Snippet Generator will produce a tad ugly representation of your parameter selections and looks something like this:

checkout([$class: 'SubversionSCM',            additionalCredentials: [],            excludedCommitMessages: '',            excludedRegions: '',            excludedRevprop: '',            excludedUsers: '',            filterChangelog: false,            ignoreDirPropChanges: false,            includedRegions: '',            locations: [[credentialsId: '34761a89-1402-47d7-96e2-aec22ffdc50b',                         depthOption: 'infinity',                         ignoreExternalsOption: true,                         local: 'cable_branch',                         remote: "https://trac.nci.org.au/svn/cable/branches/$SVN_BRANCH"]],            workspaceUpdater: [$class: 'UpdateUpdater']]) 

Notice that the remote section uses double quotes, so that the variable $SVN_BRANCH gets substituted correctly.

like image 113
OltzU Avatar answered Sep 21 '22 14:09

OltzU


Just adding some screen shots for OltzU's answer:

Step 1:

enter image description here

Step 2:

enter image description here

like image 23
Robin Qiu Avatar answered Sep 22 '22 14:09

Robin Qiu