Home > Back-end >  Jenkins checking change in repository before build
Jenkins checking change in repository before build

Time:06-09

I am using a monorepo for my microservices and i have a pipeline for each service.I have a webhook that notifies jenkins whenever a change is made in the repository. So the problem now is i want to check a particular folder in the repository if there is a change before some operations. Thanks

CodePudding user response:

Solution 01 Processing the Webhook Request

When the Webhook notifies Jenkins it will send a bunch of details to Jenkins. Including the change logs. You should be receiving the below details. So as the initial step you can process the details coming from Github and determine whether the folder you are looking for is changed.

"added": [ ],
"removed": [ ],
"modified": ["test/testfile.yaml"]

You can use this as a reference.

Solution 02 without Processng the Webhook

It seems you cannot get this done with a simple pattern matching as directory name checking is not supported at the moment. But as a workaround, you can use something like the below. You can specify the directory name you want to check as folderName = "DIR_NAME"

pipeline {
    agent any

    stages {
        stage('Cloning') {
            steps {
                // Get some code from a GitHub repository
                git branch: 'main', url: 'https://github.com/yasassri/sample.git'

            }

        }
        
        stage('Example Deploy') {
            when { expression {
                def changeLogSets = currentBuild.changeSets
                def folderName = "testdir2"
                    for (int i = 0; i < changeLogSets.size(); i  ) {
                        def entries = changeLogSets[i].items
                        for (int j = 0; j < entries.length; j  ) {
                            def entry = entries[j]
                            def files = new ArrayList(entry.affectedFiles)
                            for (int k = 0; k < files.size(); k  ) {
                                def file = files[k]
                                if(file.path.contains(folderName)){
                                    return true
                                }
                            }
                        }
                    }
                    return false
            }
                
            }
            steps {
                echo 'Deploying==================='
            }
    }
}
}

References: https://support.cloudbees.com/hc/en-us/articles/217630098-How-to-access-Changelogs-in-a-Pipeline-Job-

  • Related