首页 > 解决方案 > jenkins-pipeline 中 DockerBuilderPublisher 的 cleanupWithJenkinsJobDelete 有什么用?

问题描述

我在 jenkins 管道中使用以下代码来构建 docker 映像并推送到自定义工件注册表。

step([
                $class: 'DockerBuilderPublisher', 
                cleanImages: true, 
                cleanupWithJenkinsJobDelete: true, 
                cloud: 'docker-cloud', 
                dockerFileDirectory: '.', 
                pushCredentialsId: 'docker-jenkins-credential', 
                pushOnSuccess: true, 
                tagsString: "<docker-artifactory-repo>/<imagename>:<imagetag>"
            ])

有人可以解释选项的cleanupWithJenkinsJobDelete作用和用途是什么吗?所有可用选项和含义的任何文档链接都会有所帮助。谢谢。

标签: dockerjenkinsjenkins-pipeline

解决方案


我无法找到关于cleanupWithJenkinsJobDelete.

官方 javadoc: boolean cleanupWithJenkinsJobDelete

DockerBuildPublisher.class及其所有成员(也是cleanupWithJenkinsJobDelete)可以在这里找到。不幸的是,没有 javadoc,cleanupWithJenkinsJobDelete所以我们必须扫描代码以查看它的作用。

如果你责怪这个类,你可以看到这个成员被引入的提交。该提交有一个不错的描述:当 jenkins 剔除作业时从存储库中删除图像的功能。

提交引入了一个新类DockerRunListener,如果cleanupWithJenkinsJobDelete为真则执行逻辑。

当前的主分支(参见这个:DockerRunListener)禁用了代码部分(在此提交中完成)所以我的猜测是cleanupWithJenkinsJobDelete什么都不做?

@Override
    public void onDeleted(Run<?, ?> run) {
        super.onDeleted(run);
        List<DockerBuildImageAction> actions = run.getActions(DockerBuildImageAction.class);

    for(DockerBuildImageAction action : actions) {
        if( action.cleanupWithJenkinsJobDelete ) {
            LOGGER.info("Attempting to clean up docker image for " + run);


            if( action.pushOnSuccess ) {

                // TODO:

                /*
                DockerRegistryClient registryClient;
                try {
                    Identifier identifier = Identifier.fromCompoundString(action.taggedId);
                    registryClient = DockerRegistryClient.builder()
                            .withUrl(identifier.repository.getURL())
                            .build();
                    registryClient.registryApi().deleteRepositoryTag("library",
                            identifier.repository.getPath(),
                            identifier.tag.orNull());
                } catch (Exception ex) {
                    LOGGER.log(Level.WARNING, "Failed to clean up", ex);
                }
                      */
            }
        }
}

cleanupWithJenkinsJobDelete我检查了 git repo,我可以确认如果设置为 true ,这是执行逻辑的唯一位置。


推荐阅读