If you are actually looking for a way to populate Jenkins with Pipeline type 
jobs in an automated manner, then the best bet is to use the Job DSL plugin. 
i.e. the Job DSL and Pipeline combination will allow to have everything in SCM 
and you can bootstrap the full job creation from SCM. The Pipeline type job 
alone does not allow this. As far as I'm concerned that was a major oversight.

Best regards.

Artur


________________________________________
From: [email protected] <[email protected]> on behalf 
of Matt Q <[email protected]>
Sent: 25 January 2019 22:38:12
To: Jenkins Developers
Subject: Generate/Execute Pipeline from plugin programatically?

Is there an existing Java API for generating Jenkins Pipelines programmatically?

I tried to execute a simple declarative pipeline as a Groovy Postbuild step:

#!/usr/bin/env groovy
pipeline {
agent any
stages {
stage('Test Stage') {
steps {
echo '----- In Test Stage------'
}
}
}
}


in Java code as follows:

import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder;
import hudson.model.Project;

...

String pipeScript = new String("script above as a String");
SecureGroovyScript secureScript = new SecureGroovyScript(pipeScript, 
/*sandbox*/false, /*classpath*/null);
GroovyPostbuildRecorder groovy = new GroovyPostbuildRecorder(secureScript, 
/*behaviour*/2, /*matrix parent*/false);
myProject.getPublishersList().add(groovy);


This results in the stacktrace:


ERROR: Failed to evaluate groovy script.
groovy.lang.MissingMethodException<http://stacktrace.jenkins-ci.org/search?query=groovy.lang.MissingMethodException>:
 No signature of method: Script1.pipeline() is applicable for argument types: 
(Script1$_run_closure1) values: [Script1$_run_closure1@111b5ac]
        at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)<http://stacktrace.jenkins-ci.org/search/?query=org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap&entity=method>
        at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
        at 
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:151)
        at 
org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21)
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:75)
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:68)
        at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
        at 
org.kohsuke.groovy.sandbox.impl.Checker$checkedCall.callStatic(Unknown Source)
        at 
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
        at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
        at Script1.run(Script1.groovy:2)
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.run(GroovySandbox.java:139)
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:161)
        at 
org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:362)
        at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
        at 
hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
        at 
hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:720)
        at hudson.model.Build$BuildExecution.post2(Build.java:185)
        at 
hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:665)
        at hudson.model.Run.execute(Run.java:1766)
        at 
com.tikal.jenkins.plugins.multijob.MultiJobBuild.run(MultiJobBuild.java:73)
        at hudson.model.ResourceController.execute(ResourceController.java:98)
        at hudson.model.Executor.run(Executor.java:410)
Build step 'Groovy Postbuild' marked build as failure
Finished: FAILURE


I assume this is because the Domain Specific Language used for Pipelines is 
similar to but not actually Groovy. Is there an existing plugin or API to 
accomplish this?


Thanks,

-Matt

--
You received this message because you are subscribed to the Google Groups 
"Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-dev/8acadf56-6c3b-40af-8567-f46c62fe8abb%40googlegroups.com<https://groups.google.com/d/msgid/jenkinsci-dev/8acadf56-6c3b-40af-8567-f46c62fe8abb%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-dev/7b757654284747b9a4eb592ba56d9977%40partner.eso.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to