Jenkins 管道无法识别我的 Angular 项目

Jenkins pipeline doesn't recognise my Angular project

我正在处理一个结构更复杂的项目,如下所示: 'rootFolder/frontendParentFolder/actualAngularProject'

我有一个 Jenkins 作业试图 运行 angular 项目的代码覆盖率任务。

我现在的步骤是这样的:

stage('Testing Front End') {
              steps {
                  echo 'Running front end tests...'
                  dir('frontendParentFolder/actualAngularProject') {
                        sh 'ng test --code-coverage --watch=false'
                  }
              }
}

Jenkins 作业会检查和构建以及其他东西,它显然是从项目的根开始的。当它到达这一步时,它说这个...

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Testing Front End)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
Running front end tests...
[Pipeline] dir
Running in /home/jenkins/workspace/rootFolder/frontendParentFolder/actualAngularProject
[Pipeline] {
[Pipeline] sh
[actualAngularProject] Running shell script
+ ng test --code-coverage --watch=false
The test command requires to be run in an Angular project, but a project definition could not be found.
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }

我也试过像 sh 'ng test actualAngularProject --code-coverage --watch=false' 一样提到项目的名称,但我仍然得到同样的结果。 我尝试安装 NPM 插件,但我不知道如何使用它,也没有找到合适的示例。

我有: 詹金斯版本。 2.222.3; Angular命令行界面:8.3.18; 节点:12.13.0; OS: linux x64

如果我尝试 运行 来自 CLI 的相同命令,它会起作用。这可能是什么原因?

无论您做什么都是正确的,唯一的问题是:您可能会错过 angular 项目文件夹的实际路径。 我下面的示例说明了与您的示例类似的内容,将帮助您调试并找到根本原因

pipeline {
    agent any;
    stages {
        stage("create folder & file for debug") {
            steps {
                deleteDir()
                sh """
                    mkdir -p a/b/c
                    echo "Hello , I am from folder C" >> a/b/c/c.txt
                """    
            }
        }
        stage("debug") {
            steps {
                sh """
                    ls -al a
                    ls -al a/b
                    ls -al a/b/c
                    cat a/b/c/c.txt
                """
                
                dir('a/b/c') {
                    sh "cat c.txt"
                }
                
                // or
                
                dir("${env.WORKSPACE}/a/b/c") {
                    sh "cat c.txt"
                }
            }
        }
    }
}

我按照您的建议通过调试成功解决了这个问题。找到问题并解决了。 运行 在错误的文件夹中,因为我指向错误的文件夹,一开始似乎没问题。 所以这基本上是我的问题,指向一个类似的文件夹。 感谢您的帮助。