angularlinuxjenkinsnpmjenkins-pipeline

Jenkins pipeline doesn't recognise my Angular project


I have a project with a more complex structure that I'm working on, like this: 'rootFolder/frontendParentFolder/actualAngularProject'

I have a Jenkins job that tries to run the code coverage task for the angular project.

The step that I have right now is like this:

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

The Jenkins job does checkout and build and other stuff, it obviously starts from the root of the project. When it gets to this step it says this ...

[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] }

I tried also mentioning the name of the project like sh

ng test actualAngularProject --code-coverage --watch=false

But I still get the same thing.

I tried to install the NPM plugin, but I didn't know how to use it and didn't find a proper example for it.

I have:

Jenkins ver. 2.222.3; 
Angular CLI: 8.3.18; 
Node: 12.13.0; 
OS: linux x64

If I try to run the same command from CLI, it works. What could be the reason for this?


Solution

  • Whatever you are doing is correct, the only thing is: You might miss the actual path of the angular project folder. my below example, illustrate something similar to your example and will help you to debug and find your root cause

    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"
                    }
                }
            }
        }
    }