【问题标题】:jenkins declarative pipeline ignoring changelog of jenkinsfiles詹金斯声明性管道忽略詹金斯文件的变更日志
【发布时间】:2020-04-25 05:40:31
【问题描述】:

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

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-declarative-pipeline changelog


    【解决方案1】:

    如果我对您的问题表示满意...我认为您不能从 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."
    }
    
    

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

    【讨论】:

    • 你的答案很明确。如果 changeLogSet 仅包含 JenkinsFile,我们可以在第一个代码中进行类似的更改,从 buildChangeset 中删除此 changeLogSet。然后你的第二个代码块 changeSet.size == 0 中断构建。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2019-01-09
    • 2018-11-28
    相关资源
    最近更新 更多