【问题标题】:How do I get Jenkins to run a shellscript from a particular directory?如何让 Jenkins 从特定目录运行 shell 脚本?
【发布时间】:2020-07-12 08:40:06
【问题描述】:

我刚刚按照this 教程获得了与 Jenkins 一起使用的基本 React 设置。在本教程中,它们直接在 React 客户端文件夹中工作,我现在正在尝试设置一个客户端文件夹嵌套在父目录中的设置,该父目录是我的根目录,内容如下:

组织者(目录):

  • 客户端(目录)
  • jenkins(包含脚本的目录)
  • Jenkins 文件
  • node_modules(目录)
  • package.json
  • package-lock.json

我现在遇到的问题是当 Jenkinsfile:

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 test 无法识别,因为从 /var/jenkins_home/workspace/organizer 运行脚本的位置不是 package.json 文件定义 npm test 功能的客户端文件夹。我需要以某种方式从客户端文件夹中运行“npm test”,该文件夹是管理器的子目录。我该怎么做呢?我还发现 npm install 也完全错误。错误日志提供以下内容。

+ 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```



【问题讨论】:

  • 你试过了吗? sh "cd subfolder && ./jenkins/scripts/test.sh" ?
  • @MaratC 是的,我设法用类似的命令修复了它。将回答修复。

标签: node.js reactjs jenkins npm


【解决方案1】:

我将 Jenkinsfile 中的测试阶段更改为类似于以下内容以解决我的问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2015-06-14
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多