Search in sources :

Example 6 with Launcher

use of hudson.Launcher in project hudson-2.x by hudson.

the class JDK method isDefaultJDKValid.

/**
 * Checks if "java" is in PATH on the given node.
 *
 * <p>
 * If it's not, then the user must specify a configured JDK,
 * so this is often useful for form field validation.
 */
public static boolean isDefaultJDKValid(Node n) {
    try {
        TaskListener listener = new StreamTaskListener(new NullStream());
        Launcher launcher = n.createLauncher(listener);
        return launcher.launch().cmds("java", "-fullversion").stdout(listener).join() == 0;
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
}
Also used : StreamTaskListener(hudson.util.StreamTaskListener) NullStream(hudson.util.NullStream) StreamTaskListener(hudson.util.StreamTaskListener) Launcher(hudson.Launcher) IOException(java.io.IOException)

Example 7 with Launcher

use of hudson.Launcher in project hudson-2.x by hudson.

the class AbstractProject method poll.

/**
 * Checks if there's any update in SCM, and returns true if any is found.
 *
 * <p>
 * The implementation is responsible for ensuring mutual exclusion between polling and builds
 * if necessary.
 *
 * @since 1.345
 */
public PollingResult poll(TaskListener listener) {
    SCM scm = getScm();
    if (scm == null) {
        listener.getLogger().println(Messages.AbstractProject_NoSCM());
        return NO_CHANGES;
    }
    if (isDisabled()) {
        listener.getLogger().println(Messages.AbstractProject_Disabled());
        return NO_CHANGES;
    }
    R lb = getLastBuild();
    if (lb == null) {
        listener.getLogger().println(Messages.AbstractProject_NoBuilds());
        return isInQueue() ? NO_CHANGES : BUILD_NOW;
    }
    if (pollingBaseline == null) {
        // if we have a persisted baseline, we'll find it by this
        R success = getLastSuccessfulBuild();
        for (R r = lb; r != null; r = r.getPreviousBuild()) {
            SCMRevisionState s = r.getAction(SCMRevisionState.class);
            if (s != null) {
                pollingBaseline = s;
                break;
            }
            // searched far enough
            if (r == success)
                break;
        }
    // NOTE-NO-BASELINE:
    // if we don't have baseline yet, it means the data is built by old Hudson that doesn't set the baseline
    // as action, so we need to compute it. This happens later.
    }
    try {
        if (scm.requiresWorkspaceForPolling()) {
            // lock the workspace of the last build
            FilePath ws = lb.getWorkspace();
            if (ws == null || !ws.exists()) {
                // workspace offline. build now, or nothing will ever be built
                Label label = getAssignedLabel();
                if (label != null && label.isSelfLabel()) {
                    // if the build is fixed on a node, then attempting a build will do us
                    // no good. We should just wait for the slave to come back.
                    listener.getLogger().println(Messages.AbstractProject_NoWorkspace());
                    return NO_CHANGES;
                }
                listener.getLogger().println(ws == null ? Messages.AbstractProject_WorkspaceOffline() : Messages.AbstractProject_NoWorkspace());
                if (isInQueue()) {
                    listener.getLogger().println(Messages.AbstractProject_AwaitingBuildForWorkspace());
                    return NO_CHANGES;
                } else {
                    listener.getLogger().println(Messages.AbstractProject_NewBuildForWorkspace());
                    return BUILD_NOW;
                }
            } else {
                Node node = lb.getBuiltOn();
                if (node == null || node.toComputer() == null) {
                    LOGGER.log(Level.FINE, "Node on which this job previously was built is not available now, build is started on an available node");
                    return isInQueue() ? NO_CHANGES : BUILD_NOW;
                }
                WorkspaceList l = node.toComputer().getWorkspaceList();
                // if doing non-concurrent build, acquire a workspace in a way that causes builds to block for this workspace.
                // this prevents multiple workspaces of the same job --- the behavior of Hudson < 1.319.
                // 
                // OTOH, if a concurrent build is chosen, the user is willing to create a multiple workspace,
                // so better throughput is achieved over time (modulo the initial cost of creating that many workspaces)
                // by having multiple workspaces
                WorkspaceList.Lease lease = l.acquire(ws, !concurrentBuild);
                Launcher launcher = ws.createLauncher(listener);
                try {
                    LOGGER.fine("Polling SCM changes of " + getName());
                    if (// see NOTE-NO-BASELINE above
                    pollingBaseline == null)
                        calcPollingBaseline(lb, launcher, listener);
                    PollingResult r = scm.poll(this, launcher, ws, listener, pollingBaseline);
                    pollingBaseline = r.remote;
                    return r;
                } finally {
                    lease.release();
                }
            }
        } else {
            // polling without workspace
            LOGGER.fine("Polling SCM changes of " + getName());
            if (// see NOTE-NO-BASELINE above
            pollingBaseline == null)
                calcPollingBaseline(lb, null, listener);
            PollingResult r = scm.poll(this, null, null, listener, pollingBaseline);
            pollingBaseline = r.remote;
            return r;
        }
    } catch (AbortException e) {
        listener.getLogger().println(e.getMessage());
        listener.fatalError(Messages.AbstractProject_Aborted());
        LOGGER.log(Level.FINE, "Polling " + this + " aborted", e);
        return NO_CHANGES;
    } catch (IOException e) {
        e.printStackTrace(listener.fatalError(e.getMessage()));
        return NO_CHANGES;
    } catch (InterruptedException e) {
        e.printStackTrace(listener.fatalError(Messages.AbstractProject_PollingABorted()));
        return NO_CHANGES;
    }
}
Also used : FilePath(hudson.FilePath) WorkspaceList(hudson.slaves.WorkspaceList) IOException(java.io.IOException) SC_INTERNAL_SERVER_ERROR(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR) SCMRevisionState(hudson.scm.SCMRevisionState) PollingResult(hudson.scm.PollingResult) Launcher(hudson.Launcher) SCM(hudson.scm.SCM) NullSCM(hudson.scm.NullSCM) AbortException(hudson.AbortException)

Example 8 with Launcher

use of hudson.Launcher in project blueocean-plugin by jenkinsci.

the class PipelineApiTest method testArtifactsRunApi.

@Test
public void testArtifactsRunApi() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("pipeline1");
    p.getBuildersList().add(new TestBuilder() {

        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            FilePath ws = build.getWorkspace();
            if (ws == null) {
                return false;
            }
            FilePath dir = ws.child("dir");
            dir.mkdirs();
            dir.child("fizz").write("contents", null);
            dir.child("lodge").symlinkTo("fizz", listener);
            return true;
        }
    });
    ArtifactArchiver aa = new ArtifactArchiver("dir/fizz");
    aa.setAllowEmptyArchive(true);
    p.getPublishersList().add(aa);
    FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
    List artifacts = get("/organizations/jenkins/pipelines/pipeline1/runs/" + b.getId() + "/artifacts", List.class);
    assertEquals(1, artifacts.size());
    assertEquals("fizz", ((Map) artifacts.get(0)).get("name"));
    String artifactId = (String) ((Map) artifacts.get(0)).get("id");
    ArtifactContainerImpl container = new ArtifactContainerImpl(b, () -> new Link("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/1/artifacts/"));
    BlueArtifact blueArtifact = container.get(artifactId);
    assertNotNull(blueArtifact);
}
Also used : FilePath(hudson.FilePath) ArtifactArchiver(hudson.tasks.ArtifactArchiver) IOException(java.io.IOException) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) TestBuilder(org.jvnet.hudson.test.TestBuilder) BuildListener(hudson.model.BuildListener) BlueArtifact(io.jenkins.blueocean.rest.model.BlueArtifact) Launcher(hudson.Launcher) List(java.util.List) ArtifactContainerImpl(io.jenkins.blueocean.service.embedded.rest.ArtifactContainerImpl) Link(io.jenkins.blueocean.rest.hal.Link) Test(org.junit.Test)

Example 9 with Launcher

use of hudson.Launcher in project htmlpublisher-plugin by jenkinsci.

the class HtmlPublisherIntegrationTest method testIncludes.

@Test
public void testIncludes() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("include_job");
    final String reportDir = "autogen";
    p.getBuildersList().add(new TestBuilder() {

        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            FilePath ws = build.getWorkspace().child(reportDir);
            ws.child("tab1.html").write("hello", "UTF-8");
            ws.child("tab2.html").write("hello", "UTF-8");
            ws.child("dummy.html").write("hello", "UTF-8");
            return true;
        }
    });
    HtmlPublisherTarget target1 = new HtmlPublisherTarget("tab1", reportDir, "tab1.html", true, true, false);
    // default behavior is include all
    target1.setIncludes(HtmlPublisherTarget.INCLUDE_ALL_PATTERN);
    HtmlPublisherTarget target2 = new HtmlPublisherTarget("tab2", reportDir, "tab2.html", true, true, false);
    String includes = "tab2.html";
    target2.setIncludes(includes);
    assertEquals(includes, target2.getIncludes());
    HtmlPublisherTarget[] l = { target1, target2 };
    p.getPublishersList().add(new HtmlPublisher(Arrays.asList(l)));
    AbstractBuild build = j.buildAndAssertSuccess(p);
    File base = new File(build.getRootDir(), "htmlreports");
    List<String> tab1Files = Arrays.asList(new File(base, "tab1").list());
    List<String> tab2Files = Arrays.asList(new File(base, "tab2").list());
    // tab1 file copied all files
    assertTrue(tab1Files.contains("dummy.html"));
    assertTrue(tab1Files.contains("tab1.html"));
    // tab2 should not include dummy
    assertTrue(tab2Files.contains("tab2.html"));
    assertFalse(tab2Files.contains("dummy.html"));
}
Also used : FilePath(hudson.FilePath) IOException(java.io.IOException) TestBuilder(org.jvnet.hudson.test.TestBuilder) Launcher(hudson.Launcher) File(java.io.File) Test(org.junit.Test)

Example 10 with Launcher

use of hudson.Launcher in project htmlpublisher-plugin by jenkinsci.

the class HtmlPublisherIntegrationTest method testNotAllowMissingDoesntRunSubsequentReports.

@Test
public void testNotAllowMissingDoesntRunSubsequentReports() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("variable_job");
    p.getBuildersList().add(new TestBuilder() {

        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            FilePath ws = build.getWorkspace();
            ws.child("dirA").child("afile.html").write("hello", "UTF-8");
            return true;
        }
    });
    HtmlPublisherTarget target1 = new HtmlPublisherTarget("reportnameB", "dirB", "", true, true, false);
    HtmlPublisherTarget target2 = new HtmlPublisherTarget("reportnameA", "dirA", "", true, true, false);
    List<HtmlPublisherTarget> targets = new ArrayList<>();
    targets.add(target1);
    targets.add(target2);
    p.getPublishersList().add(new HtmlPublisher(targets));
    QueueTaskFuture<FreeStyleBuild> task = p.scheduleBuild2(0);
    AbstractBuild build = j.assertBuildStatus(Result.FAILURE, task);
    assertFalse(new File(build.getRootDir(), "htmlreports/reportnameA/htmlpublisher-wrapper.html").exists());
    assertFalse(new File(build.getRootDir(), "htmlreports/reportnameB/htmlpublisher-wrapper.html").exists());
}
Also used : FilePath(hudson.FilePath) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TestBuilder(org.jvnet.hudson.test.TestBuilder) Launcher(hudson.Launcher) File(java.io.File) Test(org.junit.Test)

Aggregations

Launcher (hudson.Launcher)20 IOException (java.io.IOException)17 FilePath (hudson.FilePath)14 Test (org.junit.Test)11 TestBuilder (org.jvnet.hudson.test.TestBuilder)11 File (java.io.File)8 FreeStyleProject (hudson.model.FreeStyleProject)7 ArrayList (java.util.ArrayList)6 BuildListener (hudson.model.BuildListener)5 EnvVars (hudson.EnvVars)3 FreeStyleBuild (hudson.model.FreeStyleBuild)3 TaskListener (hudson.model.TaskListener)3 ArtifactArchiver (hudson.tasks.ArtifactArchiver)3 AbortException (hudson.AbortException)2 PollingResult (hudson.scm.PollingResult)2 SCMRevisionState (hudson.scm.SCMRevisionState)2 WorkspaceList (hudson.slaves.WorkspaceList)2 Verifier (jenkins.plugins.nodejs.CIBuilderHelper.Verifier)2 DetectionFailedException (jenkins.plugins.nodejs.tools.DetectionFailedException)2 NodeJSInstallation (jenkins.plugins.nodejs.tools.NodeJSInstallation)2