/** * Overrides all values in the map by the given map. Expressions in values will be expanded. * See {@link #override(String, String)}. * @return {@code this} */ public EnvVars overrideExpandingAll(@Nonnull Map<String,String> all) { for (String key : new OverrideOrderCalculator(this, all).getOrderedVariableNames()) { override(key, expand(all.get(key))); } return this; }
/** * 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; }
/** * Expands the list of environment variables by inheriting current env variables. */ private static EnvVars inherit(@Nonnull Map<String,String> overrides) { EnvVars m = new EnvVars(EnvVars.masterEnvVars); m.overrideExpandingAll(overrides); return m; }
private void withRegion(@Nonnull EnvVars localEnv) throws IOException, InterruptedException { if (!StringUtils.isNullOrEmpty(this.step.getRegion())) { this.getContext().get(TaskListener.class).getLogger().format("Setting AWS region %s %n ", this.step.getRegion()); localEnv.override(AWSClientFactory.AWS_DEFAULT_REGION, this.step.getRegion()); localEnv.override(AWSClientFactory.AWS_REGION, this.step.getRegion()); this.envVars.overrideAll(localEnv); } }
public @Nonnull EnvVars getEnvironment(@CheckForNull Node node, @Nonnull TaskListener listener) throws IOException, InterruptedException { EnvVars env = new EnvVars(); final Computer computer = node.toComputer(); if (computer != null) { env.putAll(computer.buildEnvironment(listener)); env.putAll(getCharacteristicEnvVars()); env.put("CLASSPATH","");
/** * 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; }
public @Nonnull EnvVars getEnvironment(@Nonnull TaskListener listener) throws IOException, InterruptedException { Computer c = Computer.currentComputer(); Node n = c==null ? null : c.getNode(); EnvVars env = getParent().getEnvironment(n,listener); env.putAll(getCharacteristicEnvVars()); for (EnvironmentContributingAction a : getActions(EnvironmentContributingAction.class)) { a.buildEnvironment(this, env);
@Override public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) throws IOException, InterruptedException { GitLabWebHookCause cause = null; if (r instanceof MatrixRun) { MatrixBuild parent = ((MatrixRun)r).getParentBuild(); if (parent != null) { cause = (GitLabWebHookCause) parent.getCause(GitLabWebHookCause.class); } } else { cause = (GitLabWebHookCause) r.getCause(GitLabWebHookCause.class); } if (cause != null) { envs.overrideAll(cause.getData().getBuildVariables()); } } }
@Override public boolean permitsMethod(@Nonnull Method method, @Nonnull Object receiver, @Nonnull Object[] args) { //method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (SimpleTemplateScript2 BUILD_ID) if (method.getDeclaringClass() == GroovyObject.class && receiver instanceof EmailExtScript && "invokeMethod".equals(method.getName()) && args.length > 0) { EmailExtScript script = (EmailExtScript)receiver; String name = String.valueOf(args[0]); for (TokenMacro m : macros) { if (m.acceptsMacroName(name)) { return true; } } //Else check environment Run<?,?> build = (Run<?,?>)script.getBinding().getVariable("build"); TaskListener listener = (TaskListener)script.getBinding().getVariable("listener"); try { EnvVars vars = build.getEnvironment(listener); return vars.containsKey(name); } catch (IOException | InterruptedException e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Failed to expand environment when evaluating " + name + " on " + build.getExternalizableId(), e); } } return false; } }
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { listener.getLogger().println(Messages.JavadocArchiver_Publishing()); EnvVars env = build.getEnvironment(listener); FilePath javadoc = build.getWorkspace().child(env.expand(javadocDir)); FilePath target = new FilePath(keepAll ? getJavadocDir(build) : getJavadocDir(build.getProject())); try { if (javadoc.copyRecursiveTo("**/*",target)==0) { if(build.getResult().isBetterOrEqualTo(Result.UNSTABLE)) { // If the build failed, don't complain that there was no javadoc. // The build probably didn't even get to the point where it produces javadoc. listener.error(Messages.JavadocArchiver_NoMatchFound(javadoc,javadoc.validateAntFileMask("**/*"))); } build.setResult(Result.FAILURE); return true; } } catch (IOException e) { Util.displayIOException(e,listener); e.printStackTrace(listener.fatalError(Messages.JavadocArchiver_UnableToCopy(javadoc,target))); build.setResult(Result.FAILURE); return true; } // add build action, if javadoc is recorded for each build if(keepAll) build.addAction(new JavadocBuildAction(build)); return true; }
@Override public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) { if (StringUtils.isEmpty(path)) { return null; } try { EnvVars env = build.getEnvironment(listener); String targetPath = Util.replaceMacro(this.path, build.getBuildVariableResolver()); targetPath = env.expand(targetPath); if (IOUtils.isAbsolute(targetPath)) { return new FilePath(new File(targetPath)); } else { FilePath mrSettings = build.getModuleRoot().child(targetPath); FilePath wsSettings = build.getWorkspace().child(targetPath); try { if (!wsSettings.exists() && mrSettings.exists()) { wsSettings = mrSettings; } } catch (Exception e) { throw new IllegalStateException("failed to find settings.xml at: " + wsSettings.getRemote()); } return wsSettings; } } catch (Exception e) { throw new IllegalStateException("failed to prepare global settings.xml"); } }
@Override public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { listener.getLogger().println(Messages.JavadocArchiver_Publishing()); EnvVars env = build.getEnvironment(listener); FilePath javadoc = workspace.child(env.expand(javadocDir)); FilePath target = new FilePath(keepAll ? getJavadocDir(build) : getJavadocDir(build.getParent())); try { if (javadoc.copyRecursiveTo("**/*",target)==0) { if(build.getResult().isBetterOrEqualTo(Result.UNSTABLE)) { // If the build failed, don't complain that there was no javadoc. // The build probably didn't even get to the point where it produces javadoc. listener.error(Messages.JavadocArchiver_NoMatchFound(javadoc,javadoc.validateAntFileMask("**/*"))); } build.setResult(Result.FAILURE); return; } } catch (IOException e) { Util.displayIOException(e,listener); e.printStackTrace(listener.fatalError(Messages.JavadocArchiver_UnableToCopy(javadoc,target))); build.setResult(Result.FAILURE); return; } build.addAction(new JavadocBuildAction()); }
@Override public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { VariableResolver<String> vr = build.getBuildVariableResolver(); EnvVars env = build.getEnvironment(listener); String targets = Util.replaceMacro(this.targets,vr); targets = env.expand(targets); String pom = env.expand(this.pom); String exec = mi.getExecutable(launcher); if(exec==null) { listener.fatalError(Messages.Maven_NoExecutable(mi.getHome())); return false; args.add("-Dmaven.repo.local=" + build.getWorkspace().child(".repository")); args.addTokenized(normalizedTarget); wrapUpArguments(args,normalizedTarget,build,launcher,listener); MavenConsoleAnnotator mca = new MavenConsoleAnnotator(listener.getLogger(),build.getCharset()); int r = launcher.launch().cmds(args).envs(env).stdout(mca).pwd(build.getModuleRoot()).join(); if (0 != r) { Util.displayIOException(e,listener); Functions.printStackTrace(e, listener.fatalError(Messages.Maven_ExecFailed())); return false;
@Override public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException { try { listener.getLogger().println(Messages.Fingerprinter_Recording()); Map<String,String> record = new HashMap<String,String>(); EnvVars environment = build.getEnvironment(listener); if(targets.length()!=0) { String expandedTargets = environment.expand(targets); record(build, workspace, listener, record, expandedTargets); } FingerprintAction fingerprintAction = build.getAction(FingerprintAction.class); if (fingerprintAction != null) { fingerprintAction.add(record); } else { build.addAction(new FingerprintAction(build,record)); } if (enableFingerprintsInDependencyGraph) { Jenkins.getInstance().rebuildDependencyGraphAsync(); } } catch (IOException e) { Functions.printStackTrace(e, listener.error(Messages.Fingerprinter_Failed())); build.setResult(Result.FAILURE); } // failing to record fingerprints is an error but not fatal }
@Override public String prepareDockerImage(Docker docker, AbstractBuild build, TaskListener listener, boolean forcePull, boolean noCache) throws IOException, InterruptedException { String expandedContextPath = build.getEnvironment(listener).expand(contextPath); FilePath filePath = build.getWorkspace().child(expandedContextPath); FilePath dockerFile = filePath.child(getDockerfile()); if (!dockerFile.exists()) { listener.getLogger().println("Your project is missing a Dockerfile"); throw new InterruptedException("Your project is missing a Dockerfile"); } listener.getLogger().println("Build Docker image from " + expandedContextPath + "/"+getDockerfile()+" ..."); return docker.buildImage(filePath, dockerFile.getRemote(), forcePull, noCache); }
@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; }