@Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws InterruptedException, IOException {

    Result result = build.getResult();
    if (result == null) {
      result = Result.SUCCESS;
    }

    String packIdString = getPackageId(build, listener);
    PackId packId = PackId.parsePid(packIdString);
    if (packId == null) {
      listener.fatalError("Failed to parse Package ID: %s%n", packIdString);
      return false;
    }

    String wspFilterString = getWspFilter(build, listener);
    WspFilter filter = WspFilter.parseSimpleSpec(wspFilterString);

    GraniteClientConfig clientConfig =
        new GraniteClientConfig(
            getBaseUrl(build, listener), credentialsId, requestTimeout, serviceTimeout, waitDelay);

    BuildPackageCallable callable =
        new BuildPackageCallable(clientConfig, listener, packId, filter, download);

    final String fLocalDirectory = getLocalDirectory(build, listener);
    result = result.combine(build.getWorkspace().child(fLocalDirectory).act(callable));

    return result.isBetterOrEqualTo(Result.UNSTABLE);
  }
コード例 #2
0
 private String getStatus(Run r) {
   Result result = r.getResult();
   String status = null;
   if (result != null) {
     status = result.toString();
   }
   return status;
 }
コード例 #3
0
  @Override
  public void perform(Run<?, ?> build, FilePath ws, Launcher launcher, TaskListener listener)
      throws InterruptedException {
    if (artifacts.length() == 0) {
      listener.error(Messages.ArtifactArchiver_NoIncludes());
      build.setResult(Result.FAILURE);
      return;
    }

    if (onlyIfSuccessful
        && build.getResult() != null
        && build.getResult().isWorseThan(Result.UNSTABLE)) {
      listener.getLogger().println(Messages.ArtifactArchiver_SkipBecauseOnlyIfSuccessful());
      return;
    }

    listener.getLogger().println(Messages.ArtifactArchiver_ARCHIVING_ARTIFACTS());
    try {
      String artifacts = build.getEnvironment(listener).expand(this.artifacts);

      Map<String, String> files =
          ws.act(new ListFiles(artifacts, excludes, defaultExcludes, caseSensitive));
      if (!files.isEmpty()) {
        build
            .pickArtifactManager()
            .archive(ws, launcher, BuildListenerAdapter.wrap(listener), files);
        if (fingerprint) {
          new Fingerprinter(artifacts).perform(build, ws, launcher, listener);
        }
      } else {
        Result result = build.getResult();
        if (result != null && result.isBetterOrEqualTo(Result.UNSTABLE)) {
          // If the build failed, don't complain that there was no matching artifact.
          // The build probably didn't even get to the point where it produces artifacts.
          listenerWarnOrError(listener, Messages.ArtifactArchiver_NoMatchFound(artifacts));
          String msg = null;
          try {
            msg =
                ws.validateAntFileMask(
                    artifacts, FilePath.VALIDATE_ANT_FILE_MASK_BOUND, caseSensitive);
          } catch (Exception e) {
            listenerWarnOrError(listener, e.getMessage());
          }
          if (msg != null) listenerWarnOrError(listener, msg);
        }
        if (!allowEmptyArchive) {
          build.setResult(Result.FAILURE);
        }
        return;
      }
    } catch (IOException e) {
      Util.displayIOException(e, listener);
      e.printStackTrace(listener.error(Messages.ArtifactArchiver_FailedToArchive(artifacts)));
      build.setResult(Result.FAILURE);
      return;
    }
  }
コード例 #4
0
 private static Result resultFromString(String s) {
   Result result = Result.fromString(s);
   // fromString returns FAILURE for unknown strings instead of
   // IllegalArgumentException. Don't know why the author thought that this
   // is useful ...
   if (!result.toString().equals(s)) {
     throw new IllegalArgumentException("Unknown result type '" + s + "'");
   }
   return result;
 }
コード例 #5
0
 private boolean evaluateResult(Result result) {
   List<SubBuild> builders = getBuilders();
   for (SubBuild subBuild : builders) {
     Result buildResult = subBuild.getResult();
     if (buildResult != null && buildResult.isWorseThan(result)) {
       return true;
     }
   }
   return false;
 }
コード例 #6
0
  /**
   * Displays the combined status of all modules.
   *
   * <p>More precisely, this picks up the status of this build itself, plus all the latest builds of
   * the modules that belongs to this build.
   */
  @Override
  public Result getResult() {
    Result r = super.getResult();

    for (IvyBuild b : getModuleLastBuilds().values()) {
      Result br = b.getResult();
      if (r == null) r = br;
      else if (br == Result.NOT_BUILT) continue; // UGLY: when computing combined status, ignore the
      // modules that were not built
      else if (br != null) r = r.combine(br);
    }

    return r;
  }
コード例 #7
0
  public BuildResultDTO convert(final Result source) {
    assert source != null;

    log.trace("Converting: {}", source);

    return BuildResultDTO.valueOf(source.toString().toUpperCase());
  }
コード例 #8
0
 @Override
 public Result runBuild(DynamicBuild build, BuildExecutionContext buildExecutionContext, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
     Map<String,Object> buildEnvironment = build.getEnvironmentWithChangeSet(listener);
     ShellCommands checkoutCommands = getCheckoutCommands(buildEnvironment);
     Map config = new GroovyYamlTemplateProcessor(getDotCiYml(build), buildEnvironment).getConfig();
     this.buildConfiguration = getBuildConfiguration(build.getParent().getFullName(),config,build.getBuildId(),checkoutCommands,build.getSha(),build.getNumber());
     build.setAxisList(buildConfiguration.getAxisList());
     Result result ;
     if(buildConfiguration.isParallelized()){
         ShellScriptRunner shellScriptRunner = new ShellScriptRunner(buildExecutionContext, listener);
         Result checkoutResult = shellScriptRunner.runScript(checkoutCommands);
         if(Result.FAILURE.equals(checkoutResult)) return checkoutResult;
         result = runMultiConfigbuildRunner(build, buildConfiguration, listener,launcher);
     }else{
         result = runSubBuild(new Combination(ImmutableMap.of("script", buildConfiguration.getOnlyRun())), buildExecutionContext, listener);
     }
     Result pluginResult = runPlugins(build, buildConfiguration.getPlugins(), listener, launcher);
     Result notifierResult = runNotifiers(build, buildConfiguration.getNotifiers(), listener);
     return  result.combine(pluginResult).combine(notifierResult);
 }
コード例 #9
0
    /**
     * Decides whether the next build should be triggered.
     *
     * @param buildResult the current build result
     * @param runIfResult the definition when to trigger the next build
     * @return <code>true</code> if the next build shold be triggered
     */
    private boolean shouldScheduleNextJob(Result buildResult, String runIfResult) {
      // If runIfResult is null, set it to "allCases".
      if (runIfResult == null) {
        runIfResult = "allCases";
      }
      // If runIfResult is "allCases", we're running regardless.
      if (runIfResult.equals("allCases")) {
        return true;
      } else {
        // Otherwise, we're going to need to compare against the build
        // result.

        if (runIfResult.equals("success")) {
          return ((buildResult == null) || (buildResult.isBetterOrEqualTo(Result.SUCCESS)));
        } else if (runIfResult.equals("unstable")) {
          return ((buildResult == null) || (buildResult.isBetterOrEqualTo(Result.UNSTABLE)));
        }
      }

      // If we get down here, something weird's going on. Return false.
      return false;
    }
コード例 #10
0
  @Override
  public boolean perform(
      final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener)
      throws InterruptedException, IOException {
    final PrintStream console = listener.getLogger();
    if (!isBuildGoodEnoughToRun(build, console)) return true;
    final BPBuildEnv currentBuildEnv =
        new BPBuildEnv(
            getEnvironmentVariables(build, listener), build.getWorkspace(), build.getTimestamp());
    BPBuildEnv targetBuildEnv = null;
    if (PROMOTION_CLASS_NAME.equals(build.getClass().getCanonicalName())) {
      AbstractBuild<?, ?> promoted;
      try {
        final Method getTarget = build.getClass().getMethod("getTarget", (Class<?>[]) null);
        promoted = (AbstractBuild) getTarget.invoke(build, (Object[]) null);
      } catch (Exception e) {
        throw new RuntimeException(Messages.exception_failedToGetPromotedBuild(), e);
      }
      targetBuildEnv =
          new BPBuildEnv(
              getEnvironmentVariables(promoted, listener),
              new FilePath(promoted.getArtifactsDir()),
              promoted.getTimestamp());
    }

    final BPBuildInfo buildInfo =
        new BPBuildInfo(
            listener,
            consolePrefix,
            Hudson.getInstance().getRootPath(),
            currentBuildEnv,
            targetBuildEnv);
    fixup(build, buildInfo);
    final Result result = delegate.perform(buildInfo);

    if (build.getResult() == null) build.setResult(result);
    else build.setResult(result.combine(build.getResult()));
    return result.isBetterOrEqualTo(Result.UNSTABLE);
  }
コード例 #11
0
  private int buildWorseOrEqualsTo(final Result r) {
    LOGGER.log(Level.FINE, "Starting to build " + r.toString() + " jobs.");

    ArrayList<Predicate<AbstractProject<?, ?>>> filters =
        new ArrayList<Predicate<AbstractProject<?, ?>>>();

    filters.add(
        new Predicate<AbstractProject<?, ?>>() {
          @Override
          public boolean apply(AbstractProject<?, ?> project) {
            AbstractBuild<?, ?> build = project.getLastCompletedBuild();
            return build == null || build.getResult().isWorseOrEqualTo(r);
          }
        });

    filters = addSubFilters(filters);

    int i = build(filters);

    LOGGER.log(Level.FINE, "Finished building " + r.toString() + " jobs.");

    return i;
  }
 @DataBoundConstructor
 public SelfHealingMatrixExecutionStrategy(
     String logPattern,
     Result worseThanOrEqualTo,
     Result betterThanOrEqualTo,
     int maxRetries,
     boolean stopRetryingAfterOneFails) {
   this.logPattern = logPattern == null ? "" : logPattern;
   this.worseThanOrEqualTo = worseThanOrEqualTo == null ? Result.FAILURE : worseThanOrEqualTo;
   this.betterThanOrEqualTo =
       betterThanOrEqualTo == null
           ? Result.ABORTED
           : betterThanOrEqualTo.isWorseOrEqualTo(this.worseThanOrEqualTo)
               ? betterThanOrEqualTo
               : this.worseThanOrEqualTo;
   this.maxRetries = maxRetries < 0 ? 1 : maxRetries;
   this.stopRetryingAfterOneFails = stopRetryingAfterOneFails;
 }
コード例 #13
0
  @Override
  public void onCompleted(AbstractBuild run, TaskListener listener) {
    Lamps plugin = Lamps.getInstance();
    Set<String> jobs = plugin.getJobs();
    String jobName = run.getParent().getFullName();
    XfEventMessage xfEventMessage = new XfEventMessage();

    if (jobs.contains(jobName)) {
      Result result = run.getResult();
      Set<Lamp> activeLamps = plugin.getLampsContainingJob(jobName);
      for (Lamp lamp : activeLamps) {
        Result lampResult = result;

        xfEventMessage.sendColorMessage(lamp, lampResult, States.Action.SOLID);

        // Create Notification for LCD
        StringBuilder infoMsg = new StringBuilder(64);
        infoMsg.append(jobName).append(' ').append(run.getDisplayName()).append('\n');
        if (Result.FAILURE.equals(result)) {
          ArrayList<String> blame = Lists.newArrayList();
          if (lamp.isBlame()) {
            Set<User> culprits = run.getCulprits();
            for (User user : culprits) {
              blame.add(user.getDisplayName());
            }
          }
          if (blame.isEmpty()) {
            blame.add("Somebody");
          }
          infoMsg.insert(0, Joiner.on(", ").join(blame) + " broke the build: ");
          infoMsg.append(result.toString());
          listener.getLogger().println("[XFD] Updating Lamp display: " + infoMsg.toString());
        } else if (Result.ABORTED.equals(result)) {
          String causeMsg = "BUILD ABORTED";
          infoMsg.append(causeMsg);
          listener.getLogger().println("[XFD] Updating Lamp display: " + infoMsg.toString());
        } else {
          infoMsg.append(result.toString());
        }
        xfEventMessage.sendLCDMessage(lamp, infoMsg.toString());

        if (lamp.isSfx()) {
          try {
            Thread.sleep(1000);
            xfEventMessage.sendSfxMessage(lamp, lampResult);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        if (States.resultColorMap.get(lampResult).equals(States.Color.RED) && lamp.isNoisy()) {
          try {
            Thread.sleep(1000);
            xfEventMessage.sendBuzzerMessage(lamp);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        if (lamp.isAggregate()) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          plugin.updateAggregateStatus(lamp);
        }
      }
    }
  }
  /**
   * Waits for the given configurations to finish, retrying any that qualify to be rerun.
   *
   * @param execution Provided by the plugin.
   * @param patterns List of regular expression patterns used to scan the log to determine if a
   *     build should be rerun.
   * @param retries Mutable map that tracks the number of times a specific configuration has been
   *     retried.
   * @param configurations The configurations that have already been scheduled to run that should be
   *     waited for to finish.
   * @return The worst result of all the runs. If a build was rerun, only the result of the rerun is
   *     considered.
   * @throws InterruptedException
   * @throws IOException
   */
  private Result waitForMatrixRuns(
      MatrixBuild.MatrixBuildExecution execution,
      List<Pattern> patterns,
      Map<MatrixConfiguration, Integer> retries,
      LinkedList<MatrixConfiguration> configurations)
      throws InterruptedException, IOException {
    BuildListener listener = execution.getListener();
    PrintStream logger = listener.getLogger();

    Map<String, String> whyBlockedMap =
        new HashMap<
            String,
            String>(); // keep track of why builds are blocked so we can print unique messages when
    // they change.
    Result finalResult = Result.SUCCESS;
    int iteration = 0;
    boolean continueRetrying = true;
    while (!configurations.isEmpty()) {
      ++iteration;
      MatrixConfiguration configuration = configurations.removeFirst();
      if (isBuilding(execution, configuration, whyBlockedMap)) {
        if (iteration >= configurations.size()) {
          // Every time we loop through all the configurations, sleep for a bit.
          // This is to prevent polling too often while everything is still building.
          iteration = 0;
          Thread.sleep(1000);
        }
        configurations.add(configuration);
        continue;
      }

      Run parentBuild = execution.getBuild();
      MatrixRun matrixRun = configuration.getBuildByNumber(parentBuild.getNumber());
      Result runResult = matrixRun.getResult();
      if (continueRetrying
          && runResult.isWorseOrEqualTo(getWorseThanOrEqualTo())
          && runResult.isBetterOrEqualTo(getBetterThanOrEqualTo())) {
        if (matchesPattern(matrixRun, patterns)) {
          int retriedCount = retries.get(configuration);
          if (retriedCount < getMaxRetries()) {
            ++retriedCount;
            retries.put(configuration, retriedCount);
            // rerun
            String logMessage =
                String.format(
                    "%s was %s. Matched pattern to rerun. Rerunning (%d).",
                    matrixRun, runResult, retriedCount);
            listener.error(logMessage);

            HealedAction action = parentBuild.getAction(HealedAction.class);
            if (action == null) {
              //noinspection SynchronizationOnLocalVariableOrMethodParameter
              synchronized (parentBuild.getActions()) {
                action = parentBuild.getAction(HealedAction.class);
                if (action == null) {
                  action = new HealedAction(matrixRun.getCharset());
                  parentBuild.addAction(action);
                }
              }
            }
            action.addAutoHealedJob(matrixRun);

            MatrixConfiguration parent = matrixRun.getParent();
            if (parent != null) {
              // I'm paranoid about NPEs
              parent.removeRun(matrixRun);
              matrixRun.delete();
            } else {
              LOGGER.severe(
                  "couldn't remove old run, parent was null. This is a Jenkins core bug.");
            }
            scheduleConfigurationBuild(
                execution, configuration, new SelfHealingCause(parentBuild, retriedCount));
            configurations.add(configuration);
            continue;
          } else {
            String logMessage =
                String.format(
                    "%s was %s. Matched pattern to rerun, but the max number of retries (%d) has been met.",
                    matrixRun, runResult, getMaxRetries());
            listener.error(logMessage);
            if (getStopRetryingAfterOneFails()) {
              listener.error("Not retrying any more builds.");
              continueRetrying = false;
            }
          }
        } else {
          String logMessage =
              String.format(
                  "%s was %s. It did not match the pattern to rerun. Accepting result.",
                  matrixRun, runResult);
          logger.println(logMessage);
        }
      }
      notifyEndRun(matrixRun, execution.getAggregators(), execution.getListener());
      finalResult = finalResult.combine(runResult);
    }
    return finalResult;
  }
コード例 #15
0
 protected boolean shouldStopExecutionDueTo(GateReport gateReport) {
   Result result = gateReport.getResult();
   return result.equals(Result.FAILURE) || result.equals(Result.NOT_BUILT);
 }
コード例 #16
0
  /**
   * Changes the build result if baseline operation fails. (So cannot override {@linkplain
   * #needsToRunAfterFinalized()}).
   */
  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws IOException, InterruptedException {
    Logger.debug("Invoking perform callout " + this.getClass().getName());
    long key = -1L;
    try {
      if (!(build.getProject().getScm() instanceof DimensionsSCM)) {
        final String message =
            "[DIMENSIONS] This plugin only works with the Dimensions SCM engine.";
        listener.fatalError(message);
        build.setResult(Result.FAILURE);
        throw new IOException(message);
      }
      // Create baseline if SUCCESS or UNSTABLE (could be just some Checkstyle violations).
      Result result = build.getResult();
      if (result != null && result.isBetterThan(Result.FAILURE)) {
        if (scm == null) {
          scm = (DimensionsSCM) build.getProject().getScm();
        }
        Logger.debug(
            "Dimensions user is "
                + scm.getJobUserName()
                + " , Dimensions installation is "
                + scm.getJobServer());
        key =
            scm.getAPI()
                .login(
                    scm.getJobUserName(),
                    scm.getJobPasswd(),
                    scm.getJobDatabase(),
                    scm.getJobServer(),
                    build);
        if (key > 0L) {
          VariableResolver<String> myResolver = build.getBuildVariableResolver();
          String requests = myResolver.resolve("DM_TARGET_REQUEST");
          String blnId = myResolver.resolve("DM_BASELINE");
          StringBuffer cblId = new StringBuffer();

          if (requests != null) {
            requests = requests.replaceAll(" ", "");
            requests = requests.toUpperCase(Values.ROOT_LOCALE);
          }

          if (blnScope != null && blnScope.length() > 0 && blnScope.equals("REVISED")) {
            if (requests == null
                || blnId == null
                || requests.length() == 0
                || blnId.length() == 0) {
              listener.fatalError(
                  "[DIMENSIONS] A revised baseline is only valid if you have specified DM_TARGET_REQUEST and DM_BASELINE.");
              build.setResult(Result.FAILURE);
              return false;
            }
          }

          {
            DimensionsResult res =
                scm.getAPI()
                    .createBaseline(
                        key,
                        scm.getProjectVersion(build, listener),
                        build,
                        blnScope,
                        blnTemplate,
                        blnOwningPart,
                        blnType,
                        requests,
                        blnId,
                        blnName,
                        cblId);
            if (res == null) {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] The build failed to be tagged in Dimensions");
              listener.getLogger().flush();
              build.setResult(Result.FAILURE);
              canBaselineDeploy = canBaselineAction = canBaselineBuild = false;
            } else {
              listener
                  .getLogger()
                  .println(
                      "[DIMENSIONS] Build was successfully tagged in Dimensions as a baseline");
              listener
                  .getLogger()
                  .println(
                      "[DIMENSIONS] ("
                          + res.getMessage().replaceAll("\n", "\n[DIMENSIONS] ")
                          + ")");
              listener.getLogger().flush();
            }
          }
          if (canBaselineDeploy) {
            listener
                .getLogger()
                .println("[DIMENSIONS] Submitting a deployment job to Dimensions...");
            listener.getLogger().flush();
            DimensionsResult res =
                scm.getAPI()
                    .deployBaseline(
                        key,
                        scm.getProjectName(build, listener),
                        build,
                        deployState,
                        cblId.toString());
            if (res == null) {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] The build baseline failed to be deployed in Dimensions");
              listener.getLogger().flush();
              build.setResult(Result.FAILURE);
              canBaselineDeploy = canBaselineAction = canBaselineBuild = false;
            } else {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] Build baseline was successfully deployed in Dimensions");
              listener
                  .getLogger()
                  .println(
                      "[DIMENSIONS] ("
                          + res.getMessage().replaceAll("\n", "\n[DIMENSIONS] ")
                          + ")");
              listener.getLogger().flush();
            }
          }

          // This will active the build baseline functionality
          if (canBaselineBuild) {
            listener.getLogger().println("[DIMENSIONS] Submitting a build job to Dimensions...");
            listener.getLogger().flush();
            DimensionsResult res =
                scm.getAPI()
                    .buildBaseline(
                        key,
                        area,
                        scm.getProjectName(build, listener),
                        batch,
                        buildClean,
                        buildConfig,
                        buildOptions,
                        capture,
                        requests,
                        buildTargets,
                        build,
                        cblId.toString());
            if (res == null) {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] The build baseline failed to be built in Dimensions");
              listener.getLogger().flush();
              build.setResult(Result.FAILURE);
              canBaselineDeploy = canBaselineAction = canBaselineBuild = false;
            } else {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] Build baseline was successfully built in Dimensions");
              listener
                  .getLogger()
                  .println(
                      "[DIMENSIONS] ("
                          + res.getMessage().replaceAll("\n", "\n[DIMENSIONS] ")
                          + ")");
              listener.getLogger().flush();
            }
          }

          if (canBaselineAction) {
            listener
                .getLogger()
                .println("[DIMENSIONS] Actioning the build baseline in Dimensions...");
            listener.getLogger().flush();
            DimensionsResult res =
                scm.getAPI()
                    .actionBaseline(
                        key,
                        scm.getProjectName(build, listener),
                        build,
                        actionState,
                        cblId.toString());
            if (res == null) {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] The build baseline failed to be actioned in Dimensions");
              build.setResult(Result.FAILURE);
              listener.getLogger().flush();
            } else {
              listener
                  .getLogger()
                  .println("[DIMENSIONS] Build baseline was successfully actioned in Dimensions");
              listener
                  .getLogger()
                  .println(
                      "[DIMENSIONS] ("
                          + res.getMessage().replaceAll("\n", "\n[DIMENSIONS] ")
                          + ")");
              listener.getLogger().flush();
            }
          }
        } else {
          listener.fatalError("[DIMENSIONS] Login to Dimensions failed.");
          build.setResult(Result.FAILURE);
          return false;
        }
      }
    } catch (Exception e) {
      String message =
          Values.exceptionMessage("Unable to tag build in Dimensions", e, "no message");
      listener.fatalError(message);
      Logger.debug(message, e);
      build.setResult(Result.FAILURE);
      return false;
    } finally {
      if (scm != null) {
        scm.getAPI().logout(key, build);
      }
    }
    return true;
  }
コード例 #17
0
 protected boolean hasNotFullyBeenExecuted(Gate gate) {
   Result result = this.qualityLineReport.getResultFor(gate);
   return result.equals(Result.NOT_BUILT);
 }
コード例 #18
0
  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final BuildListener listener)
      throws InterruptedException {

    // during matrix build, the push back would happen at the very end only once for the whole
    // matrix,
    // not for individual configuration build.
    if (build instanceof MatrixRun) {
      return true;
    }

    SCM scm = build.getProject().getScm();

    if (!(scm instanceof GitSCM)) {
      return false;
    }

    final GitSCM gitSCM = (GitSCM) scm;

    if (gitSCM.getUseShallowClone()) {
      listener.getLogger().println("GitPublisher disabled while using shallow clone.");
      return true;
    }

    final String projectName = build.getProject().getName();
    final FilePath workspacePath = build.getWorkspace();
    final int buildNumber = build.getNumber();
    final Result buildResult = build.getResult();

    // If pushOnlyIfSuccess is selected and the build is not a success, don't push.
    if (pushOnlyIfSuccess && buildResult.isWorseThan(Result.SUCCESS)) {
      listener
          .getLogger()
          .println(
              "Build did not succeed and the project is configured to only push after a successful build, so no pushing will occur.");
      return true;
    } else {
      final String gitExe = gitSCM.getGitExe(build.getBuiltOn(), listener);
      EnvVars tempEnvironment;
      try {
        tempEnvironment = build.getEnvironment(listener);
      } catch (IOException e) {
        e.printStackTrace(listener.error("Failed to build up environment"));
        tempEnvironment = new EnvVars();
      }

      String confName = gitSCM.getGitConfigNameToUse();
      if ((confName != null) && (!confName.equals(""))) {
        tempEnvironment.put("GIT_COMMITTER_NAME", confName);
        tempEnvironment.put("GIT_AUTHOR_NAME", confName);
      }
      String confEmail = gitSCM.getGitConfigEmailToUse();
      if ((confEmail != null) && (!confEmail.equals(""))) {
        tempEnvironment.put("GIT_COMMITTER_EMAIL", confEmail);
        tempEnvironment.put("GIT_AUTHOR_EMAIL", confEmail);
      }

      final EnvVars environment = tempEnvironment;
      final FilePath workingDirectory = gitSCM.workingDirectory(workspacePath, environment);

      boolean pushResult = true;
      // If we're pushing the merge back...
      if (pushMerge) {
        boolean mergeResult;
        try {
          mergeResult =
              workingDirectory.act(
                  new FileCallable<Boolean>() {
                    private static final long serialVersionUID = 1L;

                    public Boolean invoke(File workspace, VirtualChannel channel)
                        throws IOException {

                      IGitAPI git =
                          new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                      // We delete the old tag generated by the SCM plugin
                      String buildnumber = "jenkins-" + projectName + "-" + buildNumber;
                      git.deleteTag(buildnumber);

                      // And add the success / fail state into the tag.
                      buildnumber += "-" + buildResult.toString();

                      git.tag(buildnumber, "Jenkins Build #" + buildNumber);

                      PreBuildMergeOptions mergeOptions = gitSCM.getMergeOptions();

                      if (mergeOptions.doMerge() && buildResult.isBetterOrEqualTo(Result.SUCCESS)) {
                        RemoteConfig remote = mergeOptions.getMergeRemote();
                        listener
                            .getLogger()
                            .println(
                                "Pushing HEAD to branch "
                                    + mergeOptions.getMergeTarget()
                                    + " of "
                                    + remote.getName()
                                    + " repository");

                        git.push(remote, "HEAD:" + mergeOptions.getMergeTarget());
                      } else {
                        // listener.getLogger().println("Pushing result " + buildnumber + " to
                        // origin repository");
                        // git.push(null);
                      }

                      return true;
                    }
                  });
        } catch (Throwable e) {
          e.printStackTrace(listener.error("Failed to push merge to origin repository"));
          build.setResult(Result.FAILURE);
          mergeResult = false;
        }

        if (!mergeResult) {
          pushResult = false;
        }
      }
      if (isPushTags()) {
        boolean allTagsResult = true;
        for (final TagToPush t : tagsToPush) {
          boolean tagResult = true;
          if (t.getTagName() == null) {
            listener.getLogger().println("No tag to push defined");
            tagResult = false;
          }
          if (t.getTargetRepoName() == null) {
            listener.getLogger().println("No target repo to push to defined");
            tagResult = false;
          }
          if (tagResult) {
            final String tagName = environment.expand(t.getTagName());
            final String targetRepo = environment.expand(t.getTargetRepoName());

            try {
              tagResult =
                  workingDirectory.act(
                      new FileCallable<Boolean>() {
                        private static final long serialVersionUID = 1L;

                        public Boolean invoke(File workspace, VirtualChannel channel)
                            throws IOException {

                          IGitAPI git =
                              new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                          RemoteConfig remote = gitSCM.getRepositoryByName(targetRepo);

                          if (remote == null) {
                            listener
                                .getLogger()
                                .println("No repository found for target repo name " + targetRepo);
                            return false;
                          }

                          if (t.isCreateTag()) {
                            if (git.tagExists(tagName)) {
                              listener
                                  .getLogger()
                                  .println(
                                      "Tag "
                                          + tagName
                                          + " already exists and Create Tag is specified, so failing.");
                              return false;
                            }
                            git.tag(tagName, "Jenkins Git plugin tagging with " + tagName);
                          } else if (!git.tagExists(tagName)) {
                            listener
                                .getLogger()
                                .println(
                                    "Tag "
                                        + tagName
                                        + " does not exist and Create Tag is not specified, so failing.");
                            return false;
                          }

                          listener
                              .getLogger()
                              .println("Pushing tag " + tagName + " to repo " + targetRepo);
                          git.push(remote, tagName);

                          return true;
                        }
                      });
            } catch (Throwable e) {
              e.printStackTrace(
                  listener.error("Failed to push tag " + tagName + " to " + targetRepo));
              build.setResult(Result.FAILURE);
              tagResult = false;
            }
          }

          if (!tagResult) {
            allTagsResult = false;
          }
        }
        if (!allTagsResult) {
          pushResult = false;
        }
      }

      if (isPushBranches()) {
        boolean allBranchesResult = true;
        for (final BranchToPush b : branchesToPush) {
          boolean branchResult = true;
          if (b.getBranchName() == null) {
            listener.getLogger().println("No branch to push defined");
            return false;
          }
          if (b.getTargetRepoName() == null) {
            listener.getLogger().println("No branch repo to push to defined");
            return false;
          }
          final String branchName = environment.expand(b.getBranchName());
          final String targetRepo = environment.expand(b.getTargetRepoName());

          if (branchResult) {
            try {
              branchResult =
                  workingDirectory.act(
                      new FileCallable<Boolean>() {
                        private static final long serialVersionUID = 1L;

                        public Boolean invoke(File workspace, VirtualChannel channel)
                            throws IOException {

                          IGitAPI git =
                              new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                          RemoteConfig remote = gitSCM.getRepositoryByName(targetRepo);

                          if (remote == null) {
                            listener
                                .getLogger()
                                .println("No repository found for target repo name " + targetRepo);
                            return false;
                          }

                          listener
                              .getLogger()
                              .println(
                                  "Pushing HEAD to branch "
                                      + branchName
                                      + " at repo "
                                      + targetRepo);
                          git.push(remote, "HEAD:" + branchName);

                          return true;
                        }
                      });
            } catch (Throwable e) {
              e.printStackTrace(
                  listener.error("Failed to push branch " + branchName + " to " + targetRepo));
              build.setResult(Result.FAILURE);
              branchResult = false;
            }
          }

          if (!branchResult) {
            allBranchesResult = false;
          }
        }
        if (!allBranchesResult) {
          pushResult = false;
        }
      }

      if (isPushNotes()) {
        boolean allNotesResult = true;
        for (final NoteToPush b : notesToPush) {
          boolean noteResult = true;
          if (b.getnoteMsg() == null) {
            listener.getLogger().println("No note to push defined");
            return false;
          }

          b.setEmptyTargetRepoToOrigin();
          final String noteMsg = environment.expand(b.getnoteMsg());
          final String noteNamespace = environment.expand(b.getnoteNamespace());
          final String targetRepo = environment.expand(b.getTargetRepoName());
          final boolean noteReplace = b.getnoteReplace();

          if (noteResult) {
            try {
              noteResult =
                  workingDirectory.act(
                      new FileCallable<Boolean>() {
                        private static final long serialVersionUID = 1L;

                        public Boolean invoke(File workspace, VirtualChannel channel)
                            throws IOException {

                          IGitAPI git =
                              new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                          RemoteConfig remote = gitSCM.getRepositoryByName(targetRepo);

                          if (remote == null) {
                            listener
                                .getLogger()
                                .println("No repository found for target repo name " + targetRepo);
                            return false;
                          }

                          listener
                              .getLogger()
                              .println(
                                  "Adding note \""
                                      + noteMsg
                                      + "\" to namespace \""
                                      + noteNamespace
                                      + "\"");

                          if (noteReplace) git.addNote(noteMsg, noteNamespace);
                          else git.appendNote(noteMsg, noteNamespace);

                          git.push(remote, "refs/notes/*");

                          return true;
                        }
                      });
            } catch (Throwable e) {
              e.printStackTrace(
                  listener.error(
                      "Failed to add note \"" + noteMsg + "\" to \"" + noteNamespace + "\""));
              build.setResult(Result.FAILURE);
              noteResult = false;
            }
          }

          if (!noteResult) {
            allNotesResult = false;
          }
        }
        if (!allNotesResult) {
          pushResult = false;
        }
      }

      return pushResult;
    }
  }
コード例 #19
0
 @Override
 public boolean evaluate(Result threshold, Result actualResult) {
   return actualResult.equals(threshold);
 }
コード例 #20
0
 public boolean apply(AbstractBuild input) {
   return includeResult.equals(input.getResult());
 }
コード例 #21
0
 @Override
 public boolean evaluate(Result threshold, Result actualResult) {
   return actualResult.isWorseOrEqualTo(threshold);
 }
コード例 #22
0
  /** {@inheritDoc} */
  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws InterruptedException, IOException {
    Result threshold = onlyStable ? Result.SUCCESS : Result.UNSTABLE;
    if (build.getResult().isWorseThan(threshold)) {
      listener
          .getLogger()
          .println(
              "Skipping Cobertura coverage report as build was not "
                  + threshold.toString()
                  + " or better ...");
      return true;
    }

    listener.getLogger().println("[Cobertura] Publishing Cobertura coverage report...");
    final FilePath[] moduleRoots = build.getModuleRoots();
    final boolean multipleModuleRoots = moduleRoots != null && moduleRoots.length > 1;
    final FilePath moduleRoot = multipleModuleRoots ? build.getWorkspace() : build.getModuleRoot();
    final File buildCoberturaDir = build.getRootDir();
    FilePath buildTarget = new FilePath(buildCoberturaDir);

    FilePath[] reports = new FilePath[0];
    try {
      reports = moduleRoot.act(new ParseReportCallable(coberturaReportFile));

      // if the build has failed, then there's not
      // much point in reporting an error
      if (build.getResult().isWorseOrEqualTo(Result.FAILURE) && reports.length == 0) {
        return true;
      }

    } catch (IOException e) {
      Util.displayIOException(e, listener);
      e.printStackTrace(listener.fatalError("Unable to find coverage results"));
      build.setResult(Result.FAILURE);
    }

    if (reports.length == 0) {
      String msg =
          "[Cobertura] No coverage results were found using the pattern '"
              + coberturaReportFile
              + "' relative to '"
              + moduleRoot.getRemote()
              + "'."
              + "  Did you enter a pattern relative to the correct directory?"
              + "  Did you generate the XML report(s) for Cobertura?";
      listener.getLogger().println(msg);
      if (failNoReports) {
        build.setResult(Result.FAILURE);
      } else {
        listener.getLogger().println("[Cobertura] Skipped cobertura reports.");
      }
      return true;
    }

    for (int i = 0; i < reports.length; i++) {
      final FilePath targetPath =
          new FilePath(buildTarget, "coverage" + (i == 0 ? "" : i) + ".xml");
      try {
        reports[i].copyTo(targetPath);
      } catch (IOException e) {
        Util.displayIOException(e, listener);
        e.printStackTrace(
            listener.fatalError(
                "Unable to copy coverage from " + reports[i] + " to " + buildTarget));
        build.setResult(Result.FAILURE);
      }
    }

    listener.getLogger().println("Publishing Cobertura coverage results...");
    Set<String> sourcePaths = new HashSet<String>();
    CoverageResult result = null;
    for (File coberturaXmlReport : getCoberturaReports(build)) {
      try {
        result = CoberturaCoverageParser.parse(coberturaXmlReport, result, sourcePaths);
      } catch (IOException e) {
        Util.displayIOException(e, listener);
        e.printStackTrace(listener.fatalError("Unable to parse " + coberturaXmlReport));
        build.setResult(Result.FAILURE);
      }
    }
    if (result != null) {
      listener.getLogger().println("Cobertura coverage report found.");
      result.setOwner(build);
      final FilePath paintedSourcesPath =
          new FilePath(new File(build.getProject().getRootDir(), "cobertura"));
      paintedSourcesPath.mkdirs();

      if (sourcePaths.contains(".")) {
        sourcePaths.remove(".");
        for (FilePath f : reports) {
          FilePath p = f.getParent();
          if (p != null && p.isDirectory()) {
            sourcePaths.add(p.getRemote());
          }
        }
      }

      SourceCodePainter painter =
          new SourceCodePainter(
              paintedSourcesPath,
              sourcePaths,
              result.getPaintedSources(),
              listener,
              getSourceEncoding());

      moduleRoot.act(painter);

      final CoberturaBuildAction action =
          CoberturaBuildAction.load(
              build,
              result,
              healthyTarget,
              unhealthyTarget,
              getOnlyStable(),
              getFailUnhealthy(),
              getFailUnstable(),
              getAutoUpdateHealth(),
              getAutoUpdateStability());

      build.getActions().add(action);
      Set<CoverageMetric> failingMetrics = failingTarget.getFailingMetrics(result);
      if (!failingMetrics.isEmpty()) {
        listener.getLogger().println("Code coverage enforcement failed for the following metrics:");
        float oldStabilityPercent;
        float setStabilityPercent;
        for (CoverageMetric metric : failingMetrics) {
          oldStabilityPercent = failingTarget.getObservedPercent(result, metric);
          setStabilityPercent = failingTarget.getSetPercent(result, metric);
          listener
              .getLogger()
              .println(
                  "    "
                      + metric.getName()
                      + "'s stability is "
                      + roundDecimalFloat(oldStabilityPercent * 100f)
                      + " and set mininum stability is "
                      + roundDecimalFloat(setStabilityPercent * 100f)
                      + ".");
        }
        if (!getFailUnstable()) {
          listener.getLogger().println("Setting Build to unstable.");
          build.setResult(Result.UNSTABLE);
        } else {
          listener.getLogger().println("Failing build due to unstability.");
          build.setResult(Result.FAILURE);
        }
      }
      if (getFailUnhealthy()) {
        Set<CoverageMetric> unhealthyMetrics = unhealthyTarget.getFailingMetrics(result);
        if (!unhealthyMetrics.isEmpty()) {
          listener.getLogger().println("Unhealthy for the following metrics:");
          float oldHealthyPercent;
          float setHealthyPercent;
          for (CoverageMetric metric : unhealthyMetrics) {
            oldHealthyPercent = unhealthyTarget.getObservedPercent(result, metric);
            setHealthyPercent = unhealthyTarget.getSetPercent(result, metric);
            listener
                .getLogger()
                .println(
                    "    "
                        + metric.getName()
                        + "'s health is "
                        + roundDecimalFloat(oldHealthyPercent * 100f)
                        + " and set minimum health is "
                        + roundDecimalFloat(setHealthyPercent * 100f)
                        + ".");
          }
          listener.getLogger().println("Failing build because it is unhealthy.");
          build.setResult(Result.FAILURE);
        }
      }
      if (build.getResult() == Result.SUCCESS) {
        if (getAutoUpdateHealth()) {
          setNewPercentages(result, true, listener);
        }

        if (getAutoUpdateStability()) {
          setNewPercentages(result, false, listener);
        }
      }
    } else {
      listener
          .getLogger()
          .println(
              "No coverage results were successfully parsed.  Did you generate "
                  + "the XML report(s) for Cobertura?");
      build.setResult(Result.FAILURE);
    }

    return true;
  }