Crime:

There was a problem recently, when you did not want to install an NVM or N version control upgrade node on Jenkins server for various reasons and the default version was always the ultra low version node. What if you want to specify the version of node to run?

The Jenkins script the bosses left behind reads something like this:

Let’s look at the run build script:

Then delight, pack up infinite failure:

It is node-v12, which is the same as the local node version. What should I do then? Pack the local version and release it directly, let me take you step by step to solve the puzzle

The clouds

At first glance, it seems fine to start the specified version of NPM from the absolute path on the server and run the srcipt script. However, when we add build:test to it with a “node -v”,

How come I specified the NPM version, but Node is v8.x? But that’s what happened, and xiaobian was very surprised.

clues

Then xiaobian will check the relevant information of NPM run, found such two sentences:

In addition to the shell’s pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix. The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of [email protected] you can customize the shell with the script-shell configuration.

Node_modules /. Bin = node_modules/. Bin = node_modules For example, vue-cli-service corresponds to node_modules/. Bin /vue-cli-service. Then, the script is executed using the default shell based on the platform environment. It is not executed in the same shell that started NPM.

Solve crimes

/usr/local/node-v12.9.1-linux-x64/bin/npm run build:test = /usr/local/node-v12.9.1-linux-x64/bin/npm run build:test = /usr/local/node-v12.9.1-linux-x64/bin/npm run build:test

  1. Run build:test using the NPM module delivered with Node-12
  2. Cross-env CUSTOM_ENV=test vue-cli-service build
  3. Execute the script through the system default shell (when not specified).
  4. Take vue-CLI as an example. The sh script looks like this:
  5. How does vue-CLI package script run because I won’t talk about it for space reasons, it doesn’t belong toThe scope of my knowledgeContent of this article.

To solve

But after reading this paragraph, I should have many ideas about how to solve it:

  • You can write your own.sh script, which is executed directly in Jenkins’ bash, using the same behavior as in the vue-cli-serve script, such as “node “$basedir/.. /usr/local/node-v12.9.1-linux-x64/bin/node “$basedir/.. /usr/local/node-v12.9.1-linux-x64/bin/node /node_modules/@vue/cli-service/bin/vue-cli-service.js” “$@”, pay attention to add node_modules

  • Jenkins execute shell unchanged, NPM run build:test:

     "scripts": {
      "build:test": "Cross - the env CUSTOM_ENV = test/usr/local/node - v12.9.1 - Linux - x64 / bin/node 'node_modules/@vue/cli-service/bin/vue-cli-service.js' build",},Copy the code
  • Take the hand of operation and maintenance and ask him to install NVM/N