AbstractBuild.java 45.6 KB
Newer Older
K
kohsuke 已提交
1 2
/*
 * The MIT License
3
 *
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc., CloudBees, Inc.
5
 *
K
kohsuke 已提交
6 7 8 9 10 11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
12
 *
K
kohsuke 已提交
13 14
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
15
 *
K
kohsuke 已提交
16 17 18 19 20 21 22 23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.model;

26
import hudson.AbortException;
27
import hudson.EnvVars;
28
import hudson.Functions;
29 30
import hudson.Launcher;
import hudson.Util;
K
kohsuke 已提交
31
import hudson.FilePath;
32 33
import hudson.console.AnnotatedLargeText;
import hudson.console.ExpandableDetailsNote;
34
import hudson.model.listeners.RunListener;
K
kohsuke 已提交
35
import hudson.slaves.WorkspaceList;
36
import hudson.slaves.NodeProperty;
37
import hudson.slaves.WorkspaceList.Lease;
38
import hudson.matrix.MatrixConfiguration;
39 40
import hudson.model.Fingerprint.BuildPtr;
import hudson.model.Fingerprint.RangeSet;
K
kohsuke 已提交
41
import hudson.model.listeners.SCMListener;
42 43 44 45
import hudson.scm.ChangeLogParser;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
import hudson.scm.SCM;
46
import hudson.scm.NullChangeLogParser;
47
import hudson.tasks.BuildStep;
48
import hudson.tasks.BuildWrapper;
49 50
import hudson.tasks.Builder;
import hudson.tasks.Fingerprinter.FingerprintAction;
51
import hudson.tasks.Publisher;
K
kohsuke 已提交
52
import hudson.tasks.BuildStepMonitor;
53
import hudson.tasks.BuildTrigger;
54
import hudson.tasks.test.AbstractTestResultAction;
55
import hudson.tasks.test.AggregatedTestResultAction;
K
kohsuke 已提交
56
import hudson.util.AdaptedIterator;
K
kohsuke 已提交
57
import hudson.util.Iterators;
58
import hudson.util.LogTaskListener;
59
import hudson.util.VariableResolver;
60
import jenkins.model.Jenkins;
61
import org.kohsuke.stapler.Stapler;
62 63
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
64
import org.kohsuke.stapler.export.Exported;
65
import org.xml.sax.SAXException;
66

67
import javax.servlet.ServletException;
68 69
import java.io.File;
import java.io.IOException;
70
import java.io.StringWriter;
71
import java.text.MessageFormat;
72 73 74 75 76 77 78 79 80 81 82
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
83 84
import java.util.logging.Level;
import java.util.logging.Logger;
K
kohsuke 已提交
85

86 87 88
/**
 * Base implementation of {@link Run}s that build software.
 *
89
 * For now this is primarily the common part of {@link Build} and MavenBuild.
90 91 92 93
 *
 * @author Kohsuke Kawaguchi
 * @see AbstractProject
 */
94
public abstract class AbstractBuild<P extends AbstractProject<P,R>,R extends AbstractBuild<P,R>> extends Run<P,R> implements Queue.Executable {
95

96 97 98 99 100
    /**
     * Set if we want the blame information to flow from upstream to downstream build.
     */
    private static final boolean upstreamCulprits = Boolean.getBoolean("hudson.upstreamCulprits");

101
    /**
K
kohsuke 已提交
102
     * Name of the slave this project was built on.
K
kohsuke 已提交
103
     * Null or "" if built by the master. (null happens when we read old record that didn't have this information.)
104 105 106
     */
    private String builtOn;

K
kohsuke 已提交
107 108
    /**
     * The file path on the node that performed a build. Kept as a string since {@link FilePath} is not serializable into XML.
K
kohsuke 已提交
109
     * @since 1.319
K
kohsuke 已提交
110 111 112
     */
    private String workspace;

113 114 115 116 117
    /**
     * Version of Hudson that built this.
     */
    private String hudsonVersion;

118 119 120 121 122 123 124 125 126 127 128
    /**
     * SCM used for this build.
     * Maybe null, for historical reason, in which case CVS is assumed.
     */
    private ChangeLogParser scm;

    /**
     * Changes in this build.
     */
    private volatile transient ChangeLogSet<? extends Entry> changeSet;

129 130 131 132 133 134 135 136 137 138 139 140 141 142
    /**
     * Cumulative list of people who contributed to the build problem.
     *
     * <p>
     * This is a list of {@link User#getId() user ids} who made a change
     * since the last non-broken build. Can be null (which should be
     * treated like empty set), because of the compatibility.
     *
     * <p>
     * This field is semi-final --- once set the value will never be modified.
     *
     * @since 1.137
     */
    private volatile Set<String> culprits;
143

K
kohsuke 已提交
144 145 146 147
    /**
     * During the build this field remembers {@link BuildWrapper.Environment}s created by
     * {@link BuildWrapper}. This design is bit ugly but forced due to compatibility.
     */
148
    protected transient List<Environment> buildEnvironments;
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    protected AbstractBuild(P job) throws IOException {
        super(job);
    }

    protected AbstractBuild(P job, Calendar timestamp) {
        super(job, timestamp);
    }

    protected AbstractBuild(P project, File buildDir) throws IOException {
        super(project, buildDir);
    }

    public final P getProject() {
        return getParent();
    }

    /**
     * Returns a {@link Slave} on which this build was done.
K
kohsuke 已提交
168 169
     *
     * @return
M
mindless 已提交
170
     *      null, for example if the slave that this build run no longer exists.
171 172
     */
    public Node getBuiltOn() {
173
        if (builtOn==null || builtOn.equals(""))
174
            return Jenkins.getInstance();
175
        else
176
            return Jenkins.getInstance().getNode(builtOn);
177 178 179
    }

    /**
M
mindless 已提交
180 181
     * Returns the name of the slave it was built on; null or "" if built by the master.
     * (null happens when we read old record that didn't have this information.)
182
     */
K
kohsuke 已提交
183
    @Exported(name="builtOn")
184 185 186 187
    public String getBuiltOnStr() {
        return builtOn;
    }

188
    /**
189 190 191 192
     * Allows subtypes to set the value of {@link #builtOn}.
     * This is used for those implementations where an {@link AbstractBuild} is made 'built' without
     * actually running its {@link #run()} method.
     *
193 194
     * @since 1.429
     */
195
    protected void setBuiltOnStr( String builtOn ) {
196 197 198
        this.builtOn = builtOn;
    }

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    /**
     * Gets the nearest ancestor {@link AbstractBuild} that belongs to
     * {@linkplain AbstractProject#getRootProject() the root project of getProject()} that
     * dominates/governs/encompasses this build.
     *
     * <p>
     * Some projects (such as matrix projects, Maven projects, or promotion processes) form a tree of jobs,
     * and still in some of them, builds of child projects are related/tied to that of the parent project.
     * In such a case, this method returns the governing build.
     *
     * @return never null. In the worst case the build dominates itself.
     * @since 1.421
     * @see AbstractProject#getRootProject()
     */
    public AbstractBuild<?,?> getRootBuild() {
        return this;
    }

K
kohsuke 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    /**
     * Used to render the side panel "Back to project" link.
     *
     * <p>
     * In a rare situation where a build can be reached from multiple paths,
     * returning different URLs from this method based on situations might
     * be desirable.
     *
     * <p>
     * If you override this method, you'll most likely also want to override
     * {@link #getDisplayName()}.
     */
    public String getUpUrl() {
        return Functions.getNearestAncestorUrl(Stapler.getCurrentRequest(),getParent())+'/';
    }

K
kohsuke 已提交
233 234 235 236 237 238 239 240 241 242 243
    /**
     * Gets the directory where this build is being built.
     *
     * <p>
     * Note to implementors: to control where the workspace is created, override
     * {@link AbstractRunner#decideWorkspace(Node,WorkspaceList)}.
     *
     * @return
     *      null if the workspace is on a slave that's not connected. Note that once the build is completed,
     *      the workspace may be used to build something else, so the value returned from this method may
     *      no longer show a workspace as it was used for this build.
K
kohsuke 已提交
244
     * @since 1.319
K
kohsuke 已提交
245 246
     */
    public final FilePath getWorkspace() {
247
        if (workspace==null) return null;
K
kohsuke 已提交
248
        Node n = getBuiltOn();
249
        if (n==null) return null;
K
kohsuke 已提交
250 251 252
        return n.createPath(workspace);
    }

253 254 255 256 257 258 259 260
    /**
     * Normally, a workspace is assigned by {@link Runner}, but this lets you set the workspace in case
     * {@link AbstractBuild} is created without a build.
     */
    protected void setWorkspace(FilePath ws) {
        this.workspace = ws.getRemote();
    }

K
kohsuke 已提交
261 262 263 264 265 266 267 268
    /**
     * Returns the root directory of the checked-out module.
     * <p>
     * This is usually where <tt>pom.xml</tt>, <tt>build.xml</tt>
     * and so on exists.
     */
    public final FilePath getModuleRoot() {
        FilePath ws = getWorkspace();
269
        if (ws==null)    return null;
270
        return getParent().getScm().getModuleRoot(ws,this);
K
kohsuke 已提交
271 272 273 274 275 276 277 278 279 280 281
    }

    /**
     * Returns the root directories of all checked-out modules.
     * <p>
     * Some SCMs support checking out multiple modules into the same workspace.
     * In these cases, the returned array will have a length greater than one.
     * @return The roots of all modules checked out from the SCM.
     */
    public FilePath[] getModuleRoots() {
        FilePath ws = getWorkspace();
282
        if (ws==null)    return null;
283
        return getParent().getScm().getModuleRoots(ws,this);
K
kohsuke 已提交
284 285
    }

286 287 288 289
    /**
     * List of users who committed a change since the last non-broken build till now.
     *
     * <p>
K
kohsuke 已提交
290 291
     * This list at least always include people who made changes in this build, but
     * if the previous build was a failure it also includes the culprit list from there.
292 293 294 295 296 297
     *
     * @return
     *      can be empty but never null.
     */
    @Exported
    public Set<User> getCulprits() {
298
        if (culprits==null) {
299
            Set<User> r = new HashSet<User>();
300
            R p = getPreviousCompletedBuild();
301
            if (p !=null && isBuilding()) {
K
kohsuke 已提交
302
                Result pr = p.getResult();
K
Kohsuke Kawaguchi 已提交
303
                if (pr!=null && pr.isWorseThan(Result.SUCCESS)) {
K
kohsuke 已提交
304 305 306 307 308 309
                    // we are still building, so this is just the current latest information,
                    // but we seems to be failing so far, so inherit culprits from the previous build.
                    // isBuilding() check is to avoid recursion when loading data from old Hudson, which doesn't record
                    // this information
                    r.addAll(p.getCulprits());
                }
310
            }
311
            for (Entry e : getChangeSet())
312
                r.add(e.getAuthor());
313 314 315 316

            if (upstreamCulprits) {
                // If we have dependencies since the last successful build, add their authors to our list
                if (getPreviousNotFailedBuild() != null) {
M
marco 已提交
317
                    Map <AbstractProject,AbstractBuild.DependencyChange> depmap = getDependencyChanges(getPreviousSuccessfulBuild());
318 319 320 321 322 323 324 325 326 327
                    for (AbstractBuild.DependencyChange dep : depmap.values()) {
                        for (AbstractBuild<?,?> b : dep.getBuilds()) {
                            for (Entry entry : b.getChangeSet()) {
                                r.add(entry.getAuthor());
                            }
                        }
                    }
                }
            }

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            return r;
        }

        return new AbstractSet<User>() {
            public Iterator<User> iterator() {
                return new AdaptedIterator<String,User>(culprits.iterator()) {
                    protected User adapt(String id) {
                        return User.get(id);
                    }
                };
            }

            public int size() {
                return culprits.size();
            }
        };
    }

346 347 348 349 350 351 352
    /**
     * Returns true if this user has made a commit to this build.
     *
     * @since 1.191
     */
    public boolean hasParticipant(User user) {
        for (ChangeLogSet.Entry e : getChangeSet())
353
            if (e.getAuthor()==user)
354 355 356 357
                return true;
        return false;
    }

K
kohsuke 已提交
358 359 360 361 362 363 364 365 366
    /**
     * Gets the version of Hudson that was used to build this job.
     *
     * @since 1.246
     */
    public String getHudsonVersion() {
        return hudsonVersion;
    }

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    @Override
    public synchronized void delete() throws IOException {
        // Need to check if deleting this build affects lastSuccessful/lastStable symlinks
        R lastSuccessful = getProject().getLastSuccessfulBuild(),
          lastStable = getProject().getLastStableBuild();

        super.delete();

        try {
            if (lastSuccessful == this)
                updateSymlink("lastSuccessful", getProject().getLastSuccessfulBuild());
            if (lastStable == this)
                updateSymlink("lastStable", getProject().getLastStableBuild());
        } catch (InterruptedException ex) {
            LOGGER.warning("Interrupted update of lastSuccessful/lastStable symlinks for "
                           + getProject().getDisplayName());
            // handle it later
            Thread.currentThread().interrupt();
        }
    }

    private void updateSymlink(String name, AbstractBuild<?,?> newTarget) throws InterruptedException {
        if (newTarget != null)
            newTarget.createSymlink(new LogTaskListener(LOGGER, Level.WARNING), name);
        else
            new File(getProject().getBuildDir(), "../"+name).delete();
    }

    private void createSymlink(TaskListener listener, String name) throws InterruptedException {
        Util.createSymlink(getProject().getBuildDir(),"builds/"+getId(),"../"+name,listener);
    }

K
kohsuke 已提交
399
    protected abstract class AbstractRunner extends Runner {
400 401
        /**
         * Since configuration can be changed while a build is in progress,
402
         * create a launcher once and stick to it for the entire build duration.
403 404 405
         */
        protected Launcher launcher;

406 407 408 409 410
        /**
         * Output/progress of this build goes here.
         */
        protected BuildListener listener;

K
kohsuke 已提交
411 412 413 414 415 416 417
        /**
         * Returns the current {@link Node} on which we are buildling.
         */
        protected final Node getCurrentNode() {
            return Executor.currentExecutor().getOwner().getNode();
        }

K
kohsuke 已提交
418 419 420 421 422 423 424 425
        /**
         * Allocates the workspace from {@link WorkspaceList}.
         *
         * @param n
         *      Passed in for the convenience. The node where the build is running.
         * @param wsl
         *      Passed in for the convenience. The returned path must be registered to this object.
         */
426
        protected Lease decideWorkspace(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
427 428 429 430 431
            String customWorkspace = getProject().getCustomWorkspace();
            if (customWorkspace != null) {
                // we allow custom workspaces to be concurrently used between jobs.
                return Lease.createDummyLease(n.getRootPath().child(getEnvironment(listener).expand(customWorkspace)));
            }
K
kohsuke 已提交
432 433 434 435
            // TODO: this cast is indicative of abstraction problem
            return wsl.allocate(n.getWorkspaceFor((TopLevelItem)getProject()));
        }

436
        public Result run(BuildListener listener) throws Exception {
437
            Node node = getCurrentNode();
438 439
            assert builtOn==null;
            builtOn = node.getNodeName();
440
            hudsonVersion = Jenkins.VERSION;
441
            this.listener = listener;
442

443
            launcher = createLauncher(listener);
444 445
            if (!Jenkins.getInstance().getNodes().isEmpty())
                listener.getLogger().println(node instanceof Jenkins ? Messages.AbstractBuild_BuildingOnMaster() : Messages.AbstractBuild_BuildingRemotely(builtOn));
446

447
            final Lease lease = decideWorkspace(node,Computer.currentComputer().getWorkspaceList());
K
kohsuke 已提交
448 449

            try {
450 451
                workspace = lease.path.getRemote();
                node.getFileSystemProvisioner().prepareWorkspace(AbstractBuild.this,lease.path,listener);
452

V
Vojtech Juranek 已提交
453
                preCheckout(launcher,listener);
K
kohsuke 已提交
454
                checkout(listener);
455

456
                if (!preBuild(listener,project.getProperties()))
K
kohsuke 已提交
457
                    return Result.FAILURE;
458

K
kohsuke 已提交
459
                Result result = doRun(listener);
460

461 462
                Computer c = node.toComputer();
                if (c==null || c.isOffline()) {
463 464
                    // As can be seen in HUDSON-5073, when a build fails because of the slave connectivity problem,
                    // error message doesn't point users to the slave. So let's do it here.
465
                    listener.hyperlink("/computer/"+builtOn+"/log","Looks like the node went offline during the build. Check the slave log for the details.");
466

D
Dave Brosius 已提交
467 468 469 470 471 472 473 474 475 476
                    if (c != null) {
                        // grab the end of the log file. This might not work very well if the slave already
                        // starts reconnecting. Fixing this requires a ring buffer in slave logs.
                        AnnotatedLargeText<Computer> log = c.getLogText();
                        StringWriter w = new StringWriter();
                        log.writeHtmlTo(Math.max(0,c.getLogFile().length()-10240),w);

                        listener.getLogger().print(ExpandableDetailsNote.encodeTo("details",w.toString()));
                        listener.getLogger().println();
                    }
477 478
                }

K
kohsuke 已提交
479 480 481 482
                // kill run-away processes that are left
                // use multiple environment variables so that people can escape this massacre by overriding an environment
                // variable for some processes
                launcher.kill(getCharacteristicEnvVars());
483

K
kohsuke 已提交
484 485
                // this is ugly, but for historical reason, if non-null value is returned
                // it should become the final result.
486 487
                if (result==null)    result = getResult();
                if (result==null)    result = Result.SUCCESS;
488

K
kohsuke 已提交
489 490
                return result;
            } finally {
491
                lease.release();
492
                this.listener = null;
K
kohsuke 已提交
493
            }
494 495
        }

496 497 498 499 500
        /**
         * Creates a {@link Launcher} that this build will use. This can be overridden by derived types
         * to decorate the resulting {@link Launcher}.
         *
         * @param listener
501
         *      Always non-null. Connected to the main build output.
502 503
         */
        protected Launcher createLauncher(BuildListener listener) throws IOException, InterruptedException {
504 505 506 507
            Launcher l = getCurrentNode().createLauncher(listener);

            if (project instanceof BuildableItemWithBuildWrappers) {
                BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
508
                for (BuildWrapper bw : biwbw.getBuildWrappersList())
509 510 511 512 513
                    l = bw.decorateLauncher(AbstractBuild.this,l,listener);
            }

            buildEnvironments = new ArrayList<Environment>();

514 515 516 517 518 519 520
            for (RunListener rl: RunListener.all()) {
                Environment environment = rl.setUpEnvironment(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

521
            for (NodeProperty nodeProperty: Jenkins.getInstance().getGlobalNodeProperties()) {
522 523 524 525 526 527 528 529 530 531 532 533 534 535
                Environment environment = nodeProperty.setUp(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

            for (NodeProperty nodeProperty: Computer.currentComputer().getNode().getNodeProperties()) {
                Environment environment = nodeProperty.setUp(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

            return l;
536 537
        }

V
Vojtech Juranek 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
        
        /**
         * Run preCheckout on {@link BuildWrapper}s
         * 
         * @param launcher
         * 		The launcher, never null.
         * @param listener
         * 		Never null, connected to the main build output.
         * @throws IOException
         * @throws InterruptedException
         */
        private void preCheckout(Launcher launcher, BuildListener listener) throws IOException, InterruptedException{
        	if (project instanceof BuildableItemWithBuildWrappers) {
                BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
                for (BuildWrapper bw : biwbw.getBuildWrappersList())
                    bw.preCheckout(AbstractBuild.this,launcher,listener);
            }
        }
        
K
kohsuke 已提交
557
        private void checkout(BuildListener listener) throws Exception {
558 559 560 561 562 563 564
            try {
                for (int retryCount=project.getScmCheckoutRetryCount(); ; retryCount--) {
                    // for historical reasons, null in the scm field means CVS, so we need to explicitly set this to something
                    // in case check out fails and leaves a broken changelog.xml behind.
                    // see http://www.nabble.com/CVSChangeLogSet.parse-yields-SAXParseExceptions-when-parsing-bad-*AccuRev*-changelog.xml-files-td22213663.html
                    AbstractBuild.this.scm = new NullChangeLogParser();

565 566 567 568
                    try {
                        if (project.checkout(AbstractBuild.this,launcher,listener,new File(getRootDir(),"changelog.xml"))) {
                            // check out succeeded
                            SCM scm = project.getScm();
569

570 571
                            AbstractBuild.this.scm = scm.createChangeLogParser();
                            AbstractBuild.this.changeSet = AbstractBuild.this.calcChangeSet();
572

573
                            for (SCMListener l : Jenkins.getInstance().getSCMListeners())
574 575 576 577
                                l.onChangeLogParsed(AbstractBuild.this,listener,changeSet);
                            return;
                        }
                    } catch (AbortException e) {
578
                        listener.error(e.getMessage());
579 580
                    } catch (IOException e) {
                        // checkout error not yet reported
581
                        e.printStackTrace(listener.getLogger());
582
                    }
583

584
                    if (retryCount == 0)   // all attempts failed
585
                        throw new RunnerAbortedException();
586

587 588 589 590 591
                    listener.getLogger().println("Retrying after 10 seconds");
                    Thread.sleep(10000);
                }
            } catch (InterruptedException e) {
                listener.getLogger().println(Messages.AbstractProject_ScmAborted());
592
                LOGGER.log(Level.INFO, AbstractBuild.this + " aborted", e);
593
                throw new RunnerAbortedException();
K
kohsuke 已提交
594
            }
595 596
        }

K
kohsuke 已提交
597 598 599 600 601 602 603 604 605
        /**
         * The portion of a build that is specific to a subclass of {@link AbstractBuild}
         * goes here.
         *
         * @return
         *      null to continue the build normally (that means the doRun method
         *      itself run successfully)
         *      Return a non-null value to abort the build right there with the specified result code.
         */
K
kohsuke 已提交
606
        protected abstract Result doRun(BuildListener listener) throws Exception, RunnerAbortedException;
607 608 609 610 611 612 613 614 615

        /**
         * @see #post(BuildListener)
         */
        protected abstract void post2(BuildListener listener) throws Exception;

        public final void post(BuildListener listener) throws Exception {
            try {
                post2(listener);
616 617 618 619 620 621

                if (result.isBetterOrEqualTo(Result.UNSTABLE))
                    createSymlink(listener, "lastSuccessful");

                if (result.isBetterOrEqualTo(Result.SUCCESS))
                    createSymlink(listener, "lastStable");
622 623 624 625 626 627
            } finally {
                // update the culprit list
                HashSet<String> r = new HashSet<String>();
                for (User u : getCulprits())
                    r.add(u.getId());
                culprits = r;
K
kohsuke 已提交
628
                CheckPoint.CULPRITS_DETERMINED.report();
629 630
            }
        }
631 632

        public void cleanUp(BuildListener listener) throws Exception {
633 634
            BuildTrigger.execute(AbstractBuild.this, listener);
            buildEnvironments = null;
635
        }
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
        /**
         * @deprecated as of 1.356
         *      Use {@link #performAllBuildSteps(BuildListener, Map, boolean)}
         */
        protected final void performAllBuildStep(BuildListener listener, Map<?,? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
            performAllBuildSteps(listener,buildSteps.values(),phase);
        }

        protected final boolean performAllBuildSteps(BuildListener listener, Map<?,? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
            return performAllBuildSteps(listener,buildSteps.values(),phase);
        }

        /**
         * @deprecated as of 1.356
         *      Use {@link #performAllBuildSteps(BuildListener, Iterable, boolean)}
         */
        protected final void performAllBuildStep(BuildListener listener, Iterable<? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
            performAllBuildSteps(listener,buildSteps,phase);
655 656 657 658 659 660 661 662
        }

        /**
         * Runs all the given build steps, even if one of them fail.
         *
         * @param phase
         *      true for the post build processing, and false for the final "run after finished" execution.
         */
663
        protected final boolean performAllBuildSteps(BuildListener listener, Iterable<? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
664
            boolean r = true;
665 666
            for (BuildStep bs : buildSteps) {
                if ((bs instanceof Publisher && ((Publisher)bs).needsToRunAfterFinalized()) ^ phase)
667
                    try {
668 669 670 671
                        if (!perform(bs,listener)) {
                            LOGGER.fine(MessageFormat.format("{0} : {1} failed", AbstractBuild.this.toString(), bs));
                            r = false;
                        }
672
                    } catch (Exception e) {
673 674 675
                        String msg = "Publisher " + bs.getClass().getName() + " aborted due to exception";
                        e.printStackTrace(listener.error(msg));
                        LOGGER.log(Level.WARNING, msg, e);
676
                        setResult(Result.FAILURE);
677
                    }
K
kohsuke 已提交
678
            }
679
            return r;
K
kohsuke 已提交
680 681 682 683 684 685 686 687 688 689 690
        }

        /**
         * Calls a build step.
         */
        protected final boolean perform(BuildStep bs, BuildListener listener) throws InterruptedException, IOException {
            BuildStepMonitor mon;
            try {
                mon = bs.getRequiredMonitorService();
            } catch (AbstractMethodError e) {
                mon = BuildStepMonitor.BUILD;
691
            }
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
            Result oldResult = AbstractBuild.this.getResult();
            boolean canContinue = mon.perform(bs, AbstractBuild.this, launcher, listener);
            Result newResult = AbstractBuild.this.getResult();
            if (newResult != oldResult) {
                String buildStepName = getBuildStepName(bs);
                listener.getLogger().format("Build step '%s' changed build result to %s%n", buildStepName, newResult);
            }
            if (!canContinue) {
                String buildStepName = getBuildStepName(bs);
                listener.getLogger().format("Build step '%s' marked build as failure%n", buildStepName);
            }
            return canContinue;
        }

        private String getBuildStepName(BuildStep bs) {
            if (bs instanceof Describable<?>) {
                return ((Describable<?>) bs).getDescriptor().getDisplayName();
            } else {
                return bs.getClass().getSimpleName();
            }
712
        }
713 714

        protected final boolean preBuild(BuildListener listener,Map<?,? extends BuildStep> steps) {
715 716 717 718
            return preBuild(listener,steps.values());
        }

        protected final boolean preBuild(BuildListener listener,Collection<? extends BuildStep> steps) {
719 720 721 722
            return preBuild(listener,(Iterable<? extends BuildStep>)steps);
        }

        protected final boolean preBuild(BuildListener listener,Iterable<? extends BuildStep> steps) {
723
            for (BuildStep bs : steps)
724 725
                if (!bs.prebuild(AbstractBuild.this,listener)) {
                    LOGGER.fine(MessageFormat.format("{0} : {1} failed", AbstractBuild.this.toString(), bs));
726
                    return false;
727
                }
728 729
            return true;
        }
730
    }
K
kohsuke 已提交
731

732 733 734 735 736 737 738 739 740 741 742 743 744 745
    /**
     * get the fingerprints associated with this build
     *
     * @return never null
     */
    @Exported(name = "fingerprint", inline = true, visibility = -1)
    public Collection<Fingerprint> getBuildFingerprints() {
        FingerprintAction fingerprintAction = getAction(FingerprintAction.class);
        if (fingerprintAction != null) {
            return fingerprintAction.getFingerprints().values();
        }
        return Collections.<Fingerprint>emptyList();
    }

746 747 748 749 750
    /**
     * Gets the changes incorporated into this build.
     *
     * @return never null.
     */
K
kohsuke 已提交
751
    @Exported
752
    public ChangeLogSet<? extends Entry> getChangeSet() {
753 754 755
        if (scm==null) {
            // for historical reason, null means CVS.
            try {
756
                Class<?> c = Jenkins.getInstance().getPluginManager().uberClassLoader.loadClass("hudson.scm.CVSChangeLogParser");
757 758 759 760 761 762 763 764 765 766 767 768
                scm = (ChangeLogParser)c.newInstance();
            } catch (ClassNotFoundException e) {
                // if CVS isn't available, fall back to something non-null.
                scm = new NullChangeLogParser();
            } catch (InstantiationException e) {
                scm = new NullChangeLogParser();
                throw (Error)new InstantiationError().initCause(e);
            } catch (IllegalAccessException e) {
                scm = new NullChangeLogParser();
                throw (Error)new IllegalAccessError().initCause(e);
            }
        }
769

770
        if (changeSet==null) // cached value
771 772 773 774 775 776
            try {
                changeSet = calcChangeSet();
            } finally {
                // defensive check. if the calculation fails (such as through an exception),
                // set a dummy value so that it'll work the next time. the exception will
                // be still reported, giving the plugin developer an opportunity to fix it.
777
                if (changeSet==null)
778 779
                    changeSet=ChangeLogSet.createEmpty(this);
            }
780 781 782 783 784 785 786 787 788 789 790 791 792
        return changeSet;
    }

    /**
     * Returns true if the changelog is already computed.
     */
    public boolean hasChangeSetComputed() {
        File changelogFile = new File(getRootDir(), "changelog.xml");
        return changelogFile.exists();
    }

    private ChangeLogSet<? extends Entry> calcChangeSet() {
        File changelogFile = new File(getRootDir(), "changelog.xml");
793
        if (!changelogFile.exists())
794
            return ChangeLogSet.createEmpty(this);
795 796 797 798 799 800 801 802

        try {
            return scm.parse(this,changelogFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
803
        return ChangeLogSet.createEmpty(this);
804 805 806
    }

    @Override
K
kohsuke 已提交
807 808
    public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
        EnvVars env = super.getEnvironment(log);
809 810 811
        FilePath ws = getWorkspace();
        if (ws!=null)   // if this is done very early on in the build, workspace may not be decided yet. see HUDSON-3997
            env.put("WORKSPACE", ws.getRemote());
K
kohsuke 已提交
812 813 814 815
        // servlet container may have set CLASSPATH in its launch script,
        // so don't let that inherit to the new child process.
        // see http://www.nabble.com/Run-Job-with-JDK-1.4.2-tf4468601.html
        env.put("CLASSPATH","");
816 817

        JDK jdk = project.getJDK();
818
        if (jdk != null) {
819 820
            Computer computer = Computer.currentComputer();
            if (computer != null) { // just in case were not in a build
K
kohsuke 已提交
821
                jdk = jdk.forNode(computer.getNode(), log);
822
            }
823
            jdk.buildEnvVars(env);
824
        }
825
        project.getScm().buildEnvVars(this,env);
826

827 828 829
        if (buildEnvironments!=null)
            for (Environment e : buildEnvironments)
                e.buildEnvVars(env);
830

831 832 833
        for (EnvironmentContributingAction a : Util.filter(getActions(),EnvironmentContributingAction.class))
            a.buildEnvVars(this,env);

834
        EnvVars.resolve(env);
835

836 837
        return env;
    }
K
kohsuke 已提交
838

839
    public Calendar due() {
840
        return getTimestamp();
841 842
    }

843 844
    /**
     * Builds up a set of variable names that contain sensitive values that
A
alanharder 已提交
845
     * should not be exposed. The expectation is that this set is populated with
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
     * keys returned by {@link #getBuildVariables()} that should have their
     * values masked for display purposes.
     *
     * @since 1.378
     */
    public Set<String> getSensitiveBuildVariables() {
        Set<String> s = new HashSet<String>();

        ParametersAction parameters = getAction(ParametersAction.class);
        if (parameters != null) {
            for (ParameterValue p : parameters) {
                if (p.isSensitive()) {
                    s.add(p.getName());
                }
            }
        }

        // Allow BuildWrappers to determine if any of their data is sensitive
        if (project instanceof BuildableItemWithBuildWrappers) {
            for (BuildWrapper bw : ((BuildableItemWithBuildWrappers) project).getBuildWrappersList()) {
                bw.makeSensitiveBuildVariables(this, s);
            }
        }
        
        return s;
    }

873 874 875 876 877 878 879 880 881
    /**
     * Provides additional variables and their values to {@link Builder}s.
     *
     * <p>
     * This mechanism is used by {@link MatrixConfiguration} to pass
     * the configuration values to the current build. It is up to
     * {@link Builder}s to decide whether it wants to recognize the values
     * or how to use them.
     *
882 883 884 885 886
     * <p>
     * This also includes build parameters if a build is parameterized.
     *
     * @return
     *      The returned map is mutable so that subtypes can put more values.
887 888
     */
    public Map<String,String> getBuildVariables() {
889 890 891
        Map<String,String> r = new HashMap<String, String>();

        ParametersAction parameters = getAction(ParametersAction.class);
892
        if (parameters!=null) {
893 894 895
            // this is a rather round about way of doing this...
            for (ParameterValue p : parameters) {
                String v = p.createVariableResolver(this).resolve(p.getName());
896
                if (v!=null) r.put(p.getName(),v);
897 898
            }
        }
899 900 901 902 903 904 905

        // allow the BuildWrappers to contribute additional build variables
        if (project instanceof BuildableItemWithBuildWrappers) {
            for (BuildWrapper bw : ((BuildableItemWithBuildWrappers) project).getBuildWrappersList())
                bw.makeBuildVariables(this,r);
        }

906 907 908
        for (BuildVariableContributor bvc : BuildVariableContributor.all())
            bvc.buildVariablesFor(this,r);

909
        return r;
910
    }
911 912 913 914 915 916 917 918

    /**
     * Creates {@link VariableResolver} backed by {@link #getBuildVariables()}.
     */
    public final VariableResolver<String> getBuildVariableResolver() {
        return new VariableResolver.ByMap<String>(getBuildVariables());
    }

919 920 921
    /**
     * Gets {@link AbstractTestResultAction} associated with this build if any.
     */
922 923 924
    public AbstractTestResultAction getTestResultAction() {
        return getAction(AbstractTestResultAction.class);
    }
925

926 927 928 929 930 931 932
    /**
     * Gets {@link AggregatedTestResultAction} associated with this build if any.
     */
    public AggregatedTestResultAction getAggregatedTestResultAction() {
        return getAction(AggregatedTestResultAction.class);
    }

K
kohsuke 已提交
933 934 935 936 937
    /**
     * Invoked by {@link Executor} to performs a build.
     */
    public abstract void run();

938 939 940 941 942 943 944 945 946 947
//
//
// fingerprint related stuff
//
//

    @Override
    public String getWhyKeepLog() {
        // if any of the downstream project is configured with 'keep dependency component',
        // we need to keep this log
K
kohsuke 已提交
948
        OUTER:
K
kohsuke 已提交
949
        for (AbstractProject<?,?> p : getParent().getDownstreamProjects()) {
950
            if (!p.isKeepDependencies()) continue;
K
kohsuke 已提交
951

K
kohsuke 已提交
952
            AbstractBuild<?,?> fb = p.getFirstBuild();
953
            if (fb==null)        continue; // no active record
K
kohsuke 已提交
954 955

            // is there any active build that depends on us?
956
            for (int i : getDownstreamRelationship(p).listNumbersReverse()) {
K
kohsuke 已提交
957 958 959
                // TODO: this is essentially a "find intersection between two sparse sequences"
                // and we should be able to do much better.

960
                if (i<fb.getNumber())
K
kohsuke 已提交
961 962
                    continue OUTER; // all the other records are younger than the first record, so pointless to search.

K
kohsuke 已提交
963
                AbstractBuild<?,?> b = p.getBuildByNumber(i);
964
                if (b!=null)
K
kohsuke 已提交
965
                    return Messages.AbstractBuild_KeptBecause(b);
966 967 968 969 970 971 972 973 974 975 976 977
            }
        }

        return super.getWhyKeepLog();
    }

    /**
     * Gets the dependency relationship from this build (as the source)
     * and that project (as the sink.)
     *
     * @return
     *      range of build numbers that represent which downstream builds are using this build.
K
kohsuke 已提交
978
     *      The range will be empty if no build of that project matches this, but it'll never be null.
979
     */
980 981 982 983
    public RangeSet getDownstreamRelationship(AbstractProject that) {
        RangeSet rs = new RangeSet();

        FingerprintAction f = getAction(FingerprintAction.class);
984
        if (f==null)     return rs;
985 986 987

        // look for fingerprints that point to this build as the source, and merge them all
        for (Fingerprint e : f.getFingerprints().values()) {
988 989 990 991

            if (upstreamCulprits) {
                // With upstreamCulprits, we allow downstream relationships
                // from intermediate jobs
992
                rs.add(e.getRangeSet(that));
993 994
            } else {
                BuildPtr o = e.getOriginal();
995
                if (o!=null && o.is(this))
996 997
                    rs.add(e.getRangeSet(that));
            }
998 999 1000 1001
        }

        return rs;
    }
1002

K
kohsuke 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    /**
     * Works like {@link #getDownstreamRelationship(AbstractProject)} but returns
     * the actual build objects, in ascending order.
     * @since 1.150
     */
    public Iterable<AbstractBuild<?,?>> getDownstreamBuilds(final AbstractProject<?,?> that) {
        final Iterable<Integer> nums = getDownstreamRelationship(that).listNumbers();

        return new Iterable<AbstractBuild<?, ?>>() {
            public Iterator<AbstractBuild<?, ?>> iterator() {
1013 1014 1015 1016 1017 1018
                return Iterators.removeNull(
                    new AdaptedIterator<Integer,AbstractBuild<?,?>>(nums) {
                        protected AbstractBuild<?, ?> adapt(Integer item) {
                            return that.getBuildByNumber(item);
                        }
                    });
K
kohsuke 已提交
1019 1020 1021 1022
            }
        };
    }

1023 1024 1025 1026 1027 1028 1029 1030
    /**
     * Gets the dependency relationship from this build (as the sink)
     * and that project (as the source.)
     *
     * @return
     *      Build number of the upstream build that feed into this build,
     *      or -1 if no record is available.
     */
1031 1032
    public int getUpstreamRelationship(AbstractProject that) {
        FingerprintAction f = getAction(FingerprintAction.class);
1033
        if (f==null)     return -1;
1034 1035 1036 1037 1038

        int n = -1;

        // look for fingerprints that point to the given project as the source, and merge them all
        for (Fingerprint e : f.getFingerprints().values()) {
1039 1040 1041 1042
            if (upstreamCulprits) {
                // With upstreamCulprits, we allow upstream relationships
                // from intermediate jobs
                Fingerprint.RangeSet rangeset = e.getRangeSet(that);
1043
                if (!rangeset.isEmpty()) {
1044 1045 1046 1047
                    n = Math.max(n, rangeset.listNumbersReverse().iterator().next());
                }
            } else {
                BuildPtr o = e.getOriginal();
1048
                if (o!=null && o.belongsTo(that))
1049 1050
                    n = Math.max(n,o.getNumber());
            }
1051 1052 1053 1054
        }

        return n;
    }
1055

K
kohsuke 已提交
1056 1057 1058 1059 1060 1061 1062 1063
    /**
     * Works like {@link #getUpstreamRelationship(AbstractProject)} but returns the
     * actual build object.
     *
     * @return
     *      null if no such upstream build was found, or it was found but the
     *      build record is already lost.
     */
1064
    public AbstractBuild<?,?> getUpstreamRelationshipBuild(AbstractProject<?,?> that) {
K
kohsuke 已提交
1065
        int n = getUpstreamRelationship(that);
1066
        if (n==-1)   return null;
K
kohsuke 已提交
1067 1068 1069
        return that.getBuildByNumber(n);
    }

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
    /**
     * Gets the downstream builds of this build, which are the builds of the
     * downstream projects that use artifacts of this build.
     *
     * @return
     *      For each project with fingerprinting enabled, returns the range
     *      of builds (which can be empty if no build uses the artifact from this build.)
     */
    public Map<AbstractProject,RangeSet> getDownstreamBuilds() {
        Map<AbstractProject,RangeSet> r = new HashMap<AbstractProject,RangeSet>();
        for (AbstractProject p : getParent().getDownstreamProjects()) {
1081
            if (p.isFingerprintConfigured())
1082 1083 1084 1085
                r.put(p,getDownstreamRelationship(p));
        }
        return r;
    }
1086

1087 1088 1089
    /**
     * Gets the upstream builds of this build, which are the builds of the
     * upstream projects whose artifacts feed into this build.
K
kohsuke 已提交
1090 1091
     *
     * @see #getTransitiveUpstreamBuilds()
1092 1093
     */
    public Map<AbstractProject,Integer> getUpstreamBuilds() {
K
kohsuke 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
        return _getUpstreamBuilds(getParent().getUpstreamProjects());
    }

    /**
     * Works like {@link #getUpstreamBuilds()}  but also includes all the transitive
     * dependencies as well.
     */
    public Map<AbstractProject,Integer> getTransitiveUpstreamBuilds() {
        return _getUpstreamBuilds(getParent().getTransitiveUpstreamProjects());
    }

    private Map<AbstractProject, Integer> _getUpstreamBuilds(Collection<AbstractProject> projects) {
1106
        Map<AbstractProject,Integer> r = new HashMap<AbstractProject,Integer>();
K
kohsuke 已提交
1107
        for (AbstractProject p : projects) {
1108
            int n = getUpstreamRelationship(p);
1109
            if (n>=0)
1110 1111 1112 1113 1114
                r.put(p,n);
        }
        return r;
    }

1115 1116 1117 1118
    /**
     * Gets the changes in the dependency between the given build and this build.
     */
    public Map<AbstractProject,DependencyChange> getDependencyChanges(AbstractBuild from) {
1119
        if (from==null)             return Collections.emptyMap(); // make it easy to call this from views
1120 1121
        FingerprintAction n = this.getAction(FingerprintAction.class);
        FingerprintAction o = from.getAction(FingerprintAction.class);
1122
        if (n==null || o==null)     return Collections.emptyMap();
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

        Map<AbstractProject,Integer> ndep = n.getDependencies();
        Map<AbstractProject,Integer> odep = o.getDependencies();

        Map<AbstractProject,DependencyChange> r = new HashMap<AbstractProject,DependencyChange>();

        for (Map.Entry<AbstractProject,Integer> entry : odep.entrySet()) {
            AbstractProject p = entry.getKey();
            Integer oldNumber = entry.getValue();
            Integer newNumber = ndep.get(p);
1133
            if (newNumber!=null && oldNumber.compareTo(newNumber)<0) {
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
                r.put(p,new DependencyChange(p,oldNumber,newNumber));
            }
        }

        return r;
    }

    /**
     * Represents a change in the dependency.
     */
    public static final class DependencyChange {
        /**
         * The dependency project.
         */
        public final AbstractProject project;
        /**
         * Version of the dependency project used in the previous build.
         */
        public final int fromId;
        /**
         * {@link Build} object for {@link #fromId}. Can be null if the log is gone.
         */
        public final AbstractBuild from;
        /**
         * Version of the dependency project used in this build.
         */
        public final int toId;

        public final AbstractBuild to;

        public DependencyChange(AbstractProject<?,?> project, int fromId, int toId) {
            this.project = project;
            this.fromId = fromId;
            this.toId = toId;
            this.from = project.getBuildByNumber(fromId);
            this.to = project.getBuildByNumber(toId);
        }
1171 1172 1173 1174 1175

        /**
         * Gets the {@link AbstractBuild} objects (fromId,toId].
         * <p>
         * This method returns all such available builds in the ascending order
1176
         * of IDs, but due to log rotations, some builds may be already unavailable.
1177 1178 1179 1180 1181
         */
        public List<AbstractBuild> getBuilds() {
            List<AbstractBuild> r = new ArrayList<AbstractBuild>();

            AbstractBuild<?,?> b = (AbstractBuild)project.getNearestBuild(fromId);
1182
            if (b!=null && b.getNumber()==fromId)
1183 1184
                b = b.getNextBuild(); // fromId exclusive

1185
            while (b!=null && b.getNumber()<=toId) {
1186 1187 1188 1189 1190 1191
                r.add(b);
                b = b.getNextBuild();
            }

            return r;
        }
1192
    }
1193

1194 1195 1196 1197
    //
    // web methods
    //

K
kohsuke 已提交
1198 1199 1200 1201 1202 1203
    /**
     * Stops this build if it's still going.
     *
     * If we use this/executor/stop URL, it causes 404 if the build is already killed,
     * as {@link #getExecutor()} returns null.
     */
1204
    public synchronized void doStop(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
K
kohsuke 已提交
1205
        Executor e = getExecutor();
1206
        if (e!=null)
K
kohsuke 已提交
1207 1208 1209 1210 1211
            e.doStop(req,rsp);
        else
            // nothing is building
            rsp.forwardToPreviousPage(req);
    }
1212 1213

    private static final Logger LOGGER = Logger.getLogger(AbstractBuild.class.getName());
1214
}