Skip to content

Commit

Permalink
Merge pull request #114 from GaneshSPatil/read-system-env-vars-while-…
Browse files Browse the repository at this point in the history
…publishing-artifact

Allow support to read System environment variables while publishing artifact
  • Loading branch information
GaneshSPatil committed Jul 12, 2019
2 parents ecb3d5b + bc23ccd commit 81b79de
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
compile group: 'com.amazonaws', name: 'aws-java-sdk-ecr', version: '1.11.583'

testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.12.2'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.28.2'
testCompile group: 'org.skyscreamer', name: 'jsonassert', version: '1.5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;

import java.util.HashMap;
import java.util.Map;

import static cd.go.artifact.docker.registry.DockerRegistryArtifactPlugin.LOG;
import static java.lang.String.format;

Expand Down Expand Up @@ -53,8 +56,9 @@ public GoPluginApiResponse execute() {
final ArtifactStoreConfig artifactStoreConfig = publishArtifactRequest.getArtifactStore().getArtifactStoreConfig();
try {
final DockerClient docker = clientFactory.docker(artifactStoreConfig);
final DockerImage image = artifactPlan.getArtifactPlanConfig().imageToPush(publishArtifactRequest.getAgentWorkingDir(),
publishArtifactRequest.getEnvironmentVariables());
Map<String, String> environmentVariables = publishArtifactRequest.getEnvironmentVariables() == null ? new HashMap<>() : publishArtifactRequest.getEnvironmentVariables();
environmentVariables.putAll(System.getenv());
final DockerImage image = artifactPlan.getArtifactPlanConfig().imageToPush(publishArtifactRequest.getAgentWorkingDir(), environmentVariables);

LOG.info(format("Pushing docker image `%s` to docker registry `%s`.", image, artifactStoreConfig.getRegistryUrl()));
consoleLogger.info(format("Pushing docker image `%s` to docker registry `%s`.", image, artifactStoreConfig.getRegistryUrl()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public PublishArtifactRequest(ArtifactStore artifactStore, ArtifactPlan artifact
this.artifactPlan = artifactPlan;
}

public PublishArtifactRequest(ArtifactStore artifactStore, ArtifactPlan artifactPlan, String agentWorkingDir, Map<String, String> environmentVariables) {
this.agentWorkingDir = agentWorkingDir;
this.artifactStore = artifactStore;
this.artifactPlan = artifactPlan;
this.environmentVariables = environmentVariables;
}

public String getAgentWorkingDir() {
return agentWorkingDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
Expand All @@ -42,6 +43,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -66,12 +68,15 @@ public class PublishArtifactExecutorTest {
private DefaultDockerClient dockerClient;
@Mock
private DockerClientFactory dockerClientFactory;
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

private File agentWorkingDir;

@Before
public void setUp() throws IOException, InterruptedException, DockerException, DockerCertificateException {
initMocks(this);
environmentVariables.clear();
agentWorkingDir = tmpFolder.newFolder("go-agent");

when(dockerClientFactory.docker(any())).thenReturn(dockerClient);
Expand Down Expand Up @@ -133,4 +138,39 @@ public void shouldAddErrorToPublishArtifactResponseWhenFailedToPublishImage() th
assertThat(response.responseCode()).isEqualTo(500);
assertThat(response.responseBody()).contains("Failed to publish Artifact[id=id, storeId=storeId, artifactPlanConfig={\"BuildFile\":\"build.json\"}]: Some error");
}

@Test
public void shouldReadEnvironmentVariablesPassedFromServer() throws IOException, DockerException, InterruptedException {
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "${IMAGE_NAME}", Optional.of("3.6"));
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath(), Collections.singletonMap("IMAGE_NAME", "alpine"));

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
when(dockerProgressHandler.getDigest()).thenReturn("foo");

final GoPluginApiResponse response = new PublishArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

verify(dockerClient).push(eq("alpine:3.6"), any(ProgressHandler.class));
assertThat(response.responseCode()).isEqualTo(200);
assertThat(response.responseBody()).isEqualTo("{\"metadata\":{\"image\":\"alpine:3.6\",\"digest\":\"foo\"}}");
}

@Test
public void shouldReadEnvironmentVariablesFromTheSystem() throws IOException, DockerException, InterruptedException {
environmentVariables.set("IMAGE_NAME", "alpine");
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "${IMAGE_NAME}", Optional.of("3.6"));
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
when(dockerProgressHandler.getDigest()).thenReturn("foo");

final GoPluginApiResponse response = new PublishArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

verify(dockerClient).push(eq("alpine:3.6"), any(ProgressHandler.class));
assertThat(response.responseCode()).isEqualTo(200);
assertThat(response.responseBody()).isEqualTo("{\"metadata\":{\"image\":\"alpine:3.6\",\"digest\":\"foo\"}}");
}
}

0 comments on commit 81b79de

Please sign in to comment.