Search in sources :

Example 46 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class NodeJSInstaller method refreshGlobalPackages.

/*
     * Installing npm packages if needed
     */
protected void refreshGlobalPackages(Node node, TaskListener log, FilePath expected) throws IOException, InterruptedException {
    String globalPackages = getNpmPackages();
    if (StringUtils.isNotBlank(globalPackages)) {
        // JENKINS-41876
        boolean skipNpmPackageInstallation = areNpmPackagesUpToDate(expected, globalPackages, getNpmPackagesRefreshHours());
        if (!skipNpmPackageInstallation) {
            expected.child(NPM_PACKAGES_RECORD_FILENAME).delete();
            ArgumentListBuilder npmScriptArgs = new ArgumentListBuilder();
            if (platform == Platform.WINDOWS) {
                npmScriptArgs.add("cmd");
                npmScriptArgs.add("/c");
            }
            FilePath binFolder = expected.child(platform.binFolder);
            FilePath npmExe = binFolder.child(platform.npmFileName);
            npmScriptArgs.add(npmExe);
            npmScriptArgs.add("install");
            npmScriptArgs.add("-g");
            for (String packageName : globalPackages.split("\\s")) {
                npmScriptArgs.add(packageName);
            }
            EnvVars env = new EnvVars();
            env.put(NodeJSConstants.ENVVAR_NODEJS_PATH, binFolder.getRemote());
            try {
                buildProxyEnvVars(env, log);
            } catch (URISyntaxException e) {
                log.error("Wrong proxy URL: " + e.getMessage());
            }
            hudson.Launcher launcher = node.createLauncher(log);
            int returnCode = launcher.launch().envs(env).cmds(npmScriptArgs).stdout(log).join();
            if (returnCode == 0) {
                // leave a record for the next up-to-date check
                expected.child(NPM_PACKAGES_RECORD_FILENAME).write(globalPackages, "UTF-8");
                expected.child(NPM_PACKAGES_RECORD_FILENAME).act(new ChmodRecAPlusX());
            }
        }
    }
}
Also used : FilePath(hudson.FilePath) EnvVars(hudson.EnvVars) Launcher(hudson.Launcher) ArgumentListBuilder(hudson.util.ArgumentListBuilder) URISyntaxException(java.net.URISyntaxException)

Example 47 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class NodeJSBuildWrapper method setUp.

/*
     * (non-Javadoc)
     * @see jenkins.tasks.SimpleBuildWrapper#setUp(jenkins.tasks.SimpleBuildWrapper.Context, hudson.model.Run, hudson.FilePath, hudson.Launcher, hudson.model.TaskListener, hudson.EnvVars)
     */
@Override
public void setUp(final Context context, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
    // get specific installation for the node
    NodeJSInstallation ni = getNodeJS();
    if (ni == null) {
        throw new IOException(Messages.NodeJSBuilders_noInstallationFound(nodeJSInstallationName));
    }
    Computer computer = workspace.toComputer();
    if (computer == null) {
        throw new AbortException(Messages.NodeJSBuilders_nodeOffline());
    }
    Node node = computer.getNode();
    if (node == null) {
        throw new AbortException(Messages.NodeJSBuilders_nodeOffline());
    }
    ni = ni.forNode(node, listener);
    ni = ni.forEnvironment(initialEnvironment);
    String exec = ni.getExecutable(launcher);
    if (exec == null) {
        throw new AbortException(Messages.NodeJSBuilders_noExecutableFound(ni.getHome()));
    }
    ni.buildEnvVars(new EnvVarsAdapter(context));
    EnvVars env = initialEnvironment.overrideAll(context.getEnv());
    // add npmrc config
    if (configId != null) {
        ConfigFile cf = new ConfigFile(configId, null, true);
        FilePath configFile = ConfigFileManager.provisionConfigFile(cf, env, build, workspace, listener, new ArrayList<String>());
        context.env(NodeJSConstants.NPM_USERCONFIG, configFile.getRemote());
        build.addAction(new CleanTempFilesAction(configFile.getRemote()));
    }
}
Also used : NodeJSInstallation(jenkins.plugins.nodejs.tools.NodeJSInstallation) FilePath(hudson.FilePath) EnvVars(hudson.EnvVars) ConfigFile(org.jenkinsci.lib.configprovider.model.ConfigFile) CleanTempFilesAction(org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction) Node(hudson.model.Node) Computer(hudson.model.Computer) IOException(java.io.IOException) AbortException(hudson.AbortException)

Example 48 with EnvVars

use of hudson.EnvVars in project jenkins-client-plugin by openshift.

the class BaseStep method consolidateEnvVars.

// borrowed from openshift pipeline plugin
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;
}
Also used : EnvVars(hudson.EnvVars) Computer(hudson.model.Computer) IOException(java.io.IOException) TreeMap(java.util.TreeMap)

Example 49 with EnvVars

use of hudson.EnvVars in project configuration-as-code-plugin by jenkinsci.

the class JenkinsConfigurator method describe.

@Override
public Set<Attribute> describe() {
    final Set<Attribute> attributes = super.describe();
    final Jenkins jenkins = Jenkins.getInstance();
    attributes.add(new PersistedListAttribute<Cloud>("clouds", jenkins.clouds, Cloud.class));
    attributes.add(new Attribute<String>("jobs", String.class).multiple(true).setter((target, attribute, value) -> {
        JenkinsJobManagement mng = new JenkinsJobManagement(System.out, new EnvVars(), null, null, LookupStrategy.JENKINS_ROOT);
        for (String script : (List<String>) value) {
            new JenkinsDslScriptLoader(mng).runScript(script);
        }
    }));
    // Check for unclassified Descriptors
    final ExtensionList<Descriptor> descriptors = jenkins.getExtensionList(Descriptor.class);
    for (Descriptor descriptor : descriptors) {
        if (descriptor.getGlobalConfigPage() != null && descriptor.getCategory() instanceof GlobalConfigurationCategory.Unclassified) {
            final DescriptorConfigurator configurator = new DescriptorConfigurator(descriptor);
            attributes.add(new Attribute(configurator.getName(), configurator.getTarget()).setter(NOOP));
        }
    }
    // Add remoting security, all legwork will be done by a configurator
    attributes.add(new Attribute("remotingSecurity", AdminWhitelistRule.class).setter(NOOP));
    return attributes;
}
Also used : JenkinsDslScriptLoader(javaposse.jobdsl.plugin.JenkinsDslScriptLoader) JenkinsJobManagement(javaposse.jobdsl.plugin.JenkinsJobManagement) Descriptor(hudson.model.Descriptor) Jenkins(jenkins.model.Jenkins) AdminWhitelistRule(jenkins.security.s2m.AdminWhitelistRule) Set(java.util.Set) Logger(java.util.logging.Logger) Level(java.util.logging.Level) List(java.util.List) Map(java.util.Map) Extension(hudson.Extension) LookupStrategy(javaposse.jobdsl.plugin.LookupStrategy) EnvVars(hudson.EnvVars) Cloud(hudson.slaves.Cloud) ExtensionList(hudson.ExtensionList) GlobalConfigurationCategory(jenkins.model.GlobalConfigurationCategory) JenkinsJobManagement(javaposse.jobdsl.plugin.JenkinsJobManagement) JenkinsDslScriptLoader(javaposse.jobdsl.plugin.JenkinsDslScriptLoader) Jenkins(jenkins.model.Jenkins) EnvVars(hudson.EnvVars) Cloud(hudson.slaves.Cloud) Descriptor(hudson.model.Descriptor) GlobalConfigurationCategory(jenkins.model.GlobalConfigurationCategory)

Example 50 with EnvVars

use of hudson.EnvVars in project htmlpublisher-plugin by jenkinsci.

the class HtmlPublisherIntegrationTest method addEnvironmentVariable.

private void addEnvironmentVariable(String key, String value) {
    EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
    EnvVars envVars = prop.getEnvVars();
    envVars.put(key, value);
    j.jenkins.getGlobalNodeProperties().add(prop);
}
Also used : EnvVars(hudson.EnvVars) EnvironmentVariablesNodeProperty(hudson.slaves.EnvironmentVariablesNodeProperty)

Aggregations

EnvVars (hudson.EnvVars)72 Test (org.junit.Test)25 IOException (java.io.IOException)17 FilePath (hudson.FilePath)11 TaskListener (hudson.model.TaskListener)9 AbortException (hudson.AbortException)8 ClientConfiguration (com.amazonaws.ClientConfiguration)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Computer (hudson.model.Computer)5 FreeStyleProject (hudson.model.FreeStyleProject)5 ArgumentListBuilder (hudson.util.ArgumentListBuilder)5 ParameterValue (hudson.model.ParameterValue)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Step (org.jenkinsci.plugins.workflow.steps.Step)4 ConduitAPIClient (com.uber.jenkins.phabricator.conduit.ConduitAPIClient)3 ConduitAPIException (com.uber.jenkins.phabricator.conduit.ConduitAPIException)3 Differential (com.uber.jenkins.phabricator.conduit.Differential)3 DifferentialClient (com.uber.jenkins.phabricator.conduit.DifferentialClient)3