本文是 此长文中的部分内容,方便初学者照着操作。


在测试 Jenkins 流水线时,使用若依应用系统,有个模块需要使用 npm 进行安装,但是流水线报错,记录下碰到的问题。本文适合像我一样的 npm 小白

NPM 依赖问题

流水线运行 npm run build:prod 时报错,提示 vue-cli-service 未找到。

[Pipeline] stage
[Pipeline] { (代码编译 )
[Pipeline] sh
+ cd ruoyi-ui
+ sed -i s/localhost/ruoyi-gateway-svc/g vue.config.js
+ npm run build:prod

> ruoyi@3.6.3 build:prod
> vue-cli-service build

/tmp/buildprod-9e1c1786.sh: line 1: vue-cli-service: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

上面流水线的步骤是先从 Gitlab 拉取代码,然后进入 ruoyi-ui 目录,然后进行 npm build。这里缺少了 npm install 安装依赖这一步。

运行 npm install 时,npm 会自动读取当前目录下的 package.json 文件,并安装里面的 devDependencies

[root@k8s-m01 ruoyi-ui]# ls
README.md  babel.config.js  bin  build  package.json  public  src  vue.config.js

[root@k8s-m01 ruoyi-ui]# cat package.json
{
  "name": "ruoyi",
  "version": "3.6.3",
  "description": "若依管理系统",
  "author": "若依",
  "license": "MIT",
  "scripts": {
    "dev": "vue-cli-service serve",
    "build:prod": "vue-cli-service build",
    "build:stage": "vue-cli-service build --mode staging",
    "preview": "node build/index.js --preview",
    "lint": "eslint --ext .js,.vue src"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*.{js,vue}": [
      "eslint --fix",
      "git add"
    ]
  },
  "keywords": [
    "vue",
    "admin",
    "dashboard",
    "element-ui",
    "boilerplate",
    "admin-template",
    "management-system"
  ],
  "repository": {
    "type": "git",
    "url": "https://gitee.com/y_project/RuoYi-Cloud.git"
  },
  "dependencies": {
    "@riophae/vue-treeselect": "0.4.0",
    "axios": "0.24.0",
    "clipboard": "2.0.8",
    "core-js": "3.25.3",
    "echarts": "5.4.0",
    "element-ui": "2.15.13",
    "file-saver": "2.0.5",
    "fuse.js": "6.4.3",
    "highlight.js": "9.18.5",
    "js-beautify": "1.13.0",
    "js-cookie": "3.0.1",
    "jsencrypt": "3.0.0-rc.1",
    "nprogress": "0.2.0",
    "quill": "1.3.7",
    "screenfull": "5.0.2",
    "sortablejs": "1.10.2",
    "vue": "2.6.12",
    "vue-count-to": "1.0.13",
    "vue-cropper": "0.5.5",
    "vue-meta": "2.4.0",
    "vue-router": "3.4.9",
    "vuedraggable": "2.24.3",
    "vuex": "3.6.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "4.4.6",
    "@vue/cli-plugin-eslint": "4.4.6",
    "@vue/cli-service": "4.4.6",
    "babel-eslint": "10.1.0",
    "babel-plugin-dynamic-import-node": "2.3.3",
    "chalk": "4.1.0",
    "compression-webpack-plugin": "5.0.2",
    "connect": "3.6.6",
    "eslint": "7.15.0",
    "eslint-plugin-vue": "7.2.0",
    "lint-staged": "10.5.3",
    "runjs": "4.4.2",
    "sass": "1.32.13",
    "sass-loader": "10.1.1",
    "script-ext-html-webpack-plugin": "2.1.5",
    "svg-sprite-loader": "5.1.1",
    "vue-template-compiler": "2.6.12"
  },
  "engines": {
    "node": ">=8.9",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ]
}

npm install 可以通过 --registry=https://registry.npm.taobao.org来更换国内的源,加速依赖拉取。

运行 npm install 后,默认会将依赖文件保存在 ./node_modules/.bin 中,并自动将此目录加入 PATH 变量,因此里面的可执行程序可以不加完整路径直接执行。

如果要进行排错,可以在流水线中加入 sleep 10m,等待执行完 npm install 后,手动进入容器运行命令排错。

            container('node') {
                stage('代码编译 ') {
                    sh 'cd ruoyi-ui && sed -i \'s/localhost/ruoyi-gateway-svc/g\' vue.config.js && cat vue.config.js && ls -al && npm install --registry=https://registry.npm.taobao.org && sleep 10m && npm run build:prod && ls -la'
                }
            }


构建报错

运行流水线碰到这个错误,运行 npm install '--registry=https://registry.npm.taobao.org' 的过程中进程被 Killed,此问题我碰到过两次,实际上可能是由于网络连接原因引起的,比如 Pod 访问 npm 仓库中断等,安装命令没有问题,多运行几次可能就好了。

+ cd ruoyi-ui
+ sed -i s/localhost/ruoyi-gateway-svc/g vue.config.js
+ npm install '--registry=https://registry.npm.taobao.org'
 > git branch -a -v --no-abbrev # timeout=10
 > git checkout -b master 7930d922da0de217ad196ba54849c6fd5540945a # timeout=10
npm WARN ERESOLVE overriding peer dependency
Killed
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 137
Finished: FAILURE