Java Code Examples for jenkins.model.Jenkins#getGlobalNodeProperties()

The following examples show how to use jenkins.model.Jenkins#getGlobalNodeProperties() . 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 6 votes vote down vote up
public void createGlobalEnvironmentVariables(String key, String value){

        Jenkins instance = Jenkins.getInstance();

        DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
        List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);

        EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
        EnvVars envVars = null;

        if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
            newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
            globalNodeProperties.add(newEnvVarsNodeProperty);
            envVars = newEnvVarsNodeProperty.getEnvVars();
        } else {
            envVars = envVarsNodePropertyList.get(0).getEnvVars();
        }
        envVars.put(key, value);
        try {
            instance.save();
        } catch(Exception e) {
            System.out.println("Failed to create env variable");
        }
    }
 
Example 2
Source File: GlobalNodePropertiesTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
public void configure() throws Exception {
    final Jenkins jenkins = Jenkins.get();

    DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties = jenkins.getGlobalNodeProperties();

    Set<Map.Entry<String, String>> entries = ((EnvironmentVariablesNodeProperty) nodeProperties.get(0)).getEnvVars().entrySet();
    assertEquals(1, entries.size());

    Map.Entry<String, String> envVar = entries.iterator().next();
    assertEquals("FOO", envVar.getKey());
    assertEquals("BAR", envVar.getValue());
}
 
Example 3
Source File: ContainerStepExecution.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean start() throws Exception {
    LOGGER.log(Level.FINE, "Starting container step.");
    String containerName = step.getName();
    String shell = step.getShell();

    KubernetesNodeContext nodeContext = new KubernetesNodeContext(getContext());

    EnvironmentExpander env = getContext().get(EnvironmentExpander.class);
    EnvVars globalVars = null;
    Jenkins instance = Jenkins.getInstance();

    DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
    List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties
            .getAll(EnvironmentVariablesNodeProperty.class);
    if (envVarsNodePropertyList != null && envVarsNodePropertyList.size() != 0) {
        globalVars = envVarsNodePropertyList.get(0).getEnvVars();
    }

    EnvVars rcEnvVars = null;
    Run run = getContext().get(Run.class);
    TaskListener taskListener = getContext().get(TaskListener.class);
    if(run!=null && taskListener != null) {
        rcEnvVars = run.getEnvironment(taskListener);
    }

    decorator = new ContainerExecDecorator();
    decorator.setNodeContext(nodeContext);
    decorator.setContainerName(containerName);
    decorator.setEnvironmentExpander(env);
    decorator.setWs(getContext().get(FilePath.class));
    decorator.setGlobalVars(globalVars);
    decorator.setRunContextEnvVars(rcEnvVars);
    decorator.setShell(shell);
    getContext().newBodyInvoker()
            .withContext(BodyInvoker
                    .mergeLauncherDecorators(getContext().get(LauncherDecorator.class), decorator))
            .withCallback(new ContainerExecCallback(decorator))
            .start();
    return false;
}