Search in sources :

Example 1 with ConfigFile

use of org.jenkinsci.lib.configprovider.model.ConfigFile in project nodejs-plugin by jenkinsci.

the class NpmrcFileSupplyTest method test_supply_npmrc_with_registry.

@Test
public void test_supply_npmrc_with_registry() throws Exception {
    StandardUsernameCredentials user = createUser("test-user-id", "myuser", "mypassword");
    NPMRegistry privateRegistry = new NPMRegistry("https://private.organization.com/", user.getId(), null);
    NPMRegistry officalRegistry = new NPMRegistry("https://registry.npmjs.org/", null, "@user1 user2");
    Config config = createSetting("mytest", "email=guest@example.com", Arrays.asList(privateRegistry, officalRegistry));
    FreeStyleBuild build = new MockBuild(j.createFreeStyleProject(), folder.newFolder());
    FilePath npmrcFile = ConfigFileManager.provisionConfigFile(new ConfigFile(config.id, null, true), null, build, build.getWorkspace(), j.createTaskListener(), new ArrayList<String>(1));
    assertTrue(npmrcFile.exists());
    assertTrue(npmrcFile.length() > 0);
    Npmrc npmrc = Npmrc.load(new File(npmrcFile.getRemote()));
    assertTrue("Missing setting email", npmrc.contains("email"));
    assertEquals("Unexpected value from settings email", "guest@example.com", npmrc.get("email"));
}
Also used : FilePath(hudson.FilePath) Npmrc(jenkins.plugins.nodejs.configfiles.Npmrc) NPMRegistry(jenkins.plugins.nodejs.configfiles.NPMRegistry) ConfigFile(org.jenkinsci.lib.configprovider.model.ConfigFile) Config(org.jenkinsci.lib.configprovider.model.Config) NPMConfig(jenkins.plugins.nodejs.configfiles.NPMConfig) StandardUsernameCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials) FreeStyleBuild(hudson.model.FreeStyleBuild) ConfigFile(org.jenkinsci.lib.configprovider.model.ConfigFile) File(java.io.File) Test(org.junit.Test)

Example 2 with ConfigFile

use of org.jenkinsci.lib.configprovider.model.ConfigFile in project nodejs-plugin by jenkinsci.

the class NodeJSCommandInterpreter method perform.

/*
     * (non-Javadoc)
     * @see hudson.tasks.CommandInterpreter#perform(hudson.model.AbstractBuild, hudson.Launcher, hudson.model.TaskListener)
     */
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws InterruptedException {
    try {
        EnvVars env = build.getEnvironment(listener);
        EnvVars newEnv = new EnvVars();
        // get specific installation for the node
        NodeJSInstallation ni = getNodeJS();
        if (ni == null) {
            if (nodeJSInstallationName != null) {
                throw new AbortException(Messages.NodeJSBuilders_noInstallationFound(nodeJSInstallationName));
            }
            // use system NodeJS if any, in case let fails later
            nodeExec = (launcher.isUnix() ? Platform.LINUX : Platform.WINDOWS).nodeFileName;
        } else {
            Node node = build.getBuiltOn();
            if (node == null) {
                throw new AbortException(Messages.NodeJSBuilders_nodeOffline());
            }
            ni = ni.forNode(node, listener);
            ni = ni.forEnvironment(env);
            String exec = ni.getExecutable(launcher);
            if (exec == null) {
                listener.fatalError(Messages.NodeJSBuilders_noExecutableFound(ni.getHome()));
                return false;
            }
            ni.buildEnvVars(newEnv);
            // enhance env with installation environment because is passed to supplyConfig
            env.overrideAll(newEnv);
            nodeExec = ni.getExecutable(launcher);
            if (nodeExec == null) {
                throw new AbortException(Messages.NodeJSBuilders_noExecutableFound(ni.getHome()));
            }
        }
        if (configId != null) {
            // add npmrc config
            ConfigFile cf = new ConfigFile(configId, null, true);
            FilePath configFile = ConfigFileManager.provisionConfigFile(cf, env, build, build.getWorkspace(), listener, new ArrayList<String>());
            newEnv.put(NodeJSConstants.NPM_USERCONFIG, configFile.getRemote());
            build.addAction(new CleanTempFilesAction(configFile.getRemote()));
        }
        // add an Environment so when the in super class is called build.getEnviroment() gets the enhanced env
        build.getEnvironments().add(Environment.create(newEnv));
    } catch (AbortException e) {
        // NOSONAR
        listener.fatalError(e.getMessage());
        return false;
    } catch (IOException e) {
        Util.displayIOException(e, listener);
        e.printStackTrace(listener.fatalError(hudson.tasks.Messages.CommandInterpreter_CommandFailed()));
    }
    return internalPerform(build, launcher, listener);
}
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) IOException(java.io.IOException) AbortException(hudson.AbortException)

Example 3 with ConfigFile

use of org.jenkinsci.lib.configprovider.model.ConfigFile 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)

Aggregations

FilePath (hudson.FilePath)3 ConfigFile (org.jenkinsci.lib.configprovider.model.ConfigFile)3 AbortException (hudson.AbortException)2 EnvVars (hudson.EnvVars)2 Node (hudson.model.Node)2 IOException (java.io.IOException)2 NodeJSInstallation (jenkins.plugins.nodejs.tools.NodeJSInstallation)2 CleanTempFilesAction (org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction)2 StandardUsernameCredentials (com.cloudbees.plugins.credentials.common.StandardUsernameCredentials)1 Computer (hudson.model.Computer)1 FreeStyleBuild (hudson.model.FreeStyleBuild)1 File (java.io.File)1 NPMConfig (jenkins.plugins.nodejs.configfiles.NPMConfig)1 NPMRegistry (jenkins.plugins.nodejs.configfiles.NPMRegistry)1 Npmrc (jenkins.plugins.nodejs.configfiles.Npmrc)1 Config (org.jenkinsci.lib.configprovider.model.Config)1 Test (org.junit.Test)1