use of org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep in project workflow-cps-plugin by jenkinsci.
the class CpsScmFlowDefinition method create.
@SuppressFBWarnings(value = { "NP_LOAD_OF_KNOWN_NULL_VALUE", "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" }, justification = "false positives for try-resource in java 11")
@Override
public CpsFlowExecution create(FlowExecutionOwner owner, TaskListener listener, List<? extends Action> actions) throws Exception {
for (Action a : actions) {
if (a instanceof CpsFlowFactoryAction2) {
return ((CpsFlowFactoryAction2) a).create(this, owner, actions);
}
}
Queue.Executable _build = owner.getExecutable();
if (!(_build instanceof Run)) {
throw new IOException("can only check out SCM into a Run");
}
Run<?, ?> build = (Run<?, ?>) _build;
String expandedScriptPath = build.getEnvironment(listener).expand(scriptPath);
if (isLightweight()) {
try (SCMFileSystem fs = SCMFileSystem.of(build.getParent(), scm)) {
if (fs != null) {
try {
String script = fs.child(expandedScriptPath).contentAsString();
listener.getLogger().println("Obtained " + expandedScriptPath + " from " + scm.getKey());
Queue.Executable exec = owner.getExecutable();
FlowDurabilityHint hint = (exec instanceof Run) ? DurabilityHintProvider.suggestedFor(((Run) exec).getParent()) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint();
return new CpsFlowExecution(script, true, owner, hint);
} catch (FileNotFoundException e) {
throw new AbortException("Unable to find " + expandedScriptPath + " from " + scm.getKey());
}
} else {
listener.getLogger().println("Lightweight checkout support not available, falling back to full checkout.");
}
}
}
FilePath dir;
Node node = Jenkins.get();
if (build.getParent() instanceof TopLevelItem) {
FilePath baseWorkspace = node.getWorkspaceFor((TopLevelItem) build.getParent());
if (baseWorkspace == null) {
throw new IOException(node.getDisplayName() + " may be offline");
}
dir = getFilePathWithSuffix(baseWorkspace, scm);
} else {
// should not happen, but just in case:
dir = new FilePath(owner.getRootDir());
}
listener.getLogger().println("Checking out " + scm.getKey() + " into " + dir + " to read " + expandedScriptPath);
String script = null;
Computer computer = node.toComputer();
if (computer == null) {
throw new IOException(node.getDisplayName() + " may be offline");
}
SCMStep delegate = new GenericSCMStep(scm);
delegate.setPoll(true);
delegate.setChangelog(true);
FilePath acquiredDir;
try (WorkspaceList.Lease lease = computer.getWorkspaceList().acquire(dir)) {
dir.withSuffix("-scm-key.txt").write(scm.getKey(), "UTF-8");
for (int retryCount = Jenkins.get().getScmCheckoutRetryCount(); retryCount >= 0; retryCount--) {
try {
delegate.checkout(build, dir, listener, node.createLauncher(listener));
break;
} catch (AbortException e) {
// If so, just skip echoing it.
if (e.getMessage() != null) {
listener.error(e.getMessage());
}
} catch (InterruptedIOException e) {
throw e;
} catch (Exception e) {
// checkout error not yet reported
Functions.printStackTrace(e, listener.error("Checkout failed"));
}
if (// all attempts failed
retryCount == 0)
throw new AbortException("Maximum checkout retry attempts reached, aborting");
listener.getLogger().println("Retrying after 10 seconds");
Thread.sleep(10000);
}
FilePath scriptFile = dir.child(expandedScriptPath);
if (!new File(scriptFile.getRemote()).getCanonicalFile().toPath().startsWith(new File(dir.getRemote()).getCanonicalPath())) {
// TODO JENKINS-26838
throw new IOException(scriptFile + " references a file that is not inside " + dir);
}
if (!scriptFile.exists()) {
throw new AbortException(scriptFile + " not found");
}
script = scriptFile.readToString();
acquiredDir = lease.path;
}
Queue.Executable queueExec = owner.getExecutable();
FlowDurabilityHint hint = (queueExec instanceof Run) ? DurabilityHintProvider.suggestedFor(((Run) queueExec).getParent()) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint();
CpsFlowExecution exec = new CpsFlowExecution(script, true, owner, hint);
exec.flowStartNodeActions.add(new WorkspaceActionImpl(acquiredDir, null));
return exec;
}
Aggregations