I'm absolutely new to Groovy and Jenskins, please ignore if question sounds noob. Following is a code snippet from a jenkins file containing groovy code.
def boolean hasChanged(String searchText) {
return sh(
returnStatus: true,
script: "git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} | grep \"${searchText}\""
) == 0
}
Questions:
- Is the above snippet is function/method written in groovy?
- what does
return sh
do? - Per my understanding
script: "git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} | grep \"${searchText}\""
the output ofgrep \"${searchText}\""
is fed intoit diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT}
, is the understanding correct?
Please assist.
CodePudding user response:
It is looks like a Groovy with Jenkins plugins
(sh
)
Here I Added comments to explain this code.
// hasChanged method return boolean value
def boolean hasChanged(String searchText) {
// Insted of
// def shResult = sh(...); return shResult
// the sh results is returned
return sh(
// Preform the sh script and return the script exist code
returnStatus: true,
script: "git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} | grep \"${searchText}\""
) == 0 // check script exist code status
}
The output of git diff
is piped to grep
command that searches for given text in the git diff output
CodePudding user response:
- Yes
- In this case, the entire Groovy function returns
True
if grep finds${searchText}
in the output of commandgit diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT}
, or elseFalse
.