Skip to content

Commit

Permalink
feature(cf): use trigger for build metadata (#3721)
Browse files Browse the repository at this point in the history
Also include pipeline ID on server group and remove backwards compatibility for version on older existing server groups.
  • Loading branch information
claymccoy authored and jkschneider committed May 28, 2019
1 parent 8138ed8 commit cf31f15
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ private CloudFoundryServerGroup map(Application application) {

final CloudFoundryBuildInfo buildInfo = getBuildInfoFromEnvVars(environmentVars);
final ArtifactInfo artifactInfo = getArtifactInfoFromEnvVars(environmentVars);
final String pipelineId = environmentVars.get(ServerGroupMetaDataEnvVar.PipelineId.envVarName);
Arrays.asList(ServerGroupMetaDataEnvVar.values())
.forEach(envVar -> environmentVars.remove(envVar.envVarName));

Expand Down Expand Up @@ -370,6 +371,7 @@ private CloudFoundryServerGroup map(Application application) {
.env(environmentVars)
.ciBuild(buildInfo)
.appArtifact(artifactInfo)
.pipelineId(pipelineId)
.build();
}

Expand All @@ -382,15 +384,9 @@ private CloudFoundryBuildInfo getBuildInfoFromEnvVars(Map<String, String> enviro
}

private ArtifactInfo getArtifactInfoFromEnvVars(Map<String, String> environmentVars) {
String version = environmentVars.get(ServerGroupMetaDataEnvVar.ArtifactVersion.envVarName);
if (version == null) {
// This is here for backwards compatibility so that we will display the version on older
// server groups too.
version = environmentVars.get(ServerGroupMetaDataEnvVar.Version.envVarName);
}
return ArtifactInfo.builder()
.name(environmentVars.get(ServerGroupMetaDataEnvVar.ArtifactName.envVarName))
.version(version)
.version(environmentVars.get(ServerGroupMetaDataEnvVar.ArtifactVersion.envVarName))
.url(environmentVars.get(ServerGroupMetaDataEnvVar.ArtifactUrl.envVarName))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class DeployCloudFoundryServerGroupDescription
private boolean startApplication;
private Artifact applicationArtifact;
private Artifact manifest;
private String executionId;
private Map<String, Object> trigger;

@JsonIgnore private ArtifactCredentials artifactCredentials;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.netflix.spinnaker.clouddriver.deploy.DeploymentResult.Deployment.*;
import static java.util.stream.Collectors.toList;

import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.clouddriver.artifacts.maven.MavenArtifactCredentials;
import com.netflix.spinnaker.clouddriver.cloudfoundry.CloudFoundryCloudProvider;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.CloudFoundryApiException;
Expand All @@ -38,6 +39,8 @@
import java.util.*;
import java.util.function.Function;
import javax.annotation.Nullable;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -182,40 +185,32 @@ private static CloudFoundryServerGroup createApplication(
Optional.ofNullable(description.getApplicationAttributes().getEnv())
.map(HashMap::new)
.orElse(new HashMap<>());
final Artifact applicationArtifact = description.getApplicationArtifact();
if (applicationArtifact != null
&& MavenArtifactCredentials.TYPES.contains(applicationArtifact.getType())) {
description
.getArtifactCredentials()
.resolveArtifactName(applicationArtifact)
.map(
resolvedName ->
environmentVars.put(
ServerGroupMetaDataEnvVar.ArtifactName.envVarName, resolvedName));
description
.getArtifactCredentials()
.resolveArtifactVersion(applicationArtifact)
.map(
resolvedVersion ->
environmentVars.put(
ServerGroupMetaDataEnvVar.ArtifactVersion.envVarName, resolvedVersion));
Optional.ofNullable(applicationArtifact.getLocation())
.map(
artifactUrl ->
environmentVars.put(
ServerGroupMetaDataEnvVar.ArtifactUrl.envVarName, artifactUrl));
final Map<String, Object> metadata = applicationArtifact.getMetadata();
if (metadata != null) {
final Map<String, String> buildInfo =
(Map<String, String>) applicationArtifact.getMetadata().get("build");
if (buildInfo != null) {
environmentVars.put(ServerGroupMetaDataEnvVar.JobName.envVarName, buildInfo.get("name"));
environmentVars.put(
ServerGroupMetaDataEnvVar.JobNumber.envVarName, buildInfo.get("number"));
environmentVars.put(ServerGroupMetaDataEnvVar.JobUrl.envVarName, buildInfo.get("url"));
}
}
}
final ExternalReference artifactInfo = resolveArtifactInfo(description);
artifactInfo
.getName()
.map(name -> environmentVars.put(ServerGroupMetaDataEnvVar.ArtifactName.envVarName, name));
artifactInfo
.getNumber()
.map(
number ->
environmentVars.put(ServerGroupMetaDataEnvVar.ArtifactVersion.envVarName, number));
artifactInfo
.getUrl()
.map(url -> environmentVars.put(ServerGroupMetaDataEnvVar.ArtifactUrl.envVarName, url));
final ExternalReference buildInfo = resolveBuildInfo(description);
buildInfo
.getName()
.map(name -> environmentVars.put(ServerGroupMetaDataEnvVar.JobName.envVarName, name));
buildInfo
.getNumber()
.map(number -> environmentVars.put(ServerGroupMetaDataEnvVar.JobNumber.envVarName, number));
buildInfo
.getUrl()
.map(url -> environmentVars.put(ServerGroupMetaDataEnvVar.JobUrl.envVarName, url));
Optional.ofNullable(description.getExecutionId())
.ifPresent(
executionId ->
environmentVars.put(ServerGroupMetaDataEnvVar.PipelineId.envVarName, executionId));

CloudFoundryServerGroup serverGroup =
client
Expand All @@ -232,6 +227,68 @@ private static CloudFoundryServerGroup createApplication(
return serverGroup;
}

private static ExternalReference resolveArtifactInfo(
DeployCloudFoundryServerGroupDescription description) {
return Optional.ofNullable(description.getApplicationArtifact())
.map(
applicationArtifact -> {
final ExternalReference.ExternalReferenceBuilder artifactInfo =
ExternalReference.builder();
if (MavenArtifactCredentials.TYPES.contains(applicationArtifact.getType())) {
final ArtifactCredentials artifactCredentials =
description.getArtifactCredentials();
artifactInfo
.name(artifactCredentials.resolveArtifactName(applicationArtifact))
.number(artifactCredentials.resolveArtifactVersion(applicationArtifact))
.url(Optional.ofNullable(applicationArtifact.getLocation()));
}
return artifactInfo.build();
})
.orElseGet(() -> ExternalReference.builder().build());
}

private static ExternalReference resolveBuildInfo(
DeployCloudFoundryServerGroupDescription description) {
Map<String, Object> buildInfo = null;
if (buildInfo == null) {
final Artifact applicationArtifact = description.getApplicationArtifact();
if (applicationArtifact != null) {
final Map<String, Object> metadata = applicationArtifact.getMetadata();
if (metadata != null) {
buildInfo = (Map<String, Object>) applicationArtifact.getMetadata().get("build");
}
}
}
if (buildInfo == null) {
final Map<String, Object> trigger = description.getTrigger();
if (trigger != null) {
final String triggerType = (String) trigger.get("type");
if (triggerType.equals("jenkins")) {
buildInfo = (Map<String, Object>) trigger.get("buildInfo");
}
}
}
return Optional.ofNullable(buildInfo)
.map(
buildInfoMap ->
ExternalReference.builder()
.name(Optional.ofNullable(buildInfoMap.get("name")).map(Object::toString))
.number(Optional.ofNullable(buildInfoMap.get("number")).map(Object::toString))
.url(Optional.ofNullable(buildInfoMap.get("url")).map(Object::toString))
.build())
.orElse(ExternalReference.builder().build());
}

@Data
@Builder
private static class ExternalReference {
@Builder.Default private Optional<String> name = Optional.empty();

@Builder.Default private Optional<String> number = Optional.empty();

@Builder.Default private Optional<String> url = Optional.empty();
}

@Nullable
private File downloadPackageArtifact(DeployCloudFoundryServerGroupDescription description) {
File file = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public class CloudFoundryServerGroup extends CloudFoundryModel implements Server
@JsonView(Views.Cache.class)
ArtifactInfo appArtifact;

@JsonView(Views.Cache.class)
String pipelineId;

@Wither
@JsonView(Views.Relationship.class)
Set<CloudFoundryInstance> instances;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public enum ServerGroupMetaDataEnvVar {
JobUrl(ServerGroupMetaDataEnvVar.PREFIX + "BUILD_JOB_URL"),
ArtifactName(ServerGroupMetaDataEnvVar.PREFIX + "ARTIFACT_NAME"),
ArtifactVersion(ServerGroupMetaDataEnvVar.PREFIX + "ARTIFACT_VERSION"),
@Deprecated
Version(ServerGroupMetaDataEnvVar.PREFIX + "BUILD_VERSION"), // prefer ArtifactVersion
ArtifactUrl(ServerGroupMetaDataEnvVar.PREFIX + "ARTIFACT_URL");
ArtifactUrl(ServerGroupMetaDataEnvVar.PREFIX + "ARTIFACT_URL"),
PipelineId(ServerGroupMetaDataEnvVar.PREFIX + "PIPELINE_ID");

public static final String PREFIX = "__SPINNAKER_";
public final String envVarName;
Expand Down

0 comments on commit cf31f15

Please sign in to comment.