Node.js 如何让Jenkins从特定目录运行shell脚本?

Node.js 如何让Jenkins从特定目录运行shell脚本?,node.js,reactjs,jenkins,npm,Node.js,Reactjs,Jenkins,Npm,我只是按照教程做了一个基本的反应设置。在本教程中,它们直接在React client文件夹中工作,我现在尝试进行设置,其中client文件夹嵌套在父目录中,父目录是我的根目录,其内容如下所示: 主办机构(署长): 客户(主任) jenkins(包含脚本的目录) 詹金斯档案 节点_模块(dir) package.json package-lock.json 我现在遇到的问题是,当Jenkins文件: pipeline { agent { docker {

我只是按照教程做了一个基本的反应设置。在本教程中,它们直接在React client文件夹中工作,我现在尝试进行设置,其中client文件夹嵌套在父目录中,父目录是我的根目录,其内容如下所示:

主办机构(署长):

  • 客户(主任)
  • jenkins(包含脚本的目录)
  • 詹金斯档案
  • 节点_模块(dir)
  • package.json
  • package-lock.json
我现在遇到的问题是,当Jenkins文件:

pipeline {
    agent {
        docker {
            image 'node:6-alpine'
            args '-p 3000:3000'
        }
    }
    environment {
        CI = 'true'
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                sh './jenkins/scripts/test.sh'
            }
        }
    }
}
调用test.sh:

#!/usr/bin/env sh

echo 'The following "npm" command (if executed) installs the "cross-env"'
echo 'dependency into the local "node_modules" directory, which will ultimately'
echo 'be stored in the Jenkins home directory. As described in'
echo 'https://docs.npmjs.com/cli/install, the "--save-dev" flag causes the'
echo '"cross-env" dependency to be installed as "devDependencies". For the'
echo 'purposes of this tutorial, this flag is not important. However, when'
echo 'installing this dependency, it would typically be done so using this'
echo 'flag. For a comprehensive explanation about "devDependencies", see'
echo 'https://stackoverflow.com/questions/18875674/whats-the-difference-between-dependencies-devdependencies-and-peerdependencies.'
set -x
# npm install --save-dev cross-env
set +x

echo 'The following "npm" command tests that your simple Node.js/React'
echo 'application renders satisfactorily. This command actually invokes the test'
echo 'runner Jest (https://facebook.github.io/jest/).'
set -x
pwd
npm test
无法识别npm测试,因为脚本从
/var/jenkins_home/workspace/organizer
运行的位置不是package.json文件定义npm测试功能的客户端文件夹。我需要从客户机文件夹(organizer的子目录)中运行“npm测试”。我该怎么做呢?我还想到,npm的安装位置也完全错误。错误日志提供以下信息

+ pwd

/var/jenkins_home/workspace/organizer

+ npm test


> organizer@1.0.0 test /var/jenkins_home/workspace/organizer

> echo "Error: no test specified" && exit 1


Error: no test specified

npm ERR! Test failed.  See above for more details.

script returned exit code 1```




我更改了Jenkins文件中的测试阶段,使其类似于以下内容,以解决我的问题:

stage('Test') {
            steps {
                sh '''
                        cd client
                        ../jenkins/scripts/test.sh
                '''
            }
        }

您是否尝试了例如
sh”cd子文件夹&&./jenkins/scripts/test.sh“
?@MaratC是的,我用类似的命令修复了它。我会回答你的问题。