Search code examples
jenkinsgroovy

Jenkins: Get GIT output into an array


I am currently using the bellow code to get a list of files that were changed between current commit and master branch:

        gitOutput = bat script: "git diff-tree -r --no-commit-id --name-only HEAD origin/master", returnStdout: true
        changedFiles = gitOutput .split("\n")
        echo changedFiles.toString() 

This is the output I am getting:

[
, D:\home\jenkins\myjob>git diff-tree -r --no-commit-id --name-only HEAD origin/master 
, Jenkinsfile, MyData/Program.cs]

The output I'd like to get would be:

[Jenkinsfile, MyData/Program.cs]

I've tried to add .trim() but I'm getting:

No signature of method: [Ljava.lang.String;.trim() is applicable for argument types: () values: []

And I'm not sure how to remove the elements that are my command and not the actual output


Solution

  • I found a solution by using: listOfFiles[2..-1] I can drop the first 2 elements and have what I need