Skip to content

Commit

Permalink
Merge pull request #3 from leelavathidh192/PLUGINS-3786
Browse files Browse the repository at this point in the history
HCL Accelerate Multiple Instance Support in jenkins
  • Loading branch information
deepaannjohn authored Jan 23, 2023
2 parents a29412a + f88ff23 commit 4b4965b
Show file tree
Hide file tree
Showing 51 changed files with 1,879 additions and 1,136 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.hcl.devops</groupId>
<artifactId>hcl-accelerate</artifactId>
<version>2.1.5</version>
<version>3.0.0</version>
<packaging>hpi</packaging>
<name>HCL Accelerate Plugin</name>
<description>This plugin can run Jenkins jobs as a part of a deployment plan in HCL Accelerate.</description>
Expand Down
130 changes: 79 additions & 51 deletions src/main/java/com/hcl/devops/connect/CRPipeline/CheckGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.hcl.devops.connect.CloudPublisher;
import com.hcl.devops.connect.DevOpsGlobalConfiguration;
import com.hcl.devops.connect.Endpoints.EndpointManager;
import com.hcl.devops.connect.Entry;
import java.util.List;
import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

Expand All @@ -45,80 +48,105 @@ public CheckGate(
String pipelineId,
String stageName,
String versionId,
String fatal
) {
String fatal) {
this.pipelineId = pipelineId;
this.stageName = stageName;
this.versionId = versionId;
this.fatal = fatal;
}

public String getPipelineId() { return this.pipelineId; }
public String getStageName() { return this.stageName; }
public String getVersionId() { return this.versionId; }
public String getFatal() { return this.fatal; }
public String getPipelineId() {
return this.pipelineId;
}
public String getStageName() {
return this.stageName;
}
public String getVersionId() {
return this.versionId;
}
public String getFatal() {
return this.fatal;
}

private static String getPipelinesUrl() {
private static String getPipelinesUrl(Entry entry) {
EndpointManager em = new EndpointManager();
return em.getPipelinesEndpoint();
return em.getPipelinesEndpoint(entry);
}

@Override
public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher, final TaskListener listener)
throws AbortException, InterruptedException, IOException, RuntimeException {
if (!Jenkins.getInstance().getDescriptorByType(DevOpsGlobalConfiguration.class).isConfigured()) {
listener.getLogger().println("Could not check Accelerate gates as there is no configuration specified.");
return;
}

throws AbortException, InterruptedException, IOException, RuntimeException {
EnvVars envVars = build.getEnvironment(listener);

String pipelineId = envVars.expand(this.pipelineId);
String stageName = envVars.expand(this.stageName);
String versionId = envVars.expand(this.versionId);
String fatal = envVars.expand(this.fatal);

listener.getLogger().println("Check gates on pipeline: " + CheckGate.getPipelinesUrl() + pipelineId);
listener.getLogger().println("Checking gate on stage \"" + stageName + "\" for version \"" + versionId + "\" in HCL Accelerate...");
Boolean throwException = false;
try {
String result = CloudPublisher.checkGate(pipelineId, stageName, versionId);
JSONObject resultObj = JSONObject.fromObject(result);
if (resultObj.has("errors")) {
throw new RuntimeException(resultObj.get("errors").toString());
}
Iterator<?> keys = resultObj.keys();
Boolean anyGateFailed = false;
while(keys.hasNext()) {
String key = keys.next().toString();
String value = resultObj.get(key).toString();
if (value.equals("true")) {
listener.getLogger().println("Gate \"" + key + "\" passed");
} else if (value.equals("false")) {
listener.getLogger().println("Gate \"" + key + "\" failed");
anyGateFailed = true;
List<Entry> entries = Jenkins.getInstance().getDescriptorByType(DevOpsGlobalConfiguration.class).getEntries();
Entry finalEntry = null;
for (Entry entry : entries) {
try {
if (CloudPublisher.isPipeline(entry, pipelineId)) {
finalEntry = entry;
break;
}
}
if (anyGateFailed) {
if (fatal != null && fatal.equals("true")) {
throwException = true;
} catch (Exception e) {
listener.getLogger().println("Entry: " + entry);
listener.error("Error checking validity of pipelineId: " + e.getClass() + " - " + e.getMessage());
throw new RuntimeException("Not able to validate pipelineId");
}
}
if (finalEntry != null) {
listener.getLogger()
.println("Check gates on pipeline: " + CheckGate.getPipelinesUrl(finalEntry) + pipelineId);
listener.getLogger().println("Checking gate on stage \"" + stageName + "\" for version \"" + versionId
+ "\" in HCL Accelerate (" + finalEntry.getBaseUrl() + ").");
Boolean throwException = false;
String logString = finalEntry.getBaseUrl();
try {
String result = CloudPublisher.checkGate(finalEntry, pipelineId, stageName, versionId);
JSONObject resultObj = JSONObject.fromObject(result);
if (resultObj.has("errors")) {
throw new RuntimeException(resultObj.get("errors").toString());
}
Iterator<?> keys = resultObj.keys();
Boolean anyGateFailed = false;
while (keys.hasNext()) {
String key = keys.next().toString();
String value = resultObj.get(key).toString();
if (value.equals("true")) {
listener.getLogger().println("Gate \"" + key + "\" passed (" + logString + ").");
} else if (value.equals("false")) {
listener.getLogger().println("Gate \"" + key + "\" failed (" + logString + ").");
anyGateFailed = true;
}
}
if (anyGateFailed) {
if (fatal != null && fatal.equals("true")) {
throwException = true;
}
build.setResult(Result.FAILURE);
} else {
listener.getLogger().println("No gate failures, gates pass (" + logString + ").");
}
} catch (Exception ex) {
listener.error("Error checking gate (" + logString + "): " + ex.getClass() + " - " + ex.getMessage());
listener.error("Stack trace (" + logString + "): ");
StackTraceElement[] elements = ex.getStackTrace();
for (int i = 0; i < elements.length; i++) {
StackTraceElement s = elements[i];
listener.error("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":"
+ s.getLineNumber() + ")");
}
build.setResult(Result.FAILURE);
} else {
listener.getLogger().println("No gate failures, gates pass.");
}
} catch (Exception ex) {
listener.error("Error checking gate: " + ex.getClass() + " - " + ex.getMessage());
listener.error("Stack trace:");
StackTraceElement[] elements = ex.getStackTrace();
for (int i = 0; i < elements.length; i++) {
StackTraceElement s = elements[i];
listener.error("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
}
build.setResult(Result.FAILURE);
}
if (throwException) {
throw new RuntimeException("Gate failure and fatal set to \"true\", exception thrown to stop build.");
if (throwException) {
throw new RuntimeException(
"Gate failure and fatal set to \"true\", exception thrown to stop build (" + logString + ").");
}
} else {
throw new RuntimeException("Invalid pipelineId");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher
throws AbortException, InterruptedException, IOException {
CrAction action = build.getAction(CrAction.class);

if(action == null) {
if (action == null) {
action = new CrAction();
build.addAction(action);
}
Expand Down
Loading

0 comments on commit 4b4965b

Please sign in to comment.