詹金斯无法找到npm [英] Jenkins unable to find npm

查看:545
本文介绍了詹金斯无法找到npm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此linux服务器上,我有一个名为"myuser"的用户. 对于这个用户,在回显路径时,我得到了这个信息:

On this linux server, I have a user named "myuser". For this user, when echoing the path, I get this:

/home/myuser/bin:/home/myuser/.local/bin:/home/myuser/.nvm/versions/node/v6.11.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

/home/myuser/bin:/home/myuser/.local/bin:/home/myuser/.nvm/versions/node/v6.11.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

具有节点应用程序,在手动部署时,我运行:

Having a node application, when deploying manually I run:

npm i

它有效.

现在,我安装了Jenkins.我要安装的Jenkins项目位于:

Now, I installed Jenkins. Jenkins project I'm trying to install is located at:

/var/lib/jenkins/workspace/test

该构建正在执行Shell脚本.在该窗口中,我输入:

The build is executing a shell script. In that window I entered:

#!/bin/bash
npm i

在与詹金斯一起建造时,我得到了:

When building with Jenkins, I get this:

[test] $ /bin/bash /tmp/jenkins756533162549346948.sh
/tmp/jenkins756533162549346948.sh: line 3: npm: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

如果我只写:

echo $PATH

在詹金斯(Jenkins)外壳中,我得到了:

in the Jenkins shell, I get this:

[test] $ /bin/sh -xe /tmp/jenkins5067097808572366507.sh
+ echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
[test] $ /var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/6.11.1/bin/node /tmp/jenkins8733250738704177758.js
Finished: SUCCESS

如您所见,我安装了nodejs插件. 无论如何,当使用Jenkins shell时,找不到npm甚至even节点. 我如何让Jenkins知道npm/node在哪里? 我试图先在shell中写这个:

As you can see, I installed nodejs plugin. Anyway, when using Jenkins shell, the npm and even node are not found. How can I make Jenkins to know where npm/node is? I have tried to first write this in the shell:

$ PATH =/home/myuser/.nvm/versions/node/v6.11.1/bin

$PATH=/home/myuser/.nvm/versions/node/v6.11.1/bin

但仍然没有运气.

推荐答案

只需为jenkins安装nodeJS plugin,您就可以找到它

Just install the nodeJS plugin for jenkins, you can find it here.

安装插件后,重新启动jenkins,然后转到全局配置以指定版本.

After installing the plugin, restart jenkins, and go to the global configs to specify the version.

有关配置的完整详细信息,请参见上面链接的插件文档.

The full details of configurations can be found in the plugin documentation linked above.

要进入jenkins 2.x的插件页面:

To get to the plugin page in jenkins 2.x:

只需进入Manage Jenkins > Manage Plugins视图,即可对Jenkins环境的管理员使用. - https://jenkins.io/doc/book/managing/plugins/

simply go to Manage Jenkins > Manage Plugins view, available to administrators of a Jenkins environment. - https://jenkins.io/doc/book/managing/plugins/

但是,我建议您使用管道代替插件CI流程:

However, I do recommend using pipelines instead of a plugin for the CI process:

管道是指示描述软件交付管道的各个部分的说明.

将此管道配置添加到jenkins上的node.js项目中,以使其运行.

Add this pipeline config to your node.js project on jenkins to have it running.

pipeline {
    agent {
        docker {
            image 'node:6-alpine'
            args '-p 3000:3000'
        }
    }
    environment {
        CI = 'true' 
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') { 
            steps {
                sh './jenkins/scripts/test.sh' 
            }
        }
    }
}

如您所见,这将为应用程序运行两个阶段,即 building testing . npm是通过docker映像node:6-alpine安装的.

As you can see this runs two stages, building and testing for the application. npm is installed through the docker image node:6-alpine.

Jenkins文档提供了完整的教程,可通过CI构建Node.js应用:

Jenkins docs provide a full tutorial to build a nodejs app through CI: https://jenkins.io/doc/tutorials/build-a-node-js-and-react-app-with-npm/

这篇关于詹金斯无法找到npm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆