0

This is the Jenkisnfile script currently i am using to run my pipeline for the NodeJS applications. it's working well and suited to my previous pipeline process.

Now I Need to change a little bit in my script for the New Project Requirement.

In the "Step B" if the application failed to start with the NPM Start command, "Step C" (build) does not need to trigger the Job.

Now with this pipeline script approach, "Step C" (build) is always running whether the application failed to start or successfully run because of the parallel block.

pipeline {
agent any   
stages {
    stage('need to run parallelly'){
        steps{
            script{
                parallel(
                    a:{
                        dir('file path'){
                            bat """
                            npm install
                            """
                        }
                    },
                    b:{
                        dir('file path'){
                            bat """
                            npm start
                            """
                        }  
                    },
                    "build":{
                        build job: 'JenkinsTest'
                    },
                )
            }
        }
    }
}
Ojer_Dev
  • 108
  • 1
  • 12
  • 2
    Why don't you move Step C (I guess you mean the 'build'?) into a seperate stage? Jenkins will automatically stop the pipeline if A or B fails. – Michael Kemmerzell Sep 26 '22 at 12:27
  • Since I am using the npm start command in step b, it will always be logging the API calls in the console output. if I put the npm start command in the stage or step it won't end. So the Jenkins pipeline can't reach the step c stage I mean the trigger job stage. That's why I preferred parallel blocks. – Ojer_Dev Sep 26 '22 at 14:06

1 Answers1

0

There are 2 ways:

1/ Using global boolean parameter and sleep command. I mean, estimate the time ur Stage B run the "NPM Command" finished, sleep stage C until u get NPM Command result, for eg:

    boolean isFail = false
Stage B:
      
      if (NPM_Command.isFail()) isFail = true
    Stage C: 
      Sleep(60) //eg 1 min is enough for run stage B
      if(isFail) 
        // do your work

2/ As ur comment, ur problem is Stage C cant be reach if stage B failed, try this solution

himneh
  • 96
  • 6
  • hi, @himneh thank you for your answer. I think the first solution will work for me. I have modified the script as per the first solution. But still, I am getting the same error scenario. I am thinking that I have wrongly modified the script. Can you please help me to correct the script? i have added the [GitHubrepo](https://github.com/OjerDev/jenkins-nodejs-parallel) – Ojer_Dev Oct 26 '22 at 18:24
  • sorry for taking a long time to reply to your answer. – Ojer_Dev Oct 26 '22 at 18:27