Why doesn't postinstall run every time? - jenkins

I have a package.json file that has the following JSON at the bottom.
"scripts": {
"postinstall": [
"./node_modules/bower/bin/bower install && ./node_modules/protractor/bin/webdriver-manager update"
]
}
My reason for having this is so bower dependencies and my protractor tests will run after an "npm install". However, it seems that "postinstall" doesn't always run. I'm trying to get everything setup on Jenkins, so I'd like to be able to run the following commands and be good to go.
npm install
grunt jenkins
The jenkins task calls tasks to build, test, and run e2e tests.

I was able to fix this by changing the command from an array to a string.
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install && ./node_modules/protractor/bin/webdriver-manager update"
}

Related

Github Desktop commit not working with husky prehook

I installed husky and set up a prehook, which works in the CLI. However, when I try to commit in the Github desktop, the "committing to..." button is loading forever. For context, .git file is located one level higher than package.json.
package.json
"scripts": {
...
"prepare": "cd .. && husky install dashboard/.husky"
}
pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
cd dashboard
npm run test
I tried adding PATH="/usr/local/bin:$PATH" to pre-commit but it doesn't seem to help.
I figured it out thanks to this comment here:
https://github.com/desktop/desktop/issues/2518#issuecomment-323872966
Basically, my hook was running npm run test, and the test had --watch option so it never exited. Removing the command fixed the issue.

Using cypress behind proxy in Jenkins pipeline

I have seen this on github but I was still not able to get cypress to configure/download correctly. In my jenkins pipeline I run npm install but this runs into a timeout because of the proxy. It downloads all other dependencies expect cypress.
What I did was download cypress and put the zip file in the project. I then run sh "setCYPRESS_INSTALL_BINARY=cypress.zip npm i cypress" but this still fails.
Part that fails in Jenkins pipeline:
sh "npm config set proxy http://<proxy>"
sh "npm config set registry http://<proxy>/"
sh "setCYPRESS_INSTALL_BINARY=cypress.zip npm i cypress"
sh "npm i"
sh "npm run build
How can I have npm i run without download cypress. I currently cannot get passed this line sh "setCYPRESS_INSTALL_BINARY=cypress.zip npm i cypress" but I am also concerned npm i will still try to download cypress after setCYPRESS_INSTALL_BINARY actually works
-------------------------Update One-----------------
I updated the Jenkinsfile to have CYPRESS_INSTALL_BINARY=cypress.zip npm i but now I get the below error. Cypress.zip is in the project now but would really like to have it uploaded to Jenkins and just reference the file path within my Jenkinsfile.
I am not sure that is possible and the most simple solution is the one I am doing I think however it fails.
New error:
[view] Running shell script
+ CYPRESS_INSTALL_BINARY=cypress.zip
+ npm install
npm WARN locking Error: EIO: i/o error, open '/home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock'
npm WARN locking /home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock failed { Error: EIO: i/o error, open '/home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock'
npm WARN locking stack: 'Error: EIO: i/o error, open \'/home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock\'',
npm WARN locking errno: -5,
npm WARN locking code: 'EIO',
npm WARN locking syscall: 'open',
npm WARN locking path: '/home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock' }
npm ERR! path /home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock
npm ERR! code EIO
npm ERR! errno -5
npm ERR! syscall open
npm ERR! EIO: i/o error, open '/home/jenkins/.npm/_locks/staging-c21b8f081f002623.lock'
script returned exit code 251
I had similar issues on Windows environment. I placed cypress.zip into project's root folder.
set CYPRESS_INSTALL_BINARY=cypress.zip
echo %CYPRESS_INSTALL_BINARY%
npm install cypress --save-dev
So installation did not work, because npm executable looks for cypress in <your project folder>\node_modules\cypress\
Try to set CYPRESS_INSTALL_BINARY to absolute path.
I'm assuming you're on linux, so the way to set an environment variable is CYPRESS_INSTALL_BINARY='cypress.zip' ("set " is only used on Windows)
Cypress will not try to download the binary if it's found on the system, even when running npm install again. By default Cypress downloads the binary to ~/.cache/Cypress, so on subsequent builds Cypress will not have to extract the zip if this directory persists between builds.
The following should work:
CYPRESS_INSTALL_BINARY='cypress.zip` npm install
no need to explicitly run npm install cypress
Then, ensure the ~/.cache directory is saved after and restored before npm install across builds.
or
Pre-extract the cypress.zip onto the build system to /Cypress, and use the following:
CYPRESS_INSTALL_BINARY=0 npm install ## Don't install the binary!
CYPRESS_RUN_BINARY='Cypress/Cypress' ## this is the Cypress executable!
again, make sure /Cypress persists across builds
What if I don't cache ~/.cache ?
Well, your build will still run, but take significantly longer as Cypress will have to extract its binary on every build. Read more here about caching the binary in CI
...
Edit: For Question Update
You're still not setting environment variables correctly. This will not work:
CYPRESS_INSTALL_BINARY='cypress.zip'
npm install
You need to do:
CYPRESS_INSTALL_BINARY='cypress.zip' npm install
In one line.
or
export CYPRESS_INSTALL_BINARY='cypress.zip'
npm install
-> Why here
I created a tool to deal with this problem:
https://github.com/tomasbjerre/dictator-cypress-example
You would need to clone this repository and publish your own cypress-dictator to your private npm repository:
https://github.com/tomasbjerre/dictator-cypress
It uses dictator builder to, before npm install, copy platform specific zip-files to root of repository and make sure it is pointed at by .npmrc.
{
"message": "Copy linux cypress to cypress.zip",
"triggers": [
{
"runningOnPlatform": ["linux"]
}
],
"actions": [
{
"copyFrom": "linux-x64.zip",
"target": "cypress.zip"
}
]
}

Jenkins Slave (service) , cannot detect protractor

We are using slave as service and we r trying to run protractor with simple batch file after calling to npm install , but from some reason protractor was not detected, do u know what could be the reason/problem?
if i use web option (slave) for running job - everything went fine,
BTW - I try to set the service with user that allowed to run the test, and also set node in PATH, but nothing help
Appreciate your comments,
Thanks
Eyal
because you install protractor as a global package, So you should use webdriver-manager from global package install folder. The current folder where you execute npm install -g protractor has no webdriver-manager this cmd/binary. So jenkins report can't find webdriver-manager in current folder or PATH.
For best practice, you should add protractor as your nodejs project's dependency through npm install -S protractor before you write script, after do that, you will found 'protractor' will be add into package.json.
When others who get your source code, he only need to execute npm install under folder where package.json insides to get all dependencies installed.
After npm install done, the webdriver-manager will be found <package.json file inside folder>\node_modules\.bin\webriver-manager
So your cmd should write down as following:
pwd
ls -l "${WORKSPACE}"
cd /d <package.json file inside folder>
npm install
node_modules\.bin\webdriver-manager update
node_modules\.bin\protractor conf.js

Lerna Monorepos and Travis-CI

I need to setup Travis in a monorepo,
I couldn't find resources.
How can I setup the npm deploy for every package?
To setup a lerna repository with travis:
Using:
$ node -v
v10.14.2
$ npm -v
6.4.1
With the structure:
packages/
foo
index.js
package.json
package-lock.json
bar
index.js
package.json
package-lock.json
package.json
package-lock.json
lerna.json
.travis.yml
package-lock.json must be included for all packages.
package.json
{
"name": "my-project-name",
"scripts": {
"postinstall": "lerna bootstrap",
"test": "my-testing-script",
...
},
"dependencies": {
"lerna": "^3.7.1",
...
}
}
NPM script postinstall to set up the packages before running script test. Some people install the package globally, but since you already installed it locally, you don't need to.
Since this is the main package.json, you can put all the dependencies in dependencies.
The package.json for the packages can be configured as you need.
lerna.json
{
"packages": [
"packages/*"
]
}
The file can be configured however you need.
.travis.yml
language: node_js
node_js:
- "10.14"
script: npm run test
Here you can configure the testing environment the way you need.
In my case, I needed to transpile some files with babel and I used before_script to run this process before the testing script is run.

new grunt task added to Jenkins is not found

I am running Jenkins on Windows and grunt v0.4.1. I added a new task grunt-replace to the grunt.js however jenkins is having an issue seeing it:
C:\Jenkins\workspace\xxx>grunt
Local Npm module "grunt-replace" not found. Is it installed?
I've tried several things...
npm install -g grunt-replace
Restarting the jenkins service
npm install grunt-replace
I have installed these as the service jenkins is running under..
What am i missing here? What is the correct way to get jenkins to see the new task?
whats the way you start your task? i would add grunt-replace to the devDependencies in your package.json , add your test-command to the scripts.test-property in your package.json, and then let jenkins execute that.
package.json
{
...some other configs
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt-replace": "*",
... all the grunt-modules you need to be installed
}
}
jenkins excute
$ npm install && npm test

Resources