Allow certain artifacts to be optionally deployed

Update `DistributeCommand` so that regex patterns can be used to mark
artifacts that are optional and need not fail the release.

Closes gh-22543
This commit is contained in:
Phillip Webb 2020-07-23 20:45:25 -07:00
parent d69c35a1db
commit b77dbcd06f
5 changed files with 153 additions and 4 deletions

View File

@ -20,6 +20,8 @@ import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.spring.concourse.releasescripts.ReleaseInfo;
@ -40,6 +42,7 @@ import org.springframework.util.Assert;
* Command used to deploy builds from Artifactory to Bintray.
*
* @author Madhura Bhave
* @author Phillip Webb
*/
@Component
public class DistributeCommand implements Command {
@ -50,9 +53,14 @@ public class DistributeCommand implements Command {
private final ObjectMapper objectMapper;
public DistributeCommand(ArtifactoryService artifactoryService, ObjectMapper objectMapper) {
private final List<Pattern> optionalDeployments;
public DistributeCommand(ArtifactoryService artifactoryService, ObjectMapper objectMapper,
DistributeProperties distributeProperties) {
this.artifactoryService = artifactoryService;
this.objectMapper = objectMapper;
this.optionalDeployments = distributeProperties.getOptionalDeployments().stream().map(Pattern::compile)
.collect(Collectors.toList());
}
@Override
@ -80,8 +88,18 @@ public class DistributeCommand implements Command {
}
}
ReleaseInfo releaseInfo = ReleaseInfo.from(buildInfo);
Set<String> artifactDigests = buildInfo.getArtifactDigests((artifact) -> !artifact.getName().endsWith(".zip"));
Set<String> artifactDigests = buildInfo.getArtifactDigests(this::isIncluded);
this.artifactoryService.distribute(type.getRepo(), releaseInfo, artifactDigests);
}
private boolean isIncluded(Artifact artifact) {
String path = artifact.getName();
for (Pattern optionalDeployment : this.optionalDeployments) {
if (optionalDeployment.matcher(path).matches()) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.concourse.releasescripts.command;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Distribution properties.
*
* @author Phillip Webb
*/
@ConfigurationProperties(prefix = "distribute")
public class DistributeProperties {
private List<String> optionalDeployments = new ArrayList<>();
public List<String> getOptionalDeployments() {
return this.optionalDeployments;
}
public void setOptionalDeployments(List<String> optionalDeployments) {
this.optionalDeployments = optionalDeployments;
}
}

View File

@ -16,6 +16,7 @@
package io.spring.concourse.releasescripts.command;
import java.util.Arrays;
import java.util.Set;
import com.fasterxml.jackson.databind.DeserializationFeature;
@ -42,6 +43,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
* Tests for {@link DistributeCommand}.
*
* @author Madhura Bhave
* @author Phillip Webb
*/
class DistributeCommandTests {
@ -55,8 +57,10 @@ class DistributeCommandTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
DistributeProperties distributeProperties = new DistributeProperties();
distributeProperties.setOptionalDeployments(Arrays.asList(".*\\.zip", "demo-\\d\\.\\d\\.\\d\\.doc"));
this.objectMapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
this.command = new DistributeCommand(this.service, this.objectMapper);
this.command = new DistributeCommand(this.service, this.objectMapper, distributeProperties);
}
@Test
@ -94,8 +98,30 @@ class DistributeCommandTests {
assertThat(artifactDigests).containsExactly("aaaaaaaaa85f5c5093721f3ed0edda8ff8290yyyyyyyyyy");
}
@Test
@SuppressWarnings("unchecked")
void distributeWhenReleaseTypeReleaseAndFilteredShouldCallService() throws Exception {
ArgumentCaptor<ReleaseInfo> releaseInfoCaptor = ArgumentCaptor.forClass(ReleaseInfo.class);
ArgumentCaptor<Set<String>> artifactDigestCaptor = ArgumentCaptor.forClass(Set.class);
this.command.run(new DefaultApplicationArguments("distribute", "RELEASE",
getBuildInfoLocation("filtered-build-info-response.json")));
verify(this.service).distribute(eq(ReleaseType.RELEASE.getRepo()), releaseInfoCaptor.capture(),
artifactDigestCaptor.capture());
ReleaseInfo releaseInfo = releaseInfoCaptor.getValue();
assertThat(releaseInfo.getBuildName()).isEqualTo("example");
assertThat(releaseInfo.getBuildNumber()).isEqualTo("example-build-1");
assertThat(releaseInfo.getGroupId()).isEqualTo("org.example.demo");
assertThat(releaseInfo.getVersion()).isEqualTo("2.2.0");
Set<String> artifactDigests = artifactDigestCaptor.getValue();
assertThat(artifactDigests).containsExactly("aaaaaaaaa85f5c5093721f3ed0edda8ff8290yyyyyyyyyy");
}
private String getBuildInfoLocation() throws Exception {
return new ClassPathResource("build-info-response.json", ArtifactoryService.class).getFile().getAbsolutePath();
return getBuildInfoLocation("build-info-response.json");
}
private String getBuildInfoLocation(String file) throws Exception {
return new ClassPathResource(file, ArtifactoryService.class).getFile().getAbsolutePath();
}
}

View File

@ -9,3 +9,7 @@ bintray:
sonatype:
user-token: sonatype-user
password-token: sonatype-password
distribute:
optional-deployments:
- '.*\.zip'
- 'spring-boot-project-\d\.\d\.\d(?:\.RELEASE)?\.pom'

View File

@ -0,0 +1,59 @@
{
"buildInfo": {
"version": "1.0.1",
"name": "example",
"number": "example-build-1",
"started": "2019-09-10T12:18:05.430+0000",
"durationMillis": 0,
"artifactoryPrincipal": "user",
"url": "https://my-ci.com",
"modules": [
{
"id": "org.example.demo:demo:2.2.0",
"artifacts": [
{
"type": "jar",
"sha1": "ayyyya9151a22cb3145538e523dbbaaaaaaaa",
"sha256": "aaaaaaaaa85f5c5093721f3ed0edda8ff8290yyyyyyyyyy",
"md5": "aaaaaacddea1724b0b69d8yyyyyyy",
"name": "demo-2.2.0.jar"
}
]
},
{
"id": "org.example.demo:demo:2.2.0:zip",
"artifacts": [
{
"type": "zip",
"sha1": "ayyyya9151a22cb3145538e523dbbaaaaaaab",
"sha256": "aaaaaaaaa85f5c5093721f3ed0edda8ff8290yyyyyyyyyz",
"md5": "aaaaaacddea1724b0b69d8yyyyyyz",
"name": "demo-2.2.0.zip"
}
]
},
{
"id": "org.example.demo:demo:2.2.0:doc",
"artifacts": [
{
"type": "jar",
"sha1": "ayyyya9151a22cb3145538e523dbbaaaaaaba",
"sha256": "aaaaaaaaa85f5c5093721f3ed0edda8ff8290yyyyyyyyzy",
"md5": "aaaaaacddea1724b0b69d8yyyyyzy",
"name": "demo-2.2.0.doc"
}
]
}
],
"statuses": [
{
"status": "staged",
"repository": "libs-release-local",
"timestamp": "2019-09-10T12:42:24.716+0000",
"user": "user",
"timestampDate": 1568119344716
}
]
},
"uri": "https://my-artifactory-repo.com/api/build/example/example-build-1"
}