jenkins 声明性管道忽略 jenkinsfiles 的变更日志

jenkins declarative pipeline ignoring changelog of jenkinsfiles

我在 git 存储库中有应用程序及其代码。还有用于构建应用程序的 jenkinsfiles 和另一个存储库中的这些文件。问题是詹金斯建立了变更日志。 Jenkins 添加 jenkinsfiles 变更日志来构建变更集,我不想这样做。因为这些更改是根据与应用程序无关的基础架构进行的。如何防止这种情况?我没有找到任何解决方法或解决方案。

如果我回答你的问题...我不认为你可以从 Jenkins 从 git 读取的变更集中删除 Jenkinsfile 变更,但是,如果有的话,你可以跳过你的管道来构建仅对 Jenkinsfile 进行更改。

如果有帮助... 首先,你需要阅读变更集:

    def changedFiles = []
    for (changeLogSet in currentBuild.changeSets) { 
        for (entry in changeLogSet.getItems()) { 
            for (file in entry.getAffectedFiles()) {
                changedFiles.add(file.getPath())
            }
        }
    }

然后,你可以检查是否只有Jenkinsfile:

if (changedFiles.size() == 1 && changedFiles[0] == "Jenkinsfile"){
    println "Only Jenkinsfile has changed... Skipping build..."
    currentBuild.getRawBuild().getExecutor().interrupt(Result.SUCCESS) // this skips your build prematurely and makes the rest of the stages green
    sleep(1) // you just need this
}else{
    println "There are other changes rather than only Jenkinsfile, build will proceed."
}

P.S。您有多种方法可以提前终止作业而不会导致构建失败,但根据我的经验,这是最干净的方法,即使您需要在 Jenkins 上允许一些管理员签名权限(我前段时间在另一个线程中看到过它,不过现在找不到了)