Home > Net >  Is there a way to figure out what file in GiHub causes the build in Jenkins
Is there a way to figure out what file in GiHub causes the build in Jenkins

Time:07-09

I currently set up Jenkins and GitHub so that when a push is sent to GitHub a build is automatically triggered. Is there a way to get the name of the file that causes the build. For example, if I have a file in GitHub named test.txt in a repo called TestRepo and I commit a change in test.txt, how could I figure out the build was caused by a change in test.txt and not due to another file in the repo.

CodePudding user response:

Assuming you are using the GitHub plugin for polling, the build will be triggered by a commit/s not by a specific file. You can see the changes log directly in Jenkins in 'Changes' tab or if you need to get the list of files in the pipeline you can try to have a step with something like 'git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT' after the checkout step.

CodePudding user response:

You can use the following Groovy script to get the files changed. You can simply retrieve the changeset with currentBuild.changeSets and process the changeset as shown below to get the files that changed.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                git (url: 'https://github.com/xxxx/sample.git', branch: 'main')
                script {
                    def changedFiles = getFilesChanged()
                    echo "${changedFiles}"
                }
            }
        }
    }
}

def getFilesChanged() {
    def filesList = []
    def changeLogSets = currentBuild.changeSets
        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]
                    filesList.add(file.path)
            }
        }
    }
    return filesList
}
  • Related