@Override
    public void build(DefaultGoPublisher publisher, EnvironmentVariableContext environmentVariableContext, TaskExtension taskExtension, ArtifactExtension artifactExtension, PluginRequestProcessorRegistry pluginRequestProcessorRegistry, Charset consoleLogCharset) {
        downloadMetadataFile(publisher);
        try {
            pluginRequestProcessorRegistry.registerProcessorFor(CONSOLE_LOG.requestName(), ArtifactRequestProcessor.forFetchArtifact(publisher, environmentVariableContext));
            final String message = format("[%s] Fetching pluggable artifact using plugin `%s`.", GoConstants.PRODUCT_NAME, artifactStore.getPluginId());
            LOGGER.info(message);
            publisher.taggedConsumeLine(TaggedStreamConsumer.OUT, message);

            List<FetchArtifactEnvironmentVariable> newEnvironmentVariables = artifactExtension.fetchArtifact(
                    artifactStore.getPluginId(), artifactStore, configuration, getMetadataFromFile(artifactId), agentWorkingDirectory());

            updateEnvironmentVariableContextWith(publisher, environmentVariableContext, newEnvironmentVariables);

        } catch (Exception e) {
            publisher.taggedConsumeLine(TaggedStreamConsumer.ERR, e.getMessage());
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            pluginRequestProcessorRegistry.removeProcessorFor(CONSOLE_LOG.requestName());
        }
    }
@Test
    public void shouldUpdateEnvironmentVariableContextAfterFetchingArtifact() {
        final FetchPluggableArtifactBuilder builder = new FetchPluggableArtifactBuilder(new RunIfConfigs(), new NullBuilder(), "", jobIdentifier, artifactStore, fetchPluggableArtifactTask.getConfiguration(), fetchPluggableArtifactTask.getArtifactId(), sourceOnServer, metadataDest.toFile(), checksumFileHandler);

        EnvironmentVariableContext environmentVariableContext = new EnvironmentVariableContext();
        environmentVariableContext.setProperty("VAR1", "old-value1", false);
        environmentVariableContext.setProperty("VAR2", "old-value2", true);
        environmentVariableContext.setProperty("VAR3", "old-value3", true);
        environmentVariableContext.setProperty("VAR4", "old-value4", true);

        when(artifactExtension.fetchArtifact(eq(PLUGIN_ID), eq(artifactStore), any(), anyMap(), eq(metadataDest.getParent().toString())))
                .thenReturn(List.of(
                        new FetchArtifactEnvironmentVariable("VAR1", "value1-is-now-secure", true),
                        new FetchArtifactEnvironmentVariable("VAR2", "value2-is-now-insecure", false),
                        new FetchArtifactEnvironmentVariable("VAR3", "value3-but-secure-is-unchanged", true),
                        new FetchArtifactEnvironmentVariable("VAR5", "new-value5-insecure", false),
                        new FetchArtifactEnvironmentVariable("VAR6", "new-value6-secure", true)
                ));

        builder.build(publisher, environmentVariableContext, null, artifactExtension, registry, UTF_8);

        Map<String, String> newVariablesAfterFetchArtifact = environmentVariableContext.getProperties();

        assertThat(newVariablesAfterFetchArtifact.size(), is(6));

        assertVariable(environmentVariableContext, "VAR1", "value1-is-now-secure", true);
        assertVariable(environmentVariableContext, "VAR2", "value2-is-now-insecure", false);
        assertVariable(environmentVariableContext, "VAR3", "value3-but-secure-is-unchanged", true);
        assertVariable(environmentVariableContext, "VAR4", "old-value4", true);
        assertVariable(environmentVariableContext, "VAR5", "new-value5-insecure", false);
        assertVariable(environmentVariableContext, "VAR6", "new-value6-secure", true);

        ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
        verify(publisher, atLeastOnce()).taggedConsumeLine(eq(OUT), captor.capture());


        assertThat(captor.getAllValues(), hasItems(
                "WARNING: Replacing environment variable: VAR1 = ******** (previously: old-value1)",
                "WARNING: Replacing environment variable: VAR2 = value2-is-now-insecure (previously: ********)",
                "WARNING: Replacing environment variable: VAR3 = ******** (previously: ********)",
                " NOTE: Setting new environment variable: VAR5 = new-value5-insecure",
                " NOTE: Setting new environment variable: VAR6 = ********"));

        String consoleOutput = String.join(" -- ", captor.getAllValues());
        assertThat(consoleOutput, not(containsString("value1-is-now-secure")));
        assertThat(consoleOutput, not(containsString("value3-but-secure-is-unchanged")));
        assertThat(consoleOutput, not(containsString("new-value6-secure")));
    }