我有一个如下所示的全局配置类,其中包含所有全局字段。
@Extension
public class CustomGlobalConfiguration extends GlobalConfiguration {
/** @return the singleton instance */
public static CustomGlobalConfiguration get() {
return GlobalConfiguration.all().get(CustomGlobalConfiguration.class);
}
private String pcip;
private String userName;
private String password;
public CustomGlobalConfiguration() {
// When Jenkins is restarted, load any saved configuration from disk.
load();
}
public String getPcip() {
return pcip;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
@DataBoundSetter
public void setPcip(String pcip) {
this.pcip = pcip;
save();
}
@DataBoundSetter
public void setUserName(String userName) {
this.userName = userName;
save();
}
@DataBoundSetter
public void setPassword(String password) {
this.password = password;
save();
}
下面是我的两个构建器类,它们试图获取全局值集。但是在第二节课中,这似乎不起作用。
A级
public class IntegrationLeader extends Builder{
private final String blueprintName, appName;
private static final String USER_AGENT = "Mozilla/5.0";
private static final String POST_PARAMS = "{\"length\":250}";
@DataBoundConstructor
public IntegrationLeader(String blueprintName, String appName) {
this.blueprintName = blueprintName;
this.appName = appName;
}
// We'll use this from the <tt>config.jelly</tt>.
public String getBlueprintName(){
return blueprintName;
}
public String getAppName() {
return appName;
}
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
EnvVars envVars = new EnvVars();
final EnvVars env = build.getEnvironment(listener);
PrintStream log = listener.getLogger();
//Expanding appname to include the env variables in it's name
String expandedapp = env.expand(appName);
String ip = getDescriptor().getCalmIP()
//rest of the other stuff
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
private String calmIP;
private String calmUser;
private String calmPwd;
public List<String> offers;
public List<String> profiles;
public List<String> projects;
public List<String> actions;
public DescriptorImpl() {
load();
}
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project types
return true;
}
/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "Nutanix Calm Blueprint Launch";
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
CustomGlobalConfiguration calmGlobalConfiguration = CustomGlobalConfiguration.get();
calmIP = calmGlobalConfiguration.getPcip();
calmUser = calmGlobalConfiguration.getUserName();
calmPwd = calmGlobalConfiguration.getPassword();
save();
return super.configure(req, formData);
}
}
B级
public class RunActionOnApp extends Builder{
private final String blueprintName, appName;
private static final String USER_AGENT = "Mozilla/5.0";
private static final String POST_PARAMS = "{\"length\":250}";
// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public IntegrationLeader(String blueprintName, String appName) {
this.blueprintName = blueprintName;
this.appName = appName;
}
// We'll use this from the <tt>config.jelly</tt>.
public String getBlueprintName(){
return blueprintName;
}
public String getAppName() {
return appName;
}
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
EnvVars envVars = new EnvVars();
final EnvVars env = build.getEnvironment(listener);
PrintStream log = listener.getLogger();
//Expanding appname to include the env variables in it's name
String expandedapp = env.expand(appName);
String ip = getDescriptor().getCalmIP()
//rest of the other stuff
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
@Extension // This indicates to Jenkins that this is an implementation of an extension point.
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
/**
* To persist global configuration information, simply store it in a
* field and call save().
*
* <p>
* If you don't want fields to be persisted, use <tt>transient</tt>.
*/
private String calmIP;
private String calmUser;
private String calmPwd;
public List<String> offers;
public List<String> profiles;
public List<String> projects;
public List<String> actions;
private int lastEditorId = 0;
private int lastprofId = 0;
public DescriptorImpl() {
load();
}
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project types
return true;
}
/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "Nutanix Calm Blueprint Launch";
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
// To persist global configuration information,
// set that to properties and call save().
CustomGlobalConfiguration calmGlobalConfiguration = CustomGlobalConfiguration.get();
calmIP = calmGlobalConfiguration.getPcip();
calmUser = calmGlobalConfiguration.getUserName();
calmPwd = calmGlobalConfiguration.getPassword();
// ^Can also use req.bindJSON(this, formData);
// (easier when there are many fields; need set* methods for this, like setUseFrench)
save();
return super.configure(req, formData);
}
}
正在使用 configure 功能访问全局配置值。该hpi成功创建并上传。如果我使用 IntegrationLeader 添加了构建步骤,则可以完美读取全局配置(因为在api调用之后填充了其他字段,为了进行api调用,我们需要由GlobalConfiguration读取的凭据)。但是,当我使用 RunActionOnApp 添加构建步骤时,由于ip,用户名和密码字段被设置为null,因此api调用开始失败。