/** * Exposes the originalFileName as an environment variable. */ @Override public void buildEnvironment(Run<?,?> build, EnvVars env) { env.put(name,originalFileName); }
@Override public void buildEnvironmentFor(Run r, EnvVars env, TaskListener listener) throws IOException, InterruptedException { Computer c = Computer.currentComputer(); if (c!=null){ EnvVars compEnv = c.getEnvironment().overrideAll(env); env.putAll(compEnv); } env.put("BUILD_DISPLAY_NAME",r.getDisplayName()); Jenkins j = Jenkins.getInstance(); String rootUrl = j.getRootUrl(); if(rootUrl!=null) { env.put("BUILD_URL", rootUrl+r.getUrl()); } }
/** * Add a key/value but only if the value is not-null. Otherwise no-op. * @since 1.556 */ public void putIfNotNull(String key, String value) { if (value!=null) put(key,value); }
/** * Takes a string that looks like "a=b" and adds that to this map. */ public void addLine(String line) { int sep = line.indexOf('='); if(sep > 0) { put(line.substring(0,sep),line.substring(sep+1)); } }
/** * Builds an environment variables from an array of the form {@code "key","value","key","value"...} */ public EnvVars(String... keyValuePairs) { this(); if(keyValuePairs.length%2!=0) throw new IllegalArgumentException(Arrays.asList(keyValuePairs).toString()); for( int i=0; i<keyValuePairs.length; i+=2 ) put(keyValuePairs[i],keyValuePairs[i+1]); }
/** * Exposes the name/value as an environment variable. */ @Override public void buildEnvironment(Run<?,?> build, EnvVars env) { // TODO: check with Tom if this is really what he had in mind env.put(name,job.toString()); env.put(name.toUpperCase(Locale.ENGLISH),job.toString()); // backward compatibility pre 1.345 }
private static EnvVars toMap(List<Entry> entries) { EnvVars map = new EnvVars(); if (entries!=null) for (Entry entry: entries) map.put(entry.key,entry.value); return map; }
/** * Builds up the environment variable map that's sufficient to identify a process * as ours. This is used to kill run-away processes via {@link ProcessTree#killAll(Map)}. */ public EnvVars getCharacteristicEnvVars() { EnvVars env = new EnvVars(); env.put("JENKINS_SERVER_COOKIE",SERVER_COOKIE.get()); env.put("HUDSON_SERVER_COOKIE",SERVER_COOKIE.get()); // Legacy compatibility env.put("JOB_NAME",getFullName()); env.put("JOB_BASE_NAME", getName()); return env; }
/** * Builds up the environment variable map that's sufficient to identify a process * as ours. This is used to kill run-away processes via {@link ProcessTree#killAll(Map)}. */ public @Nonnull final EnvVars getCharacteristicEnvVars() { EnvVars env = getParent().getCharacteristicEnvVars(); env.put("BUILD_NUMBER",String.valueOf(number)); env.put("BUILD_ID",getId()); env.put("BUILD_TAG","jenkins-"+getParent().getFullName().replace('/', '-')+"-"+number); return env; }
/** * Build up the environment variables toward the Maven launch. */ protected void buildEnvVars(EnvVars env, MavenInstallation mi) throws IOException, InterruptedException { if(mi!=null) { // if somebody has use M2_HOME they will get a classloading error // when M2_HOME points to a different version of Maven2 from // MAVEN_HOME (as Maven 2 gives M2_HOME priority.) // // The other solution would be to set M2_HOME if we are calling Maven2 // and MAVEN_HOME for Maven1 (only of use for strange people that // are calling Maven2 from Maven1) mi.buildEnvVars(env); } // just as a precaution // see http://maven.apache.org/continuum/faqs.html#how-does-continuum-detect-a-successful-build env.put("MAVEN_TERMINATE_CMD","on"); String jvmOptions = env.expand(this.jvmOptions); if(jvmOptions!=null) env.put("MAVEN_OPTS",jvmOptions.replaceAll("[\t\r\n]+"," ")); }
/** * Expands the list of environment variables by inheriting current env variables. */ private static EnvVars inherit(@CheckForNull String[] env) { // convert String[] to Map first EnvVars m = new EnvVars(); if(env!=null) { for (String e : env) { int index = e.indexOf('='); m.put(e.substring(0,index), e.substring(index+1)); } } // then do the inheritance return inherit(m); }
/** * Overrides the current entry by the given entry. * * <p> * Handles {@code PATH+XYZ} notation. */ public void override(String key, String value) { if(value==null || value.length()==0) { remove(key); return; } int idx = key.indexOf('+'); if(idx>0) { String realKey = key.substring(0,idx); String v = get(realKey); if(v==null) v=value; else { // we might be handling environment variables for a agent that can have different path separator // than the master, so the following is an attempt to get it right. // it's still more error prone that I'd like. char ch = platform==null ? File.pathSeparatorChar : platform.pathSeparator; v=value+ch+v; } put(realKey,v); return; } put(key,value); }
/** * Exposes the name/value as an environment variable. */ @Override public void buildEnvironment(Run<?,?> build, EnvVars env) { Run run = getRun(); String value = (null == run) ? "UNKNOWN" : Jenkins.getInstance().getRootUrl() + run.getUrl(); env.put(name, value); env.put(name + ".jobName", getJobName()); // undocumented, left for backward compatibility env.put(name + "_JOBNAME", getJobName()); // prefer this version env.put(name + ".number" , getNumber ()); // same as above env.put(name + "_NUMBER" , getNumber ()); // if run is null, default to the standard '#1' display name format env.put(name + "_NAME", (null == run) ? "#" + getNumber() : run.getDisplayName()); // since 1.504 String buildResult = (null == run || null == run.getResult()) ? "UNKNOWN" : run.getResult().toString(); env.put(name + "_RESULT", buildResult); // since 1.517 env.put(name.toUpperCase(Locale.ENGLISH),value); // backward compatibility pre 1.345 }
@Override public void buildEnvironmentFor(Job j, EnvVars env, TaskListener listener) throws IOException, InterruptedException { Jenkins jenkins = Jenkins.getInstance(); String rootUrl = jenkins.getRootUrl(); if(rootUrl!=null) { env.put("JENKINS_URL", rootUrl); env.put("HUDSON_URL", rootUrl); // Legacy compatibility env.put("JOB_URL", rootUrl+j.getUrl()); } String root = jenkins.getRootDir().getPath(); env.put("JENKINS_HOME", root); env.put("HUDSON_HOME", root); // legacy compatibility Thread t = Thread.currentThread(); if (t instanceof Executor) { Executor e = (Executor) t; env.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber())); if (e.getOwner() instanceof MasterComputer) { env.put("NODE_NAME", "master"); } else { env.put("NODE_NAME", e.getOwner().getName()); } Node n = e.getOwner().getNode(); if (n != null) env.put("NODE_LABELS", Util.join(n.getAssignedLabels(), " ")); } } }
/** * Creates an environment variable override to be used for launching processes on this node. * * @see ProcStarter#envs(Map) * @since 1.489 */ public @Nonnull EnvVars buildEnvironment(@Nonnull TaskListener listener) throws IOException, InterruptedException { EnvVars env = new EnvVars(); Node node = getNode(); if (node==null) return env; // bail out for (NodeProperty nodeProperty: Jenkins.getInstance().getGlobalNodeProperties()) { nodeProperty.buildEnvVars(env,listener); } for (NodeProperty nodeProperty: node.getNodeProperties()) { nodeProperty.buildEnvVars(env,listener); } // TODO: hmm, they don't really belong String rootUrl = Jenkins.getInstance().getRootUrl(); if(rootUrl!=null) { env.put("HUDSON_URL", rootUrl); // Legacy. env.put("JENKINS_URL", rootUrl); } return env; }
@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()); project.getScm().buildEnvVars(this,env); if (buildEnvironments!=null) for (Environment e : buildEnvironments) e.buildEnvVars(env); for (EnvironmentContributingAction a : getActions(EnvironmentContributingAction.class)) a.buildEnvVars(this,env); EnvVars.resolve(env); return env; }