gpt4 book ai didi

java - 如何获取管道兼容的jenkins SimpleBuildStep插件中的环境参数?

转载 作者:行者123 更新时间:2023-11-30 02:09:43 24 4
gpt4 key购买 nike

我刚刚添加了管道兼容性,但无法获取任何 env 参数。我的类(class)有一个定义如下:

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/browse/JENKINS-29144最后一条评论说我应该实现 Step相反,是 org.jenkinsci.plugins.workflow.steps.Step来自

<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方法?

最佳答案

这是通过步骤扩展完成的:

public class ExampleBuildStep extends Step

并使用 EnvVars 作为输入创建执行方法:

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

这里是Step类实现的方法:

@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() 返回的名称来使用它:

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

关于java - 如何获取管道兼容的jenkins SimpleBuildStep插件中的环境参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50442303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com