void cancelBuilds(PullRequestData pullRequestData) {
   final String pullRequestUrl = pullRequestData.pullRequestUrl.toString();
   List<AbstractBuild> pullRequestBuilds = new ArrayList<AbstractBuild>();
   for (Object b : project.getBuilds()) {
     if (b instanceof AbstractBuild) {
       AbstractBuild build = (AbstractBuild) b;
       if (build.isBuilding() && isPullRequestBuild(build, pullRequestUrl)) {
         pullRequestBuilds.add(build);
       }
     }
   }
   if (!pullRequestBuilds.isEmpty()) {
     String[] buildNumbers = new String[pullRequestBuilds.size()];
     for (int i = 0; i < pullRequestBuilds.size(); i++) {
       buildNumbers[i] = String.valueOf(pullRequestBuilds.get(i).getNumber());
     }
     LOGGER.info(
         MessageFormat.format(
             "Aborting the following builds of pull request {0}: {1}",
             pullRequestUrl, StringUtils.join(buildNumbers, ", ")));
     for (AbstractBuild build : pullRequestBuilds) {
       Executor executor = build.getExecutor();
       if (executor != null) {
         executor.interrupt();
       }
     }
   } else {
     LOGGER.fine(
         "There is no previous running builds to cancel for Pull Request: " + pullRequestUrl);
   }
 }
 @Override
 public FilePath locate(AbstractMavenBuild build) {
   final Node builtOn = build.getBuiltOn();
   final FilePath rootPath = builtOn != null ? builtOn.getRootPath() : null;
   final Executor executor = Executor.currentExecutor();
   if (rootPath == null || executor == null) {
     return null;
   }
   return rootPath.child("maven-repositories/" + executor.getNumber());
 }
Ejemplo n.º 3
0
 @Override
 public boolean cancel(boolean mayInterruptIfRunning) {
   Queue q = Jenkins.getInstance().getQueue();
   synchronized (q) {
     synchronized (this) {
       if (!executors.isEmpty()) {
         if (mayInterruptIfRunning) for (Executor e : executors) e.interrupt();
         return mayInterruptIfRunning;
       }
       return q.cancel(task);
     }
   }
 }
  @SuppressWarnings("unchecked")
  public Map<String, String> getBuildVariables(AbstractBuild build, EnvInjectLogger logger)
      throws EnvInjectException {
    Map<String, String> result = new HashMap<String, String>();

    // Add build process variables
    result.putAll(build.getCharacteristicEnvVars());

    try {
      //            EnvVars envVars = new EnvVars();
      //            for (EnvironmentContributor ec : EnvironmentContributor.all()) {
      //                ec.buildEnvironmentFor(build, envVars, new LogTaskListener(LOG, Level.ALL));
      //                result.putAll(envVars);
      //            }

      JDK jdk = build.getProject().getJDK();
      if (jdk != null) {
        Node node = build.getBuiltOn();
        if (node != null) {
          jdk = jdk.forNode(node, logger.getListener());
        }
        jdk.buildEnvVars(result);
      }
    } catch (IOException ioe) {
      throw new EnvInjectException(ioe);
    } catch (InterruptedException ie) {
      throw new EnvInjectException(ie);
    }

    Executor e = build.getExecutor();
    if (e != null) {
      result.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber()));
    }

    String rootUrl = Hudson.getInstance().getRootUrl();
    if (rootUrl != null) {
      result.put("BUILD_URL", rootUrl + build.getUrl());
      result.put("JOB_URL", rootUrl + build.getParent().getUrl());
    }

    // Add build variables such as parameters, plugins contributions, ...
    result.putAll(build.getBuildVariables());

    // Retrieve triggered cause
    Map<String, String> triggerVariable = new BuildCauseRetriever().getTriggeredCause(build);
    result.putAll(triggerVariable);

    return result;
  }
Ejemplo n.º 5
0
 private Object writeReplace() {
   // when called from remote, methods need to be executed in the proper Executor's context.
   return Channel.current()
       .export(
           MavenBuildProxy2.class,
           Executor.currentExecutor().newImpersonatingProxy(MavenBuildProxy2.class, this));
 }
Ejemplo n.º 6
0
  /** Performs an installation. */
  private int install(ToolInstallation t, BuildIDs id, AbstractProject p)
      throws IOException, InterruptedException {

    Run b = p.getBuildByNumber(Integer.parseInt(id.number));
    if (b == null) throw new AbortException("No such build: " + id.number);

    Executor exec = b.getExecutor();
    if (exec == null) throw new AbortException(b.getFullDisplayName() + " is not building");

    Node node = exec.getOwner().getNode();
    if (node == null) {
      throw new AbortException(
          "The node " + exec.getOwner().getDisplayName() + " has been deleted");
    }

    t = t.translate(node, EnvVars.getRemote(checkChannel()), new StreamTaskListener(stderr));
    stdout.println(t.getHome());
    return 0;
  }
Ejemplo n.º 7
0
  /**
   * Assign a ${@link Queue.Executable} to this OneShotSlave. By design, only one Queue.Executable
   * can be assigned, then slave is shut down. This method has to be called just as the ${@link Run}
   * as been created. It run the actual launch of the executor and use Run's ${@link
   * hudson.model.BuildListener} as computer launcher listener to collect the startup log as part of
   * the build.
   *
   * <p>Delaying launch of the executor until the Run is actually started allows to fail the build
   * on launch failure, so we have a strong 1:1 relation between a Run and it's Executor.
   *
   * @param listener
   */
  synchronized void provision(TaskListener listener) {
    if (executable != null) {
      // already provisioned
      return;
    }

    final Executor executor = Executor.currentExecutor();
    if (executor == null) {
      throw new IllegalStateException("running task without associated executor thread");
    }

    try {
      realLauncher.launch(this.getComputer(), listener);

      if (getComputer().isActuallyOffline()) {
        provisionFailed(new IllegalStateException("Computer is offline after launch"));
      }
    } catch (Exception e) {
      provisionFailed(e);
    }
    executable = executor.getCurrentExecutable();
  }
        public void doRun() {
          // timed out
          long effectiveTimeoutMinutes = MINUTES.convert(effectiveTimeout, MILLISECONDS);
          String msg;
          if (failBuild) {
            msg = Messages.Timeout_Message(effectiveTimeoutMinutes, Messages.Timeout_Failed());
          } else {
            msg = Messages.Timeout_Message(effectiveTimeoutMinutes, Messages.Timeout_Aborted());
          }

          listener.getLogger().println(msg);
          if (writingDescription) {
            try {
              build.setDescription(msg);
            } catch (IOException e) {
              listener.getLogger().println("failed to write to the build description!");
            }
          }

          timeout = true;
          Executor e = build.getExecutor();
          if (e != null) e.interrupt(failBuild ? Result.FAILURE : Result.ABORTED);
        }