Java Code Examples for hudson.model.AbstractBuild#getEnvironment()

The following examples show how to use hudson.model.AbstractBuild#getEnvironment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RepairnatorPostBuild.java    From repairnator with MIT License 5 votes vote down vote up
public void printAllEnv(AbstractBuild build,BuildListener listener) throws IOException,InterruptedException{
    System.out.println("-----------Printing Env----------");
    final EnvVars env = build.getEnvironment(listener);
    for(String key : env.keySet()) {
        System.out.println(key + ":" + env.get(key));
    }
    System.out.println("---------------------------------");
}
 
Example 2
Source File: RepairnatorPostBuild.java    From repairnator with MIT License 5 votes vote down vote up
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
    System.setOut(listener.getLogger());
    System.setErr(listener.getLogger());
    try {
        EnvVars env = build.getEnvironment(listener);
        String branch = this.decideGitBranch(env);
        String url = this.decideGitUrl(env);

        if (!this.isCheckPassed(branch,url,env)){
            return false;
        }

        this.configure(url,branch,env);

        System.out.println("The following tools will be used : " + Arrays.toString(this.config.getTools()));
        System.out.println("workspace for repairnator: " + this.config.getTempDir().getAbsolutePath());

        String snapShotUrl = "https://repo.jenkins-ci.org/snapshots/fr/inria/repairnator/repairnator-pipeline";

        File jar = new File(this.getConfig().getJarLocation());
        if (this.shouldDownloadJar()) {
            RepairnatorJarDownloader repJarDownloader = new RepairnatorJarDownloader(snapShotUrl,this.getConfig().getJarLocation());
            repJarDownloader.downloadJarHardCoded("https://github.com/henry-lp/mvn-repo/raw/master/repairnator-pipeline-3.3-SNAPSHOT-jar-with-dependencies.jar");
        }

        if (this.shouldInstallMaven(env)) {
            System.out.println("M2_HOME is null, proceed installing default maven version 3.6.3");
            MavenCustomInstaller mvn = new MavenCustomInstaller(build,listener,config.getMavenHome());
            mvn.install();
        }

        this.runRepairnator(env);
        this.cleanUp();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return true;
}
 
Example 3
Source File: MattermostNotifier.java    From jenkins-mattermost-plugin with MIT License 5 votes vote down vote up
public MattermostService newMattermostService(AbstractBuild r, BuildListener listener) {
 String endpoint = this.getEndpoint().getPlainText();
  if (StringUtils.isEmpty(endpoint)) {
endpoint = getDescriptor().getEndpoint().getPlainText();
  }
  String room = this.room;
  if (StringUtils.isEmpty(room)) {
    room = getDescriptor().getRoom();
  }

  String icon = this.icon;
  if (StringUtils.isEmpty(icon)) {
    icon = getDescriptor().getIcon();
  }

  EnvVars env = null;
  try {
    env = r.getEnvironment(listener);
  } catch (Exception e) {
    listener.getLogger().println("Error retrieving environment vars: " + e.getMessage());
    env = new EnvVars();
  }
  endpoint = env.expand(endpoint);
  room = env.expand(room);
  icon = env.expand(icon);

  return new StandardMattermostService(endpoint, room, icon);
}
 
Example 4
Source File: SampleDockerBuilder.java    From docker-commons-plugin with MIT License 5 votes vote down vote up
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    EnvVars env = build.getEnvironment(listener);
    // prepare the credentials to talk to this docker and make it available for docker you'll be forking
    String dockerExecutable = DockerTool.getExecutable(toolName, build.getBuiltOn(), listener, env);
    KeyMaterialFactory keyMaterialFactory = server.newKeyMaterialFactory(build).plus(registry.newKeyMaterialFactory(build.getParent(), build.getWorkspace(), launcher, env, listener, dockerExecutable));
    try (KeyMaterial key = keyMaterialFactory.materialize()) {
        // fork docker with appropriate environment to interact with this docker daemon
        return launcher.launch().cmds(dockerExecutable, "info").envs(key.env()).join() == 0;
    }
}
 
Example 5
Source File: LambdaInvokePublisher.java    From aws-lambda-jenkins-plugin with MIT License 5 votes vote down vote up
public boolean perform(LambdaInvokeVariables lambdaInvokeVariables,AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
    if (lambdaInvokeVariables.getSuccessOnly() && build.getResult().isWorseThan(Result.SUCCESS)) {
        listener.getLogger().println("Build not successful, not invoking Lambda function: " + lambdaInvokeVariables.getFunctionName());
        return true;
    } else if (!lambdaInvokeVariables.getSuccessOnly() && build.getResult().isWorseThan(Result.UNSTABLE)) {
        listener.getLogger().println("Build failed, not invoking Lambda function: " + lambdaInvokeVariables.getFunctionName());
        return true;
    }
    try {
        LambdaInvokeVariables executionVariables = lambdaInvokeVariables.getClone();
        executionVariables.expandVariables(build.getEnvironment(listener));
        JenkinsLogger logger = new JenkinsLogger(listener.getLogger());
        LambdaClientConfig clientConfig = executionVariables.getLambdaClientConfig();
        InvokeConfig invokeConfig = executionVariables.getInvokeConfig();

        InvokeCallable invokeCallable = new InvokeCallable(listener, invokeConfig, clientConfig);

        LambdaInvocationResult invocationResult = launcher.getChannel().call(invokeCallable);

        if(!invocationResult.isSuccess()){
            build.setResult(Result.FAILURE);
        }

        for (Map.Entry<String,String> entry : invocationResult.getInjectables().entrySet()) {
            build.addAction(new LambdaOutputInjectionAction(entry.getKey(), entry.getValue()));
        }
        build.getEnvironment(listener);
        build.addAction(new LambdaInvokeAction(executionVariables.getFunctionName(), invocationResult.isSuccess()));
        return true;
    } catch (Exception exc) {
        throw new RuntimeException(exc);
    }
}
 
Example 6
Source File: BaseStep.java    From jenkins-client-plugin with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> consolidateEnvVars(TaskListener listener,
        AbstractBuild<?, ?> build,
        Launcher launcher) {
    // EnvVars extends TreeMap
    TreeMap<String, String> overrides = new TreeMap<String, String>();
    // merge from all potential sources
    if (build != null) {
        try {
            EnvVars buildEnv = build.getEnvironment(listener);
            if (isVerbose())
                listener.getLogger()
                        .println("build env vars:  " + buildEnv);
            overrides.putAll(buildEnv);
        } catch (IOException | InterruptedException e) {
            if (isVerbose())
                e.printStackTrace(listener.getLogger());
        }
    }

    try {
        EnvVars computerEnv = null;
        Computer computer = Computer.currentComputer();
        if (computer != null) {
            computerEnv = computer.getEnvironment();
        } else {
            if (launcher != null)
                computer = launcher.getComputer();
            if (computer != null) {
                computerEnv = computer.getEnvironment();
            }
        }
        if (isVerbose())
            listener.getLogger().println(
                    "computer env vars:  " + computerEnv);
        if (computerEnv != null)
            overrides.putAll(computerEnv);
    } catch (IOException | InterruptedException e2) {
        if (isVerbose())
            e2.printStackTrace(listener.getLogger());
    }

    return overrides;
}
 
Example 7
Source File: BaseStep.java    From jenkins-client-plugin with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> consolidateEnvVars(TaskListener listener,
        AbstractBuild<?, ?> build,
        Launcher launcher) {
    // EnvVars extends TreeMap
    TreeMap<String, String> overrides = new TreeMap<String, String>();
    // merge from all potential sources
    if (build != null) {
        try {
            EnvVars buildEnv = build.getEnvironment(listener);
            if (isVerbose())
                listener.getLogger()
                        .println("build env vars:  " + buildEnv);
            overrides.putAll(buildEnv);
        } catch (IOException | InterruptedException e) {
            if (isVerbose())
                e.printStackTrace(listener.getLogger());
        }
    }

    try {
        EnvVars computerEnv = null;
        Computer computer = Computer.currentComputer();
        if (computer != null) {
            computerEnv = computer.getEnvironment();
        } else {
            if (launcher != null)
                computer = launcher.getComputer();
            if (computer != null) {
                computerEnv = computer.getEnvironment();
            }
        }
        if (isVerbose())
            listener.getLogger().println(
                    "computer env vars:  " + computerEnv);
        if (computerEnv != null)
            overrides.putAll(computerEnv);
    } catch (IOException | InterruptedException e2) {
        if (isVerbose())
            e2.printStackTrace(listener.getLogger());
    }

    return overrides;
}
 
Example 8
Source File: CaptureEnvironmentBuilder.java    From jenkins-test-harness with MIT License 4 votes vote down vote up
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
	envVars = build.getEnvironment(listener);
    return true;
}
 
Example 9
Source File: ZAProxy.java    From zaproxy-plugin with MIT License 4 votes vote down vote up
/**
 * Start ZAProxy using command line. It uses host and port configured in Jenkins admin mode and
 * ZAProxy program is launched in daemon mode (i.e without UI).
 * ZAProxy is started on the build's machine (so master machine ou slave machine) thanks to 
 * {@link FilePath} object and {@link Launcher} object.
 * 
 * @param build
 * @param listener the listener to display log during the job execution in jenkins
 * @param launcher the object to launch a process locally or remotely
 * @throws InterruptedException 
 * @throws IOException 
 * @throws IllegalArgumentException 
 */
public void startZAP(AbstractBuild<?, ?> build, BuildListener listener, Launcher launcher) 
		throws IllegalArgumentException, IOException, InterruptedException {
	checkParams(build, listener);
	
	FilePath ws = build.getWorkspace();
	if (ws == null) {
		Node node = build.getBuiltOn();
		if (node == null) {
			throw new NullPointerException("no such build node: " + build.getBuiltOnStr());
		}
		throw new NullPointerException("no workspace from node " + node + " which is computer " + node.toComputer() + " and has channel " + node.getChannel());
	}
	
	// Contains the absolute path to ZAP program
	FilePath zapPathWithProgName = new FilePath(ws.getChannel(), zapProgram + getZAPProgramNameWithSeparator(build));
	listener.getLogger().println("Start ZAProxy [" + zapPathWithProgName.getRemote() + "]");
	
	// Command to start ZAProxy with parameters
	List<String> cmd = new ArrayList<String>();
	cmd.add(zapPathWithProgName.getRemote());
	cmd.add(CMD_LINE_DAEMON);
	cmd.add(CMD_LINE_HOST);
	cmd.add(zapProxyHost);
	cmd.add(CMD_LINE_PORT);
	cmd.add(String.valueOf(zapProxyPort));
	cmd.add(CMD_LINE_CONFIG);
	cmd.add(CMD_LINE_API_KEY + "=" + API_KEY);
	
	// Set the default directory used by ZAP if it's defined and if a scan is provided
	if(scanURL && zapDefaultDir != null && !zapDefaultDir.isEmpty()) {
		cmd.add(CMD_LINE_DIR);
		cmd.add(zapDefaultDir);
	}
	
	// Adds command line arguments if it's provided
	if(!cmdLinesZAP.isEmpty()) {
		addZapCmdLine(cmd);
	}
		
	EnvVars envVars = build.getEnvironment(listener);
	// on Windows environment variables are converted to all upper case,
	// but no such conversions are done on Unix, so to make this cross-platform,
	// convert variables to all upper cases.
	for(Map.Entry<String,String> e : build.getBuildVariables().entrySet())
		envVars.put(e.getKey(),e.getValue());
	
	FilePath workDir = new FilePath(ws.getChannel(), zapProgram);
	
	// JDK choice
	computeJdkToUse(build, listener, envVars);
	
	// Launch ZAP process on remote machine (on master if no remote machine)
	launcher.launch().cmds(cmd).envs(envVars).stdout(listener).pwd(workDir).start();
	
	// Call waitForSuccessfulConnectionToZap(int, BuildListener) remotely
	build.getWorkspace().act(new WaitZAProxyInitCallable(this, listener));
}