首页 > 解决方案 > ws() 如何在 Jenkins groovy 中工作?

问题描述

情况1

下面的脚本管道代码workSpace用作 npm 构建过程的工作区,npm install可以在其中找到package.json文件workSpace

ws(workSpace){

                def commandString = "npm install ; npm rebuild node-sass ; ng build"
                executeCommand(commandString, repositoryName)
}

在哪里executeCommand()

def executeCommand(commandString, component){

        BUILD_FULL = sh (
                        script: commandString,
                        returnStatus: true
                    ) == 0
        echo "     Build status for ${component}: ${BUILD_FULL}"

}

案例2

但是,下面的代码与workSpace用于 npm 构建过程的工作区相同,但npm install无法package.jsonworkSpace

        ws(workSpace){

            buildStatus = sh (
                                returnStdout: true,
                                script: '''
                                        npm install // Install dependencies
                                        npm rebuild node-sass // Convert scss to css native
                                        ng build --prod --configuration=cloud // Build 
                                        '''
                            ) == 0
            print "User@ Build status for ${repositoryName} is ${buildStatus}"

        } // end ws()

以下是案例 2 中的错误

+ npm install // Install dependencies
npm ERR! code ENOLOCAL
npm ERR! Could not install from "../../../../../.." as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/jenkins/.npm/_logs/2019-01-07T16_20_20_339Z-debug.log

如何ws()在 groovy 中工作?

标签: jenkinsgroovybuildjenkins-pipelinejenkins-groovy

解决方案


您应该删除注释并用管道替换它们以在另一个完成后运行任务

sh '''
npm install ||
npm rebuild node-sass ||
ng build --prod --configuration=cloud 
'''

让我知道是否有效!


推荐阅读