Exemple #1
0
  @Override
  public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
    EnvVars env = super.getEnvironment(log);
    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());
    // 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", "");

    JDK jdk = project.getJDK();
    if (jdk != null) {
      Computer computer = Computer.currentComputer();
      if (computer != null) { // just in case were not in a build
        jdk = jdk.forNode(computer.getNode(), log);
      }
      jdk.buildEnvVars(env);
    }
    project.getScm().buildEnvVars(this, env);

    if (buildEnvironments != null) for (Environment e : buildEnvironments) e.buildEnvVars(env);

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

    EnvVars.resolve(env);

    return env;
  }
  @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;
  }