Search in sources :

Example 1 with SCMFileSystem

use of jenkins.scm.api.SCMFileSystem in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinition method create.

@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;
    if (isLightweight()) {
        try (SCMFileSystem fs = SCMFileSystem.of(build.getParent(), scm)) {
            if (fs != null) {
                String script = fs.child(scriptPath).contentAsString();
                listener.getLogger().println("Obtained " + scriptPath + " from " + scm.getKey());
                Queue.Executable exec = owner.getExecutable();
                FlowDurabilityHint hint = (exec instanceof Item) ? DurabilityHintProvider.suggestedFor((Item) exec) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint();
                return new CpsFlowExecution(script, true, owner, hint);
            } else {
                listener.getLogger().println("Lightweight checkout support not available, falling back to full checkout.");
            }
        }
    }
    FilePath dir;
    Node node = Jenkins.getActiveInstance();
    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);
    } else {
        // should not happen, but just in case:
        dir = new FilePath(owner.getRootDir());
    }
    listener.getLogger().println("Checking out " + scm.getKey() + " into " + dir + " to read " + scriptPath);
    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)) {
        for (int retryCount = Jenkins.getInstance().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 (IOException e) {
                // checkout error not yet reported
                // TODO 2.43+ use Functions.printStackTrace
                listener.error("Checkout failed").println(Functions.printThrowable(e).trim());
            }
            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(scriptPath);
        if (!scriptFile.absolutize().getRemote().replace('\\', '/').startsWith(dir.absolutize().getRemote().replace('\\', '/') + '/')) {
            // TODO JENKINS-26838
            throw new IOException(scriptFile + " 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;
}
Also used : FilePath(hudson.FilePath) InterruptedIOException(java.io.InterruptedIOException) Action(hudson.model.Action) Node(hudson.model.Node) TopLevelItem(hudson.model.TopLevelItem) Run(hudson.model.Run) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) WorkspaceList(hudson.slaves.WorkspaceList) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) GenericSCMStep(org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) TopLevelItem(hudson.model.TopLevelItem) Item(hudson.model.Item) WorkspaceActionImpl(org.jenkinsci.plugins.workflow.support.actions.WorkspaceActionImpl) SCMFileSystem(jenkins.scm.api.SCMFileSystem) GenericSCMStep(org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep) SCMStep(org.jenkinsci.plugins.workflow.steps.scm.SCMStep) Computer(hudson.model.Computer) Queue(hudson.model.Queue) AbortException(hudson.AbortException)

Example 2 with SCMFileSystem

use of jenkins.scm.api.SCMFileSystem in project gitea-plugin by jenkinsci.

the class GiteaSCMSource method createProbe.

@NonNull
@Override
protected SCMProbe createProbe(@NonNull final SCMHead head, SCMRevision revision) throws IOException {
    try {
        GiteaSCMFileSystem.BuilderImpl builder = ExtensionList.lookup(SCMFileSystem.Builder.class).get(GiteaSCMFileSystem.BuilderImpl.class);
        if (builder == null) {
            throw new AssertionError();
        }
        final SCMFileSystem fs = builder.build(this, head, revision);
        return new SCMProbe() {

            @NonNull
            @Override
            public SCMProbeStat stat(@NonNull String path) throws IOException {
                try {
                    return SCMProbeStat.fromType(fs.child(path).getType());
                } catch (InterruptedException e) {
                    throw new IOException("Interrupted", e);
                }
            }

            @Override
            public void close() throws IOException {
                fs.close();
            }

            @Override
            public String name() {
                return head.getName();
            }

            @Override
            public long lastModified() {
                try {
                    return fs.lastModified();
                } catch (IOException e) {
                    return 0L;
                } catch (InterruptedException e) {
                    return 0L;
                }
            }

            @Override
            public SCMFile getRoot() {
                return fs.getRoot();
            }
        };
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : SCMProbe(jenkins.scm.api.SCMProbe) UriTemplateBuilder(com.damnhandy.uri.template.UriTemplateBuilder) URIRequirementBuilder(com.cloudbees.plugins.credentials.domains.URIRequirementBuilder) SCMFileSystem(jenkins.scm.api.SCMFileSystem) NonNull(edu.umd.cs.findbugs.annotations.NonNull) IOException(java.io.IOException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

IOException (java.io.IOException)2 SCMFileSystem (jenkins.scm.api.SCMFileSystem)2 URIRequirementBuilder (com.cloudbees.plugins.credentials.domains.URIRequirementBuilder)1 UriTemplateBuilder (com.damnhandy.uri.template.UriTemplateBuilder)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 AbortException (hudson.AbortException)1 FilePath (hudson.FilePath)1 Action (hudson.model.Action)1 Computer (hudson.model.Computer)1 Item (hudson.model.Item)1 Node (hudson.model.Node)1 Queue (hudson.model.Queue)1 Run (hudson.model.Run)1 TopLevelItem (hudson.model.TopLevelItem)1 WorkspaceList (hudson.slaves.WorkspaceList)1 InterruptedIOException (java.io.InterruptedIOException)1 SCMProbe (jenkins.scm.api.SCMProbe)1 FlowDurabilityHint (org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint)1 GenericSCMStep (org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep)1 SCMStep (org.jenkinsci.plugins.workflow.steps.scm.SCMStep)1