如何在可配管的jenkins SimpleBuildStep插件中获取环境参数? [英] How to get environment parameters in pipeline compitable jenkins SimpleBuildStep plugin?

查看:231
本文介绍了如何在可配管的jenkins SimpleBuildStep插件中获取环境参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚添加了管道可组合性,但是没有任何env参数. 我的班级的定义如下:

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

并执行方法:

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

任何人都可以告诉我解决该问题的选项吗? 我还发现了一个问题- https://issues.jenkins-ci.org/浏览/JENKINS-29144 最后一条评论说我应该改为使用Step,它是来自

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>

包裹?如果可以,我该如何使用 @Override public StepExecution start(StepContext stepContext) throws Exception 方法?

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

推荐答案

它是通过逐步扩展来完成的:

It was done by Step extending:

public class ExampleBuildStep extends Step

并使用EnvVars作为输入来创建Perform方法:

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

这是Step类实现的方法:

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;
  }
}

然后您就可以通过StepDescriptor中的getFunctionName()返回的名称来使用它:

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";
  }
}

这篇关于如何在可配管的jenkins SimpleBuildStep插件中获取环境参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆