ConfusedAgain
ConfusedAgain

Reputation: 83

How do I set up a Jenkins Pipeline global library using perforce as the SCM?

I've spent many hours on this without any success at all. According to this I should be able to use any available SCM but I don't know how to map the paths, where, if anywhere, to insert the ${library.RegLib.version} or what workspace name to use.

I have a library set up as per the abovementioned docs:

<root>/src/org/somelib/MyLib.groovy

which contains:

package org.registration;

def doTest() {
    echo "test running..."
}

I've tried many different things but nothing works. I've also tried restarting Jenkins, as mentioned here. No change.

My build reports:

Loading library MyLib@#1
java.lang.ArrayIndexOutOfBoundsException: 1
    at org.jenkinsci.plugins.p4.tasks.AbstractTask.setEnvironment(AbstractTask.java:106)
    at org.jenkinsci.plugins.p4.PerforceScm.checkout(PerforceScm.java:391)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:109)
    at org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever.doRetrieve(SCMSourceRetriever.java:107)
    at org.jenkinsci.plugins.workflow.libs.SCMRetriever.retrieve(SCMRetriever.java:63)
    at org.jenkinsci.plugins.workflow.libs.LibraryAdder.retrieve(LibraryAdder.java:150)
    at org.jenkinsci.plugins.workflow.libs.LibraryAdder.add(LibraryAdder.java:131)
    at org.jenkinsci.plugins.workflow.libs.LibraryDecorator$1.call(LibraryDecorator.java:99)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1053)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:67)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:429)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:392)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:221)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:404)
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: Loading libraries failed

"Default version" is set to 1 because there's only been one commit. I've also tried #1. I don't know whether to map specific files or the top-level directory. If I remove the default version the build fails and complains that I haven't set a version. It's supposed to be optional but clearly isn't.

I've also tried using the vars directory

<root>/vars/doTest.groovy

which contains:

def call(msg) {
    echo msg
}

but I presume that also requires the library to be loaded. The docs are unclear about that.

So...

Will this work with perforce?

How do I map the paths to make it work?

How do I make the code in vars accessible? Is that loaded as part of the overall library?

Is there an error somewhere in my code?

Many thanks.

Upvotes: 1

Views: 7652

Answers (4)

ConfusedAgain
ConfusedAgain

Reputation: 83

In reply to #HS10, I did and I've been meaning to update this for the benefit of others for ages but everything else in life seems to become higher priority. Since you've asked, here's what I did.

In Jenkins/Configuration, under Global Pipeline Libraries I set the following:

enter image description here

Specifically, provide a Name and set Default version tohead. Set the Retrieval method to Legacy SCM. Perforce doesn't have Modeern SCM support, yet. Under Source Code Management select Perforce Software. Note that this is the p4 plugin, not the old Perforce one which is listed as Perforce. I suspect that it's important to use the version written by Perforce themselves. Select a Credential that you have configured and provide a matching workspace name and mapping. I may have had that wrong earlier, I don't know. Other settings should be at your discretion. The library directory structure is as per the docs. I did think for a while that the workspace name had to be _global_lib but recent experiments appear to have disproved that.

In your pipeline, import the library like this:

@Library('plib') _

// do something

You should now have a working library.

I think I had this wrong earlier, as well. Note that the underscore is important. See the Global Lirary docs for more details. Getting this working caused me a lot of pain so I hope this saves someone from having a similar experience.

Upvotes: 0

HS10
HS10

Reputation: 21

This is really late, but I was wondering if you found a solution.

In Amityo's answer you commented that your Perforce source path is //<prod>/trunk/src/apps/jenkinslib#${library.RegLib.version}/..., where ${library.RegLib.version} = 1 if no other version is explicitly specified in pipeline.

I think Jenkins will literally look for a folder named jenkinslib#1, which it won't find since your folder is just named jenkinslib.

I don't know how you would set up your structure to support different versions, but maybe having just //<prod>/trunk/src/apps/jenkinslib/... as your source path in the map might work, even though the config page tells you to add library.RegLib.version.

I would've commented all this on Amityo's post instead but I don't have enough reputation to do so.

Upvotes: 0

RRT
RRT

Reputation: 156

Seems to be an open issue with the p4 plugin, related to the p4 plugin being unable to deal with perforce checkouts at locations different from the workspace root:

https://issues.jenkins-ci.org/browse/JENKINS-40055

https://issues.jenkins-ci.org/browse/JENKINS-36243

Edit: You may be able to get this to work using older plugin versions, according to the reporter of the first issue:

The crash is not present in version 2.4 of workflow-cps-global-lib, it started to happen in version 2.5 only.

Upvotes: 0

Amityo
Amityo

Reputation: 6321

Install Pipeline Shared Libraries Plugin.

The configuration is in Manage Jenkins -> Global Pipeline Libraries

The retrieval method should be legacy mode. add repository

Tick the Load implicitly to load the scripts in every build

Put the groovy files in vars/yourGroovy.groovy and call it from Jenkinsfile:

yourGroovy()

Upvotes: 1

Related Questions