Stop image building on error from builder
Previously, the image builder used by the build tool plugins ignored errors from lifecycle phases and continued with subsequent phases. This commit inspects the status of the builder container after each lifecycle phase and aborts the image building process if the exit status of the container after any phase is non-zero. Fixes #19949pull/19986/head
parent
dc542b29d8
commit
c6a6024062
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2012-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 org.springframework.boot.buildpack.platform.build;
|
||||
|
||||
/**
|
||||
* Exception thrown to indicate a Builder error.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class BuilderException extends RuntimeException {
|
||||
|
||||
private final String operation;
|
||||
|
||||
private final int statusCode;
|
||||
|
||||
BuilderException(String operation, int statusCode) {
|
||||
super(buildMessage(operation, statusCode));
|
||||
this.operation = operation;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Builder operation that failed.
|
||||
* @return the operation description
|
||||
*/
|
||||
public String getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the status code returned from a Builder operation.
|
||||
* @return the statusCode the status code
|
||||
*/
|
||||
public int getStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
private static String buildMessage(String operation, int statusCode) {
|
||||
StringBuilder message = new StringBuilder("Builder");
|
||||
if (operation != null && !operation.isEmpty()) {
|
||||
message.append(" lifecycle '").append(operation).append("'");
|
||||
}
|
||||
message.append(" failed with status code ").append(statusCode);
|
||||
return message.toString();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2012-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 org.springframework.boot.buildpack.platform.docker.type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import org.springframework.boot.buildpack.platform.json.MappedObject;
|
||||
|
||||
/**
|
||||
* Status details returned from {@code Docker container wait}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class ContainerStatus extends MappedObject {
|
||||
|
||||
private final int statusCode;
|
||||
|
||||
private final String waitingErrorMessage;
|
||||
|
||||
ContainerStatus(int statusCode, String waitingErrorMessage) {
|
||||
super(null, null);
|
||||
this.statusCode = statusCode;
|
||||
this.waitingErrorMessage = waitingErrorMessage;
|
||||
}
|
||||
|
||||
ContainerStatus(JsonNode node) {
|
||||
super(node, MethodHandles.lookup());
|
||||
this.statusCode = valueAt("/StatusCode", Integer.class);
|
||||
this.waitingErrorMessage = valueAt("/Error/Message", String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the container exit status code.
|
||||
* @return the exit status code
|
||||
*/
|
||||
public int getStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a message indicating an error waiting for a container to stop.
|
||||
* @return the waiting error message
|
||||
*/
|
||||
public String getWaitingErrorMessage() {
|
||||
return this.waitingErrorMessage;
|
||||
}
|
||||
|
||||
public static ContainerStatus of(InputStream content) throws IOException {
|
||||
return of(content, ContainerStatus::new);
|
||||
}
|
||||
|
||||
public static ContainerStatus of(int statusCode, String errorMessage) {
|
||||
return new ContainerStatus(statusCode, errorMessage);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-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 org.springframework.boot.buildpack.platform.build;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link BuilderException}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class BuilderExceptionTests {
|
||||
|
||||
@Test
|
||||
void create() {
|
||||
BuilderException exception = new BuilderException("detector", 1);
|
||||
assertThat(exception.getOperation()).isEqualTo("detector");
|
||||
assertThat(exception.getStatusCode()).isEqualTo(1);
|
||||
assertThat(exception.getMessage()).isEqualTo("Builder lifecycle 'detector' failed with status code 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenOperationIsNull() {
|
||||
BuilderException exception = new BuilderException(null, 1);
|
||||
assertThat(exception.getOperation()).isNull();
|
||||
assertThat(exception.getStatusCode()).isEqualTo(1);
|
||||
assertThat(exception.getMessage()).isEqualTo("Builder failed with status code 1");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-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 org.springframework.boot.buildpack.platform.docker.type;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ContainerStatus}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class ContainerStatusTests {
|
||||
|
||||
@Test
|
||||
void ofCreatesFromJson() throws IOException {
|
||||
ContainerStatus status = ContainerStatus.of(getClass().getResourceAsStream("container-status-error.json"));
|
||||
assertThat(status.getStatusCode()).isEqualTo(1);
|
||||
assertThat(status.getWaitingErrorMessage()).isEqualTo("error detail");
|
||||
}
|
||||
|
||||
@Test
|
||||
void ofCreatesFromValues() {
|
||||
ContainerStatus status = ContainerStatus.of(1, "error detail");
|
||||
assertThat(status.getStatusCode()).isEqualTo(1);
|
||||
assertThat(status.getWaitingErrorMessage()).isEqualTo("error detail");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"StatusCode": 1
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
{
|
||||
"StatusCode": 1,
|
||||
"Error": {
|
||||
"Message": "error detail"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue