理解 groovy jenkins 文件中的代码

Understanding groovy code in jenkins file

我对 Groovy 和 Jenskins 完全陌生,如果问题听起来很菜鸟,请忽略。以下是来自 jenkins 文件的代码片段,其中包含 groovy 代码。

def boolean hasChanged(String searchText) {
    return sh(
        returnStatus: true,
        script: "git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} | grep \"${searchText}\""
    ) == 0
}

问题:

  1. 上面的片段是function/method写在groovy中吗?
  2. return sh是做什么的?
  3. 根据我的理解 script: "git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} | grep \"${searchText}\"" grep \"${searchText}\"" 的输出被输入 it diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT},理解正确吗?

请协助。

它看起来像 Groovy 带有 Jenkins 插件 (sh)
这里我添加了注释来解释这段代码。

// 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
}

git diff 的输出通过管道传输到 grep 命令,该命令在 git diff output

中搜索给定文本
  1. 在这种情况下,整个 Groovy 函数 returns True 如果 grep 在命令 git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} 的输出中找到 ${searchText},否则 False.