cleanupWithJenkinsJobDelete

cleanupWithJenkinsJobDelete

我在 Jenkins 管道中使用以下代码来构建docker镜像并推送到自定义 Artifact 注册表。

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

有人可以解释cleanupWithJenkinsJobDelete选项有什么用吗?
所有可用选项及其含义的任何文档链接都将有所帮助。
谢谢。

最佳答案

我找不到关于cleanupWithJenkinsJobDelete的真实文档。

官方Javadoc:boolean cleanupWithJenkinsJobDelete

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

如果您责怪该类,那么您可以在此成员的所在位置看到提交。该提交有一个不错的描述:Feature to delete images from repository when jenkins culls the job.

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

当前的主分支(请参阅此分支:DockerRunListener)已禁用了代码部分(已在此commit中完成),所以我猜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);
                }
                      */
            }
        }
}

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

关于docker - jenkins-pipeline中DockerBuilderPublisher的cleanupWithJenkinsJobDelete有什么用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59196703/

10-13 05:14