Jenkins 2 NPM_TOKEN credential - jenkins

I'm trying to run Jenkins 2 pipeline (Jenkinsfile) that will use npm publish to publish a package to local NPM repository.
In order to do that I've try to use the following stage in Jenkinsfile:
stage('TEST npm whoami') {
withEnv(["PATH+NPM=${tool name: 'node-6', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'}/bin"]) {
withCredentials([[$class: 'StringBinding', credentialsId: 'npm-token', variable: 'NPM_TOKEN']]) {
sh """
npm whoami
"""
}
}
}
Currently I'm running only npm whoami and once that will work I'll replace it with npm publish.
This is the output I'm getting:
+ npm whoami
npm ERR! Linux 4.7.5-1.el7.elrepo.x86_64
npm ERR! argv "/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node-6/bin/node" "/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node-6/bin/npm" "whoami"
npm ERR! node v6.5.0
npm ERR! npm v3.10.3
npm ERR! code ENEEDAUTH
npm ERR! need auth this command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`

From looking at this GitHub issue, it seems like NPM_TOKEN isn't something that npm itself recognizes, but rather a custom environment variable that heroku (and maybe other platforms) interpret.
What I've done, based on some of the discussion in that issue, is to create a project-level .npmrc at job execution time based on the token env var from my credential, then remove the file again before continuing. E.g.:
stage('TEST npm whoami') {
withCredentials([string(
credentialsId: 'npm-token',
variable: 'NPM_TOKEN')]) {
sh "echo //npm.skunkhenry.com/:_authToken=${env.NPM_TOKEN} > .npmrc"
sh 'npm whoami'
sh 'rm .npmrc'
}
}
Hope this helps!

The answer of Gerard Ryan and Gaston is correct, I just wanted to add one detail that I did not get at first:
If you want to use a private repository, the .npmrc should also specify the registry:
withCredentials([string(credentialsId: 'registry', variable: 'token')]) {
try {
sh "echo registry=<your-registry-URL> >> .npmrc"
sh "echo //<your-registry-URL>/:_authToken=${env.token} >> .npmrc"
sh 'npm whoami'
} finally {
sh 'rm ~/.npmrc'
}
}

Related

Newman htmlextra reporter complains about newman is missing but it's installed

I'm trying to install and run Postman's Newman tests collection with HTML reporter (on a jenkins podTemplate container with docker image from Postman's account) but it keeps failing because no suitable Newman version is found:
"npm WARN newman-reporter-htmlextra#1.19.6 requires a peer of newman#>=4 but none is installed. You must install peer dependencies yourself"
Newman image docker is "postman/newman:5.2-alpine".
And the Run command is
sh "newman run tests/collection.json -r htmlextra --reporter-htmlextra-export var/reports/newman/html/index.html";
I've also tried to install with (the "sh" prefix is because it's in groovy script..in Jenkins) :
sh "npm install -g newman#4.6.1"
sh "npm install -g newman-reporter-htmlextra"
and then executing the same run command as above.
sh "newman run tests/collection.json -r htmlextra --reporter-htmlextra-export var/reports/newman/html/index.html";
But the results are the same.
What's weird is that right after I get the error mentioned above - the jenkinsfile executes the "newman run" command and successfully creates the tests report file:
Using htmlextra version 1.19.6
Created the htmlextra report in this location: var/reports/newman/html/index.html
But then exits the script/job with FAILURE.
What am I missing?
Any advice?
Thank you!
Thats a npm bug, https://github.com/npm/npm/issues/12905
for newman-reporter-htmlextra , newman is a peer dependency.
In npm peer dependency is not detected for global packages if the dependency and the package are not installed together
In this case you can fix it by installing it together using
npm install -g newman newman-reporter-htmlextra
Try :
podTemplate(label: "newmanPodHtmlExtra", containers: [
containerTemplate(name: "newman", image: "dannydainton/htmlextra", command: "cat", ttyEnabled: true),
]) {
node("newmanPodHtmlExtra") {
def testsFolder = "./tests";
container("newman") {
stage("Checkout") {
checkout scm;
}
try{
stage("Install & run Newman") {
sh "npm install -g newman newman-reporter-htmlextra";
sh "newman run ${testsFolder}/collection.json -r htmlextra --reporter-htmlextra-export var/reports/newman/html/index.html";
}
}catch(e){}finally{
stage("Show tests results") {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'var/reports/newman/html', reportFiles: 'index.html', reportName: 'API Tests', reportTitles: ''
])
}
}
}
}
}

Jenkins error Install Xvfb and run Cypress again

Create a pipeline to do the jenkins cypress test, but I always run it, make a mistake, and try various solutions, including Jenkins Xvfb, but I still didn't succeed.
Error
+ npm run exec:e2e
> projectname#0.1.0 exec:e2e /var/lib/jenkins/workspace/project/myproject
> npx cypress run
It looks like this is your first time using Cypress: 3.4.0
[?25l[01:56:21] Verifying Cypress can run /var/lib/jenkins/.cache/Cypress/3.4.0/Cypress [started]
[01:56:21] Verifying Cypress can run /var/lib/jenkins/.cache/Cypress/3.4.0/Cypress [failed]
[?25hYour system is missing the dependency: Xvfb
Install Xvfb and run Cypress again.
Read our documentation on dependencies for more information:
https://on.cypress.io/required-dependencies
If you are using Docker, we provide containers with all required dependencies installed.
----------
Error: spawn Xvfb ENOENT
----------
Platform: linux (Ubuntu Linux - 19.04)
Cypress Version: 3.4.0
[?25hnpm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! projectname#0.1.0 exec:e2e: `npx cypress run`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the projectname#0.1.0 exec:e2e script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /var/lib/jenkins/.npm/_logs/2019-07-24T01_56_21_795Z-debug.log
I get resolved this question with other solution.
First, i created a pipeline
stage('E2E Tests') {
sh 'docker run -v $PWD:/e2e -w /e2e cypress/included:3.4.0'
}
What you can do is having a lock on your stage
stage('Run E2E tests') {
dir("cypress-e2e-tests") {
lock("cypress-e2e-tests-${env.NODE_NAME}") {
sh 'docker run -v $PWD:/e2e -w /e2e cypress/included:3.4.0'
}
}
}

How to add name to each step in jenkins pipeline stages

stage('type checking') {
steps {
script {
docker.image('node:8').inside() {
sh '''
npm run lint
npm run type-check
'''
}
}
}
}
I want to separate npm run lint and npm run type-check into 2 steps and give each a meaningful name. how to give a name for each step?
Not sure this can help:
sh 'npm run lint &'
sh 'npm run type-check &'

Jenkins stash is not stashing all files and folders

I have Jenkinsfile, Im trying to stash BuildApp
and unstash it into other docker container to run npm test.
but seems like it's missing node_modules, so npm test fails.
steps {
dir("/var/jenkins_home") {
unstash "app"
}
echo "Building app (CLOUD_ENV = ${env.CLOUD_ENV})"
sh 'yarn install'
stash name: "BuildApp", includes: "*"
sh "ls -la ${pwd()}"
}
That's where I'm trying to stash and I did debug with "ls -la ${pwd()}"
and I see that node_modules are there
but when unstash them
steps {
dir("/var/jenkins_home") {
unstash "BuildApp"
}
echo "Testing app (CLOUD_ENV = ${env.CLOUD_ENV})"
// unstash 'builtApp'
sh "ls -la ${pwd()}"
sh 'npm test > test.out'
archiveArtifacts artifacts: 'test.out', fingerprint: true
stash name: "testApp", includes: "*"
}
I did "ls -la ${pwd()}" and I can see that node_modules folder is not there.
Am I doing something wrong?
I guess that * only includes files in the current directory. In that case ** should do the trick since it also matches path separators.
Also in your code you unstash in a different directory than you execute ls. I don't see any reason for the dir command, so I suggest to remove it.

How can I add another git pull from one jenkins pipeline stage

I would like to ask on how can I add another step for pulling another repo on the jenkins pipeline I created. You see from the jenkins pipeline settings I already specified a repo for pulling the ui to build. Then after the build is made I need to pull another repo for api and build it as one docker image. I already tried this doc however I'm getting issue on getting the the ui files to combine on the api, here is my pipeline script used.
pipeline {
agent { label 'slave-jenkins'}
stages {blah blah
}
stage('Workspace Cleanup') {
steps {
step([$class: 'WsCleanup'])
checkout scm
}
}
stage('Download and Build UI Files') {
steps {
sh '''#!/bin/bash
echo "###########################"
echo "PERFORMING NPM INSTALL"
echo "###########################"
npm install
echo "###########################"
echo "PERFORMING NPM RUN BUILD"
echo "###########################"
npm run build
echo "###########################"
echo "Downloading API Repo"
echo "###########################"
**git branch: 'master',**
**credentialsId: 'XXXXXXXXXXXX',**
**url: 'ssh://git#XXXXXXe:7999/~lXXXXXX.git'**
echo ""
'''
}
}
}
You shouldn't include Jenkins Pipeline DSL into shell script - it must be a separate step, for example
steps {
// first step to run shell script
sh '''#!/bin/bash
echo "###########################"
echo "PERFORMING NPM INSTALL"
echo "###########################"
npm install
echo "###########################"
echo "PERFORMING NPM RUN BUILD"
echo "###########################"
npm run build
echo "###########################"
echo "Downloading API Repo"
echo "###########################"
'''
// second step to checkout git repo
git branch: 'master',
credentialsId: 'XXXXXXXXXXXX',
url: 'ssh://git#XXXXXXe:7999/~lXXXXXX.git'
}
But mixing two repos in one workspace (and two responsibilities in one job) probably isn't a good idea. It would be better if you split your continuous delivery process into multiple jobs:
job to build UI;
job to build API;
job to create Docker image.
and then chain this jobs, so that they are executed one by one and pass build artifacts to each other. So each part of your CD process will satisfy the principle of
single responsibility.

Resources