/hudson-core/src/main/java/hudson/model/Build.java

http://github.com/hudson/hudson · Java · 182 lines · 89 code · 18 blank · 75 comment · 16 complexity · 121cb4cb062545b28ba77ec5154550b6 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2011, Oracle Corporation, Kohsuke Kawaguchi, Martin Eigenbrodt, Anton Kozak
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.model;
  25. import hudson.tasks.BuildStep;
  26. import hudson.tasks.BuildWrapper;
  27. import hudson.tasks.Builder;
  28. import hudson.tasks.Recorder;
  29. import hudson.tasks.Notifier;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.util.ArrayList;
  33. import java.util.Calendar;
  34. import java.util.Collection;
  35. import java.util.List;
  36. import java.util.logging.Logger;
  37. import static hudson.model.Result.FAILURE;
  38. import static hudson.model.Result.ABORTED;
  39. /**
  40. * A build of a {@link Project}.
  41. *
  42. * <h2>Steps of a build</h2>
  43. * <p>
  44. * Roughly speaking, a {@link Build} goes through the following stages:
  45. *
  46. * <dl>
  47. * <dt>SCM checkout
  48. * <dd>Hudson decides which directory to use for a build, then the source code is checked out
  49. *
  50. * <dt>Pre-build steps
  51. * <dd>Everyone gets their {@link BuildStep#prebuild(AbstractBuild, BuildListener)} invoked
  52. * to indicate that the build is starting
  53. *
  54. * <dt>Build wrapper set up
  55. * <dd>{@link BuildWrapper#setUp(AbstractBuild, Launcher, BuildListener)} is invoked. This is normally
  56. * to prepare an environment for the build.
  57. *
  58. * <dt>Builder runs
  59. * <dd>{@link Builder#perform(AbstractBuild, Launcher, BuildListener)} is invoked. This is where
  60. * things that are useful to users happen, like calling Ant, Make, etc.
  61. *
  62. * <dt>Recorder runs
  63. * <dd>{@link Recorder#perform(AbstractBuild, Launcher, BuildListener)} is invoked. This is normally
  64. * to record the output from the build, such as test results.
  65. *
  66. * <dt>Notifier runs
  67. * <dd>{@link Notifier#perform(AbstractBuild, Launcher, BuildListener)} is invoked. This is normally
  68. * to send out notifications, based on the results determined so far.
  69. * </dl>
  70. *
  71. * <p>
  72. * And beyond that, the build is considered complete, and from then on {@link Build} object is there to
  73. * keep the record of what happened in this build.
  74. *
  75. * @author Kohsuke Kawaguchi
  76. */
  77. public abstract class Build <P extends BaseBuildableProject<P,B>,B extends Build<P,B>>
  78. extends AbstractBuild<P,B> {
  79. /**
  80. * Creates a new build.
  81. */
  82. protected Build(P project) throws IOException {
  83. super(project);
  84. }
  85. protected Build(P job, Calendar timestamp) {
  86. super(job, timestamp);
  87. }
  88. /**
  89. * Loads a build from a log file.
  90. */
  91. protected Build(P project, File buildDir) throws IOException {
  92. super(project,buildDir);
  93. }
  94. //
  95. //
  96. // actions
  97. //
  98. //
  99. @Override
  100. public void run() {
  101. run(createRunner());
  102. }
  103. protected Runner createRunner() {
  104. return new RunnerImpl();
  105. }
  106. protected class RunnerImpl extends AbstractRunner {
  107. protected Result doRun(BuildListener listener) throws Exception {
  108. if(!preBuild(listener,project.getBuilders()))
  109. return FAILURE;
  110. if(!preBuild(listener,project.getPublishers()))
  111. return FAILURE;
  112. Result r = null;
  113. try {
  114. List<BuildWrapper> wrappers = new ArrayList<BuildWrapper>(project.getBuildWrappers().values());
  115. ParametersAction parameters = getAction(ParametersAction.class);
  116. if (parameters != null)
  117. parameters.createBuildWrappers(Build.this,wrappers);
  118. for( BuildWrapper w : wrappers ) {
  119. Environment e = w.setUp((AbstractBuild<?,?>)Build.this, launcher, listener);
  120. if(e==null)
  121. return (r = FAILURE);
  122. buildEnvironments.add(e);
  123. }
  124. if(!build(listener,project.getBuilders()))
  125. r = FAILURE;
  126. } catch (InterruptedException e) {
  127. r = ABORTED;
  128. throw e;
  129. } finally {
  130. if (r != null) setResult(r);
  131. // tear down in reverse order
  132. boolean failed=false;
  133. for( int i=buildEnvironments.size()-1; i>=0; i-- ) {
  134. if (!buildEnvironments.get(i).tearDown(Build.this,listener)) {
  135. failed=true;
  136. }
  137. }
  138. // WARNING The return in the finally clause will trump any return before
  139. if (failed) return FAILURE;
  140. }
  141. return r;
  142. }
  143. public void post2(BuildListener listener) throws IOException, InterruptedException {
  144. if (!performAllBuildSteps(listener, project.getPublishers(), true))
  145. setResult(FAILURE);
  146. if (!performAllBuildSteps(listener, project.getProperties(), true))
  147. setResult(FAILURE);
  148. }
  149. @Override
  150. public void cleanUp(BuildListener listener) throws Exception {
  151. // at this point it's too late to mark the build as a failure, so ignore return value.
  152. performAllBuildSteps(listener, project.getPublishers(), false);
  153. performAllBuildSteps(listener, project.getProperties(), false);
  154. super.cleanUp(listener);
  155. }
  156. private boolean build(BuildListener listener, Collection<Builder> steps) throws IOException, InterruptedException {
  157. for( BuildStep bs : steps )
  158. if(!perform(bs,listener))
  159. return false;
  160. return true;
  161. }
  162. }
  163. private static final Logger LOGGER = Logger.getLogger(Build.class.getName());
  164. }