5

I have a single repo which contains multiple projects, and that too APIs in different frameworks/technologies.

I have multiple jenkins pipelines setup to cater all these jobs, and handling the conditions on stage level to skip them using For e.g.

when { 
                changeset "**servicelayer/*"
            }

Above is a scenario where I do not want to execute the full pipeline if someone checks in files to a specific folder. But this only allows to skip a particular stage. Is there a way that I can validate that at the Pipeline level to see if the condition is not met then skip the whole job?

NOTE: I have already tried configuring the included/excluded region at the Jenkins pipeline level. It doesn't work for me at all.

Sorabh Mendiratta
  • 911
  • 12
  • 23
  • https://stackoverflow.com/questions/5243593/how-to-trigger-a-build-only-if-changes-happen-on-particular-set-of-files – rohit thomas Jul 10 '18 at 09:18
  • Thanks @rohitthomas for sharing this. As I mentioned, i have tried adding the additional behaviour with giving a java pattern based regular expression for included/excluded region but somehow it is not working. I have tested the expression on few sites to see if i am making a mistake but it was fine. Hence I had to resort on the when expression to check for the changeset. The expression I used was "servicelayer\/[a-zA-Z0-9\\s\.]*" I tried finding the changeset and it was always falling correctly under the above regex. But still of no avail. – Sorabh Mendiratta Jul 10 '18 at 13:05
  • try giving the actual folder name like `servicelayer/myfolder` and see if that works, ignore the regex part for now....And the reason for included/excluded region is that if it works, then a job will not get executed itself ... but in the when condition the job will get triggered and if that's fine, then just do an `sh "exit 1"` it will fail the whole build – rohit thomas Jul 11 '18 at 02:42

1 Answers1

1

Its been sometime, i solved the issue way back but forgot to post the answer. It may help someone in future. Correct it or add comments for a better solution, i will be happy to use that if found successful.

To explain the steps here adding some comments

stage('Validation') {
    steps {
            //Moving in to the directory to execute the commands
            dir('servicelayer') {
                script {
                    //Using the git command to check the difference between previous successful commit. ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} is an environment variable comes with GIT Jenkins plugin
                    //There is a drawback though, if it is the first time you are running this job, this variable is not available and fails the build
                    //For the first time i had to use ${env.GIT_COMMIT} itself at both places to pass the build. A hack but worth it for future builds.

                    def strCount = sh(returnStdout: true, script: "git diff --name-only ${env.GIT_COMMIT} ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} | grep servicelayer | wc -l").trim()
                    if(strCount=="0") {
                        echo "Skipping build no files updated"
                        CONTINUE_BUILD = false
                    } else {
                        echo "Changes found in the servicelayer module"
                    }
                }
            }
        }
   }
stage('Installation') {
        when {
            expression { return params.FORCE_BUILD || CONTINUE_BUILD }
        }
        steps {
            dir('servicelayer') {
                nodejs(nodeJSInstallationName:"${NODEJS_INSTALLATION_NAME}", configId:"${NODEJS_CONFIG_ID}") {
                    sh 'npm install --progress=false'
                }
            }
        }    
    }
Sorabh Mendiratta
  • 911
  • 12
  • 23
  • I happened to run into a similar issue as you. I have question about your solution. Is the `CONTINUE_BUILD` the key to skip the whole jenkins pipeline? cause I didn't see any special actions but an "echo" and an normal assignment when `strCount == "0"`. If it is, how does the variable `CONTINUE_BUILD` make the whole pipeline skipped. Could you provide more details? Thank you. – Bruce May 09 '19 at 01:20
  • Apologies for the late reply, i somehow missed the notification. Yes I was using CONTINUE_BUILD variable as in when expression to proceed further or halt. Updating the above snippet to provide more info. – Sorabh Mendiratta May 29 '19 at 02:19