Home > Software engineering >  Jenkins: Get GIT output into an array
Jenkins: Get GIT output into an array

Time:10-15

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

CodePudding user response:

This happens because you directly assign the bat command to the variable, which results in your command and its' output being written into the variable.

To avoid this, use:

def gitOutput = bat(script: script, returnStdout: true)

where the script is the command that needs to be run. This will return only the output and not the command as well.

CodePudding user response:

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

  • Related