How to get environment parameters in pipeline compitable jenkins SimpleBuildStep plugin?

佐手、 提交于 2019-12-12 23:11:56

问题


I just added pipeline compitability but I can't get any of env parameters. My class have a definition as below:

public class JobBuildStep extends Builder implements SimpleBuildStep

and perform method:

public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws AbortException

Could anyone please tell me what are the options to fix it? I also found an issue with it - https://issues.jenkins-ci.org/browse/JENKINS-29144 The last comment says I should implement Step instead, is it org.jenkinsci.plugins.workflow.steps.Step from

<dependency>
  <groupId>org.jenkins-ci.plugins.workflow</groupId>
  <artifactId>workflow-basic-steps</artifactId>
  <version>2.7</version>
</dependency>

package? If so how can I use @Override public StepExecution start(StepContext stepContext) throws Exception method?


回答1:


It was done by Step extending:

public class ExampleBuildStep extends Step

and creating perform method with EnvVars as input:

public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener, @Nonnull EnvVars environment) throws AbortException

and here are the implemented methods by Step class:

@Override
public StepExecution start(StepContext stepContext) {

  return new Execution(stepContext, this);
}

private final static class Execution extends SynchronousNonBlockingStepExecution<Void> {

  private transient final ExampleBuildStep step;

  protected Execution(
    @Nonnull StepContext context,
    ExampleBuildStep step) {
    super(context);
    this.step = step;
  }

  @Override
  protected Void run() throws Exception {

    FilePath workspace = getContext().get(FilePath.class);
    workspace.mkdirs();
    step.perform(
      getContext().get(Run.class),
      workspace,
      getContext().get(Launcher.class),
      getContext().get(TaskListener.class),
      getContext().get(EnvVars.class));
    return null;
  }
}

And then you will be able to use it by the name that returns by getFunctionName() in your StepDescriptor:

@Extension
public static class DescriptorImpl extends StepDescriptor {

  @Override
  public Set<? extends Class<?>> getRequiredContext() {
    return ImmutableSet.of(FilePath.class, Run.class, Launcher.class, TaskListener.class, EnvVars.class);
  }

  @Override
  public String getFunctionName() {
    return "run_your_step";
  }

  public boolean isApplicable(Class<? extends AbstractProject> aClass) {
    // Indicates that this builder can be used with all kinds of project types
    return true;
  }

  public String getDisplayName() {
    return "Example of step plugin";
  }
}


来源:https://stackoverflow.com/questions/50442303/how-to-get-environment-parameters-in-pipeline-compitable-jenkins-simplebuildstep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!