cva6
cva6

Reputation: 395

Jenkins pipeline how to change to another folder and run npm tests

Currently, I am using Jenkins pipeline script.

For running my tests, I need to access my code which is sitting on the desktop.

I tried this:

pipeline {
  agent any

  tools {nodejs "node"}

  stages {
    stage('Tests') {
      steps {
        sh 'cd users/tests/'
        sh 'npm run shopfloor.shopfloor'
      }
    }
  }
}

How I can change to my test folder and then run "npm run test"

I tried the answer below however i am getting this error now:

Running in users/tests/
[Pipeline] {
[Pipeline] sh
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Operation not permitted
+ npm run shopfloor.shopfloor
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Operation not permitted
Error: EPERM: operation not permitted, uv_cwd
    at process.wrappedCwd (internal/bootstrap/switches/does_own_process_state.js:129:28)
    at process.cwd (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/node_modules/graceful-fs/polyfills.js:10:19)
    at Conf.loadPrefix (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/load-prefix.js:46:24)
    at load_ (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:109:8)
    at Conf.<anonymous> (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:96:5)
    at Conf.emit (events.js:315:20)
    at ConfigChain._resolve (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/node_modules/config-chain/index.js:281:34)
    at ConfigChain.add (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/node_modules/config-chain/index.js:259:10)
    at Conf.add (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:338:27)
    at Conf.<anonymous> (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:314:25)
internal/bootstrap/switches/does_own_process_state.js:129
    cachedCwd = rawMethods.cwd();

Upvotes: 4

Views: 5214

Answers (2)

subhadeep dutta gupta
subhadeep dutta gupta

Reputation: 213

Please try once using double quotes.

dir("folder")

in groovy Single quotes are a standard Java String while Double quotes are a templatable String.

Upvotes: 2

StephenKing
StephenKing

Reputation: 37610

Use the dir step to switch the directory and execute the commands in that context:

pipeline {
  agent any

  tools {nodejs "node"}

  stages {
    stage('Tests') {
      steps {
        dir('users/tests/') { // <<------------
          sh 'npm run shopfloor.shopfloor'
        }
      }
    }
  }
}

Upvotes: 8

Related Questions