来自Jenkins插件的返回值
我正在开发一个Jenkins插件,在我的一个构建步骤中,我需要返回一个值。在这个构建步骤中,我将发送一个API调用来生成注册令牌,并希望将该令牌作为这个构建步骤的输出返回。其想法是稍后以管道/自由样式使用生成的令牌 我的问题是,我该怎么做? 这是我的构建类:来自Jenkins插件的返回值,jenkins,continuous-integration,jenkins-pipeline,jenkins-plugins,Jenkins,Continuous Integration,Jenkins Pipeline,Jenkins Plugins,我正在开发一个Jenkins插件,在我的一个构建步骤中,我需要返回一个值。在这个构建步骤中,我将发送一个API调用来生成注册令牌,并希望将该令牌作为这个构建步骤的输出返回。其想法是稍后以管道/自由样式使用生成的令牌 我的问题是,我该怎么做? 这是我的构建类: package **.********.plugins; import hudson.EnvVars; import hudson.Extension; import hudson.FilePath; import hudson.Laun
package **.********.plugins;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.tasks.BuildStep;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import hudson.util.ListBoxModel;
import **.********.constants.Constants;
import **.********.helpers.ApiHelper;
import **.********.helpers.ApiResponse;
import **.********.helpers.LogHelper;
import **.********.model.AgentDockerConfigData;
import **.********.model.AgentDockerConfigGenerationRequestData;
import **.********.model.JobData;
import **.********.model.ProjectData;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.HashMap;
public class GenerateAgentConfigToken extends Builder implements SimpleBuildStep {
//region Private members
private ApiHelper apiHelper;
private String alias;
private String projectId;
private String jobId;
private String browsers;
private AgentDockerConfigData config;
//endregion
//region Constructors
public GenerateAgentConfigToken() { }
@DataBoundConstructor
public GenerateAgentConfigToken(String alias, String projectId, String jobId, String browsers) {
this.alias = alias;
this.projectId = projectId;
this.jobId = jobId;
this.browsers = browsers;
}
//endregion
//region Setters & Getters
public String getAlias() {
return alias;
}
@DataBoundSetter
public void setAlias(String alias) {
this.alias = alias;
}
public String getProjectId() {
return projectId;
}
@DataBoundSetter
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getJobId() {
return jobId;
}
@DataBoundSetter
public void setJobId(String jobId) {
this.jobId = jobId;
}
public String getBrowsers() {
return browsers;
}
@DataBoundSetter
public void setBrowsers(String browsers) {
this.browsers = browsers;
}
//endregion
private void init() {
LogHelper.Debug("Initializing API helper...");
this.apiHelper = new ApiHelper(PluginConfiguration.DESCRIPTOR.getApiKey());
}
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException {
try {
EnvVars envVars = new EnvVars();
envVars = run.getEnvironment(taskListener);
envVars.put("jobId", jobId);
init();
LogHelper.SetLogger(taskListener.getLogger(), PluginConfiguration.DESCRIPTOR.isVerbose());
generateAgentConfigToken();
} catch (Exception e) {
LogHelper.Error(e);
run.setResult(Result.FAILURE);
}
}
private void generateAgentConfigToken() throws IOException {
LogHelper.Info("Sending a request to generate agent configuration token...");
//TODO: Change the URL to the production URL
ApiResponse<AgentDockerConfigData> response = apiHelper.Post(
Constants.TP_GENERATE_AGENT_CONFIG_TOKEN_URL,
null,
null,
generateRequestBody(),
AgentDockerConfigData.class);
if (response.isSuccessful()) {
if (response.getData() != null) {
config = response.getData();
}
} else {
int statusCode = response.getStatusCode();
String responseMessage = response.getMessage();
String message = "Unable to generate agent configuration token" + (statusCode > 0 ? " - " + statusCode : "") + (responseMessage != null ? " - " + responseMessage : "");
throw new hudson.AbortException(message);
}
}
private AgentDockerConfigGenerationRequestData generateRequestBody() {
// if the user did not provide an alias and jobId, send the body as null
if (StringUtils.isEmpty(alias) && StringUtils.isEmpty(jobId))
return null;
AgentDockerConfigGenerationRequestData body = new AgentDockerConfigGenerationRequestData();
if (!StringUtils.isEmpty(alias))
body.setAlias(alias);
if (!StringUtils.isEmpty(jobId)) {
body.setJobId(jobId);
if (!StringUtils.isEmpty(browsers))
body.setBrowsers(browsers.split(","));
}
return body;
}
@Override
public DescriptorImpl getDescriptor() { return (DescriptorImpl) super.getDescriptor(); }
@Extension
@Symbol(Constants.TP_GENERATE_AGENT_CONFIG_TOKEN_SYMBOL)
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
public DescriptorImpl() {
load();
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
req.bindJSON(this, formData);
save();
return super.configure(req, formData);
}
@Override
public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends AbstractProject> jobType) {
return true;
}
@Nonnull
@Override
public String getDisplayName() {
return Constants.TP_GENERATE_AGENT_CONFIG_TOKEN;
}
public ListBoxModel doFillProjectIdItems() {
HashMap<String, Object> headers = new HashMap<>();
headers.put(Constants.ACCEPT, Constants.APPLICATION_JSON);
ApiResponse<ProjectData[]> response = null;
try {
ApiHelper apiHelper = new ApiHelper(PluginConfiguration.DESCRIPTOR.getApiKey());
response = apiHelper.Get(Constants.TP_RETURN_ACCOUNT_PROJECTS, headers, ProjectData[].class);
if (!response.isSuccessful()) {
int statusCode = response.getStatusCode();
String responseMessage = response.getMessage();
String message = "Unable to fetch the projects list" + (statusCode > 0 ? " - " + statusCode : "") + (responseMessage != null ? " - " + responseMessage : "");
throw new hudson.AbortException(message);
}
ListBoxModel model = new ListBoxModel();
model.add("Select a project", "");
for (ProjectData project : response.getData()) {
model.add(
project.getName() + " [" + project.getId() + "]",
project.getId());
}
return model;
} catch (IOException | NullPointerException e) {
LogHelper.Error(e);
}
return null;
}
public ListBoxModel doFillJobIdItems(@QueryParameter String projectId) {
if (projectId.isEmpty()) {
return new ListBoxModel();
}
HashMap<String, Object> headers = new HashMap<>();
headers.put(Constants.ACCEPT, Constants.APPLICATION_JSON);
ApiResponse<JobData[]> response = null;
try {
ApiHelper apiHelper = new ApiHelper(PluginConfiguration.DESCRIPTOR.getApiKey());
response = apiHelper.Get(String.format(Constants.TP_RETURN_PROJECT_JOBS, projectId), headers, JobData[].class);
if (!response.isSuccessful()) {
int statusCode = response.getStatusCode();
String responseMessage = response.getMessage();
String message = "Unable to fetch the project's jobs list" + (statusCode > 0 ? " - " + statusCode : "") + (responseMessage != null ? " - " + responseMessage : "");
throw new hudson.AbortException(message);
}
ListBoxModel model = new ListBoxModel();
model.add("Select a job to execute from the selected project (You must select a project first)", "");
for (JobData job : response.getData()) {
model.add(
job.getName() + " [" + job.getId() + "]",
job.getId());
}
return model;
} catch (IOException | NullPointerException e) {
LogHelper.Error(e);
}
return null;
}
}
}
package**.********.插件;
导入hudson.EnvVars;
导入哈德逊扩展;
导入hudson.FilePath;
进口哈德逊发射器;
导入hudson.model.*;
导入hudson.tasks.BuildStep;
导入hudson.tasks.BuildStepDescriptor;
导入hudson.tasks.BuildStepMonitor;
导入hudson.tasks.Builder;
导入hudson.util.ListBoxModel;
导入********.constants.constants;
导入**.******.helpers.ApiHelper;
导入**.******.helpers.ApiResponse;
导入**.******.helpers.LogHelper;
导入**.******.model.AgentDockerConfigData;
导入**.******.model.AgentDockerConfigGenerationRequestData;
导入*********.model.JobData;
导入*********.model.ProjectData;
导入jenkins.tasks.SimpleBuildStep;
导入net.sf.json.JSONObject;
导入org.apache.commons.lang.StringUtils;
导入org.jenkinsci.Symbol;
导入org.kohsuke.stapper.DataBoundConstructor;
导入org.kohsuke.stapper.DataBoundSetter;
导入org.kohsuke.stapper.QueryParameter;
导入org.kohsuke.stapler.StaplerRequest;
导入javax.annotation.Nonnull;
导入java.io.IOException;
导入java.util.HashMap;
公共类GenerateAgentConfigToken扩展生成器实现SimpleBuildStep{
//区域私人成员
私人助理蜂鸟;
私有字符串别名;
私有字符串投影;
私有字符串jobId;
私有字符串浏览器;
私有代理DockerConfigData配置;
//端区
//区域构造函数
public GenerateAgentConfigToken(){}
@数据边界构造函数
public GenerateAgentConfigToken(字符串别名、字符串项目ID、字符串作业ID、字符串浏览器){
this.alias=别名;
this.projectId=projectId;
this.jobId=jobId;
this.browsers=浏览器;
}
//端区
//区域设定者和获取者
公共字符串getAlias(){
返回别名;
}
@数据边界设置器
public void setAlias(字符串别名){
this.alias=别名;
}
公共字符串getProjectId(){
返回投影;
}
@数据边界设置器
公共void setProjectId(字符串projectId){
this.projectId=projectId;
}
公共字符串getJobId(){
返回jobId;
}
@数据边界设置器
public void setJobId(字符串jobId){
this.jobId=jobId;
}
公共字符串getBrowsers(){
返回浏览器;
}
@数据边界设置器
公共浏览器(字符串浏览器){
this.browsers=浏览器;
}
//端区
私有void init(){
调试(“初始化API帮助程序…”);
this.apiHelper=新的apiHelper(PluginConfiguration.DESCRIPTOR.getApiKey());
}
@凌驾
public BuildStepMonitor getRequiredMonitorService(){
返回BuildStepMonitor.NONE;
}
@凌驾
public void perform(@Nonnull Run Run、@Nonnull FilePath FilePath、@Nonnull Launcher Launcher、@Nonnull tasklister tasklister)抛出interruptedeexception、IOException{
试一试{
EnvVars EnvVars=新的EnvVars();
envVars=run.getEnvironment(taskListener);
环境变量put(“jobId”,jobId);
init();
SetLogger(taskListener.getLogger(),PluginConfiguration.DESCRIPTOR.isVerbose());
generateAgentConfigToken();
}捕获(例外e){
LogHelper.Error(e);
run.setResult(Result.FAILURE);
}
}
私有void generateAgentConfigToken()引发IOException{
Info(“发送生成代理配置令牌的请求…”);
//TODO:将URL更改为生产URL
ApiResponse=apiHelper.Post(
Constants.TP_生成_代理_配置_令牌_URL,
无效的
无效的
generateRequestBody(),
AgentDockerConfigData.class);
if(response.issusccessful()){
if(response.getData()!=null){
config=response.getData();
}
}否则{
int statusCode=response.getStatusCode();
字符串responseMessage=response.getMessage();
String message=“无法生成代理配置令牌”+(状态代码>0?”-“+statusCode:”)+(responseMessage!=null?”-“+responseMessage:”);
抛出新的hudson.AbortException(消息);
}
}
私有代理DockerConfigGenerationRequestData generateRequestBody(){
//如果用户未提供别名和作业ID,请将正文作为null发送
if(StringUtils.isEmpty(别名)和&StringUtils.isEmpty(jobId))
返回null;
AgentDockerConfigGenerationRequestData正文=新建AgentDockerConfigGenerationRequestData();
如果(!StringUtils.isEmpty(别名))
body.setAlias(别名);
如果(!StringUtils.isEmpty(jobId)){
body.setJobId(jobId);
如果(!StringUtils.isEmpty(浏览器))
body.setBrowser(browsers.split(“,”);
}
返回体;
}
@凌驾
公共描述符rimpl getDescriptor(){return(descriptor rimpl)super.getDescriptor();}
@延伸
@符号(Constants.TP\u GENERATE\u AGENT\u CONFIG\u TOKEN\u Symbol)
公共静态类描述符RIMPL扩展了BuildStepDescriptor{
公共描述符rimpl(){
加载();
}
@凌驾
公共布尔配置(StaplerRequest req、JSONObject formData)引发FormException{
req.bindJSON(this,formData);
save();
返回super.configure(req,formData);
}
@凌驾
公共布尔值不适用(@SuppressWarni