目前,我的设置中包含2个项目/职位,可以将它们称为job1和job2。我的插件将在job1中运行东西,然后使用一些参数在job2中安排新的构建。我正在使用parameterized-trigger插件,以将参数传递给新版本。当前,我可以获取job2,并且可以找到包含job2所需的默认参数设置的属性。但是我无法弄清楚如何从job1修改设置值。问题是job2中的每个构建都需要有自己的参数值,因此无法对其进行硬编码,job1会生成这些值。

void perform(Run<?, ?> run, FilePath workspace, TaskListener listener) throws AbortException {
  // do stuff.

  Job job = getJobWithName("job2");

  if (job != null) {
    ParameterDefinition property = getProperty(job);

    if (property != null) {
      // TODO: how to modify the property?
    } else {
      // throw error
    }
  }

  // starting a new build in a job2.
  if (job instanceof AbstractProject) {
    // TODO: change the Cause at some point
    ((AbstractProject) job).scheduleBuild(0, new Cause.UserIdCause());
  } else if (job instanceof WorkflowJob) {
    ((WorkflowJob) job).scheduleBuild(0, new Cause.UserIdCause());
  }
}


// fetches the correct job, in this case job2
private Job getJobWithName(String jobName) {
  Jenkins jenkinsInstance = Jenkins.getInstanceOrNull();

  if (jenkinsInstance != null) {
    List<Job> jobs = jenkinsInstance.getAllItems(Job.class);

    for (Job job : jobs) {
      if (job.getDisplayName().equals(jobName)) {
        return job;
      }
    }
  }

  return null;
}

// fetches the correct property
private ParameterDefinition getProperty(Job job) {
  ParametersDefinitionProperty property = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);

  for (ParameterDefinition pd : property.getParameterDefinitions()) {
    ParameterValue param = pd.getDefaultParameterValue();
    if (param != null) {
      if (name.equals("my_parameter_name") && pd.getType().equals("StringParameterDefinition")) {
        return pd;
      }
    }
  }
  return null;
}

最佳答案

job.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("StringProp", "DefaultValue1"),
new BooleanParameterDefinition("BoolProp", false, "Desc")));

关于java - 如何更改Jenkin的Property/ParameterDefinition对象的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57609666/

10-13 03:29