skip to Main Content

I am writing a simple jenkins files to test, build and deploy angular application.
but my jenkins pipeline is failing at sh ‘npm install’

Here is my jenkins pipeline script

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/0APOCALYPSE0/ngrx-task.git'
            }
        }
        stage('Test') {
            steps {
                sh 'npm install'
                sh 'ng test'
            }
        }
        stage('Build') {
          steps {
            sh 'ng build'
          }
        }
        stage('Deploy') {
            steps {
                bat 'move dist\ngrx-task\*.* C:\nginx\'
            }
        }
    }
}

I am also attaching the screenshot of the error:
enter image description here

error:

Found unhandled java.io.IOException exception:
Cannot run program "nohup" (in directory "C:ProgramDataJenkins.jenkinsworkspaceAngular build and deploy"): CreateProcess error=2, The system cannot find the file specified
    java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1170)
    java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1089)
    hudson.Proc$LocalProc.(Proc.java:252)
    hudson.Proc$LocalProc.(Proc.java:221)
    hudson.Launcher$LocalLauncher.launch(Launcher.java:994)
    hudson.Launcher$ProcStarter.start(Launcher.java:506)
    org.jenkinsci.plugins.durabletask.BourneShellScript.launchWithCookie(BourneShellScript.java:180)
    org.jenkinsci.plugins.durabletask.FileMonitoringTask.launch(FileMonitoringTask.java:134)
    org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.start(DurableTaskStep.java:329)
    org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:323)
    org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
    org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)

I tried googling but I am not able to find the solution. I am new to jenkins/devops. I think there is something wrong with sh cmd maybe jenkins in installed in windows that is why it is not able to run sh cmd.

Can anyone help me with this?

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Thanks https://stackoverflow.com/users/419705/roddy-of-the-frozen-peas for a suggestion. After replacing sh keyword with bat my jenkins pipeline is working fine.

    Here is the updated file:

    pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/0APOCALYPSE0/ngrx-task.git'
            }
        }
        stage('Test') {
            steps {
                bat 'npm install'
                bat 'npm run ng test'
            }
        }
        stage('Build') {
          steps {
            bat 'npm run ng build'
          }
        }
        stage('Deploy') {
            steps {
                bat 'move dist\ngrx-task\*.* C:\nginx\'
            }
        }
    }
    

    }


  2. I suppose you are running the default agent wish is the Built-In Node wish is not recommended, you have to set up agent either a windows agent (in wish you will have to use PowerShell "bat") or set up a Linux agent wish uses "sh".
    An agent could be a virtual machine, a raspberry pie or a remote server, anything with a command line.
    This is a guide on how to set up a Linux virtual machine agent on youtube:
    https://www.youtube.com/watch?v=99DddJiH7lM&t=403s&ab_channel=CloudBeesTV
    and the agent documentation
    furthermore, you will have to have Node.js installed on it and in path.
    Alternatively, you can use docker containers as agent you will need docker to be installed and in path
    Refer to the Docker with Pipeline doc :
    https://www.jenkins.io/doc/book/pipeline/docker/
    exp :

    pipeline {
        agent {
            docker { image 'node:20.16.0-alpine3.20' }
        }
        stages {
            stage('Test') {
                steps {
                    sh 'node --version'
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search