Plugin for freestyle job and pipeline with environment variables

198 views
Skip to first unread message

Matthias

unread,
Oct 7, 2019, 10:07:17 AM10/7/19
to Jenkins Developers
Hello

I implemented a plugin that works as a build step for freestyle jobs and as a pipeline step in groovy by implementing SimpleBuildStep.
Now, I need to pass credentials via environment variables to the plugin. I found out that this is not possible with SimpleBuildStep (https://issues.jenkins-ci.org/browse/JENKINS-29144).

Searching further, I found advices to extend Step instead. I finally got it working for the pipeline by using the following classes.

public class MyPlugin extends Step implements Serializable {

@Extension
@Symbol("myPlugin")
public static final class DescriptorImpl extends StepDescriptor {

public class MyPluginStepExecution extends SynchronousNonBlockingStepExecution<Void> {


However, my problem is now that the plugin is not anymore selectable from the "add build step" dropdown in freestyle jobs. Do I have to use another interface for this?

I tried to use SimpleBuildWrapper, BuildWrapperDescriptor, and StepExecution but now I only get "java.lang.IllegalStateException: There is no body to invoke"

Jesse Glick

unread,
Oct 7, 2019, 11:11:22 AM10/7/19
to Jenkins Dev
On Mon, Oct 7, 2019 at 10:07 AM Matthias <matthia...@posteo.de> wrote:
> Now, I need to pass credentials via environment variables to the plugin. I found out that this is not possible with SimpleBuildStep (JENKINS-29144).

You mean from an enclosing `withCredentials` step? Indeed that is a
scoped environment variable, rather than build-wide, so not currently
available from this API.

> I found advices to extend Step instead. I finally got it working for the pipeline

Looking up `getContext().get(EnvVars.class)` I guess.

> my problem is now that the plugin is not anymore selectable from the "add build step" dropdown in freestyle jobs. Do I have to use another interface for this?

Yes, `Step` is for Pipeline only. If you want a freestyle-only build
step, use `Builder` or `Publisher` without implementing
`SimpleBuildStep`. Basically `SimpleBuildStep` just offers the
convenience of writing a single extension which can be used for any
kind of project.

Matthias

unread,
Oct 7, 2019, 12:52:56 PM10/7/19
to Jenkins Developers
Understood.
But is there something where I can go with just one plugin for Freestyle and Pipeline not having the drawback of the missing environment variables?

Slide

unread,
Oct 7, 2019, 12:57:34 PM10/7/19
to jenkin...@googlegroups.com
Do you mean the same class? Having a pipeline step and a Builder/Notifier/etc implemented in the same plugin works just fine. You can look at the JUnit plugin for an example of something that has both a pipeline Step implementation and a Publisher implemented for use in freestyle.

On Mon, Oct 7, 2019 at 9:53 AM Matthias <matthia...@posteo.de> wrote:
Understood.
But is there something where I can go with just one plugin for Freestyle and Pipeline not having the drawback of the missing environment variables?

--
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 jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/aa8f7fd0-23f5-44a2-aa1d-685015820419%40googlegroups.com.


--

Matthias

unread,
Oct 7, 2019, 1:35:48 PM10/7/19
to Jenkins Developers
I compared it to my implementation and its structure looks similar to what I described above.

Am Montag, 7. Oktober 2019 18:57:34 UTC+2 schrieb slide:
Do you mean the same class? Having a pipeline step and a Builder/Notifier/etc implemented in the same plugin works just fine. You can look at the JUnit plugin for an example of something that has both a pipeline Step implementation and a Publisher implemented for use in freestyle.

On Mon, Oct 7, 2019 at 9:53 AM Matthias <matthia...@posteo.de> wrote:
Understood.
But is there something where I can go with just one plugin for Freestyle and Pipeline not having the drawback of the missing environment variables?

--
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 jenkin...@googlegroups.com.

Slide

unread,
Oct 7, 2019, 2:02:44 PM10/7/19
to jenkin...@googlegroups.com
Yes, that is the pipeline step implementation, then  https://github.com/jenkinsci/junit-plugin/blob/master/src/main/java/hudson/tasks/junit/JUnitResultArchiver.java is the implementation of the free style step (a Recorder).

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/fda513af-0c0e-4979-b1cd-10f39cb29e1a%40googlegroups.com.


--

Matthias

unread,
Oct 7, 2019, 2:09:35 PM10/7/19
to Jenkins Developers
I see. But then again I still have the problem with the missing environment variables from the groovy script in the builder class or is this only an issue for pipelines?

Slide

unread,
Oct 7, 2019, 2:32:23 PM10/7/19
to jenkin...@googlegroups.com
You access environment variables differently based on the type of job. So, in the free style Notifier/Recorder/etc you would use  https://javadoc.jenkins-ci.org/hudson/model/Run.html#getEnvironment-hudson.model.TaskListener-. For pipeline, in your Step implemention, you do what Jesse mentioned:  getContext().get(EnvVars.class).

On Mon, Oct 7, 2019 at 11:09 AM Matthias <matthia...@posteo.de> wrote:
I see. But then again I still have the problem with the missing environment variables from the groovy script in the builder class or is this only an issue for pipelines?

--
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 jenkinsci-de...@googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages