Jenkins 管道如何更改到另一个文件夹并运行 npm 测试

cva*_*va6 2 jenkins jenkins-groovy jenkins-pipeline

目前,我正在使用 Jenkins 管道脚本。

为了运行我的测试,我需要访问桌面上的代码。

我试过这个:

pipeline {
  agent any

  tools {nodejs "node"}

  stages {
    stage('Tests') {
      steps {
        sh 'cd users/tests/'
        sh 'npm run shopfloor.shopfloor'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何更改到我的测试文件夹,然后运行“npm run test”

我尝试了下面的答案,但是我现在收到此错误:

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();
Run Code Online (Sandbox Code Playgroud)

Ste*_*ing 7

使用以下dir步骤切换目录并在该上下文中执行命令:

pipeline {
  agent any

  tools {nodejs "node"}

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