示例#1
0
  /**
   * Saves the form to the configuration and disk.
   *
   * @param req StaplerRequest
   * @param rsp StaplerResponse
   * @throws ServletException if something unfortunate happens.
   * @throws IOException if something unfortunate happens.
   * @throws InterruptedException if something unfortunate happens.
   */
  public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp)
      throws ServletException, IOException, InterruptedException {
    getProject().checkPermission(AbstractProject.BUILD);
    if (isRebuildAvailable()) {
      if (!req.getMethod().equals("POST")) {
        // show the parameter entry form.
        req.getView(this, "index.jelly").forward(req, rsp);
        return;
      }
      build = req.findAncestorObject(AbstractBuild.class);
      ParametersDefinitionProperty paramDefProp =
          build.getProject().getProperty(ParametersDefinitionProperty.class);
      List<ParameterValue> values = new ArrayList<ParameterValue>();
      ParametersAction paramAction = build.getAction(ParametersAction.class);
      JSONObject formData = req.getSubmittedForm();
      if (!formData.isEmpty()) {
        JSONArray a = JSONArray.fromObject(formData.get("parameter"));
        for (Object o : a) {
          JSONObject jo = (JSONObject) o;
          String name = jo.getString("name");
          ParameterValue parameterValue =
              getParameterValue(paramDefProp, name, paramAction, req, jo);
          if (parameterValue != null) {
            values.add(parameterValue);
          }
        }
      }

      CauseAction cause = new CauseAction(new RebuildCause(build));
      Hudson.getInstance()
          .getQueue()
          .schedule(build.getProject(), 0, new ParametersAction(values), cause);
      rsp.sendRedirect("../../");
    }
  }
示例#2
0
 /**
  * Handles the rebuild request and redirects to parameterized and non parameterized build when
  * needed.
  *
  * @param request StaplerRequest the request.
  * @param response StaplerResponse the response handler.
  * @throws IOException in case of Stapler issues
  * @throws ServletException if something unfortunate happens.
  * @throws InterruptedException if something unfortunate happens.
  */
 public void doIndex(StaplerRequest request, StaplerResponse response)
     throws IOException, ServletException, InterruptedException {
   AbstractBuild currentBuild = request.findAncestorObject(AbstractBuild.class);
   if (currentBuild != null) {
     ParametersAction paramAction = currentBuild.getAction(ParametersAction.class);
     if (paramAction != null) {
       response.sendRedirect(PARAMETERIZED_URL);
     } else {
       nonParameterizedRebuild(currentBuild, response);
     }
   }
 }
示例#3
0
  /**
   * Call this method while rebuilding non parameterized build. .
   *
   * @param currentBuild current build.
   * @param response current response object.
   * @throws ServletException if something unfortunate happens.
   * @throws IOException if something unfortunate happens.
   * @throws InterruptedException if something unfortunate happens.
   */
  public void nonParameterizedRebuild(AbstractBuild currentBuild, StaplerResponse response)
      throws ServletException, IOException, InterruptedException {
    getProject().checkPermission(AbstractProject.BUILD);

    CauseAction cause = new CauseAction(new RebuildCause(currentBuild));
    Hudson.getInstance().getQueue().schedule(currentBuild.getProject(), 0, null, cause);
    response.sendRedirect("../../");
  }
示例#4
0
 /**
  * Method will return current project.
  *
  * @return currentProject.
  */
 public AbstractProject getProject() {
   if (build != null) {
     return build.getProject();
   }
   AbstractProject currentProject = null;
   StaplerRequest request = Stapler.getCurrentRequest();
   if (request != null) {
     currentProject = request.findAncestorObject(AbstractProject.class);
   }
   if (currentProject == null) {
     throw new NullPointerException("Current Project is null");
   }
   return currentProject;
 }
  @Override
  public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
    // This method deserves a refactor and cleanup.
    boolean success = true;
    Log log = new Log(listener);
    if (Result.FAILURE.equals(build.getResult())) {
      log.info("Not deploying due to job being in FAILED state.");
      return success;
    }

    logStartHeader(log);
    // todo: getting from descriptor is ugly. refactor?
    getDescriptorImpl().setGlobalConfiguration();
    OctopusApi api = getDescriptorImpl().api;

    VariableResolver resolver = build.getBuildVariableResolver();
    EnvVars envVars;
    try {
      envVars = build.getEnvironment(listener);
    } catch (Exception ex) {
      log.fatal(
          String.format(
              "Failed to retrieve environment variables for this build - '%s'", ex.getMessage()));
      return false;
    }
    EnvironmentVariableValueInjector envInjector =
        new EnvironmentVariableValueInjector(resolver, envVars);
    // NOTE: hiding the member variables of the same name with their env-injected equivalents
    String project = envInjector.injectEnvironmentVariableValues(this.project);
    String releaseVersion = envInjector.injectEnvironmentVariableValues(this.releaseVersion);
    String environment = envInjector.injectEnvironmentVariableValues(this.environment);
    String variables = envInjector.injectEnvironmentVariableValues(this.variables);

    com.octopusdeploy.api.Project p = null;
    try {
      p = api.getProjectByName(project);
    } catch (Exception ex) {
      log.fatal(
          String.format(
              "Retrieving project name '%s' failed with message '%s'", project, ex.getMessage()));
      success = false;
    }
    com.octopusdeploy.api.Environment env = null;
    try {
      env = api.getEnvironmentByName(environment);
    } catch (Exception ex) {
      log.fatal(
          String.format(
              "Retrieving environment name '%s' failed with message '%s'",
              environment, ex.getMessage()));
      success = false;
    }
    if (p == null) {
      log.fatal("Project was not found.");
      success = false;
    }
    if (env == null) {
      log.fatal("Environment was not found.");
      success = false;
    }
    if (!success) // Early exit
    {
      return success;
    }
    Set<com.octopusdeploy.api.Release> releases = null;
    try {
      releases = api.getReleasesForProject(p.getId());
    } catch (Exception ex) {
      log.fatal(
          String.format(
              "Retrieving releases for project '%s' failed with message '%s'",
              project, ex.getMessage()));
      success = false;
    }
    if (releases == null) {
      log.fatal("Releases was not found.");
      return false;
    }
    Release releaseToDeploy = null;
    for (Release r : releases) {
      if (releaseVersion.equals(r.getVersion())) {
        releaseToDeploy = r;
        break;
      }
    }
    if (releaseToDeploy == null) // early exit
    {
      log.fatal(
          String.format(
              "Unable to find release version %s for project %s", releaseVersion, project));
      return false;
    }
    Properties properties = new Properties();
    try {
      properties.load(new StringReader(variables));
    } catch (Exception ex) {
      log.fatal(
          String.format(
              "Unable to load entry variables failed with message '%s'", ex.getMessage()));
      success = false;
    }

    // TODO: Can we tell if we need to call? For now I will always try and get variable and use if I
    // find them
    Set<com.octopusdeploy.api.Variable> variablesForDeploy = null;

    try {
      String releaseId = releaseToDeploy.getId();
      String environmentId = env.getId();
      variablesForDeploy =
          api.getVariablesByReleaseAndEnvironment(releaseId, environmentId, properties);
    } catch (Exception ex) {
      log.fatal(
          String.format(
              "Retrieving variables for release '%s' to environment '%s' failed with message '%s'",
              releaseToDeploy.getId(), env.getName(), ex.getMessage()));
      success = false;
    }
    try {
      String results =
          api.executeDeployment(releaseToDeploy.getId(), env.getId(), variablesForDeploy);
      if (isTaskJson(results)) {
        JSON resultJson = JSONSerializer.toJSON(results);
        String urlSuffix = ((JSONObject) resultJson).getJSONObject("Links").getString("Web");
        String url = getDescriptorImpl().octopusHost;
        if (url.endsWith("/")) {
          url = url.substring(0, url.length() - 2);
        }
        log.info("Deployment executed: \n\t" + url + urlSuffix);
        build.addAction(
            new BuildInfoSummary(
                BuildInfoSummary.OctopusDeployEventType.Deployment, url + urlSuffix));
        if (waitForDeployment) {

          log.info("Waiting for deployment to complete.");
          String resultState = waitForDeploymentCompletion(resultJson, api, log);
          if (resultState == null) {
            log.info("Marking build failed due to failure in waiting for deployment to complete.");
            success = false;
          }

          if ("Failed".equals(resultState)) {
            log.info("Marking build failed due to deployment task status.");
            success = false;
          }
        }
      }
    } catch (IOException ex) {
      log.fatal("Failed to deploy: " + ex.getMessage());
      success = false;
    }

    return success;
  }