Skip to content

Commit

Permalink
Add ability to specify root context directory when parsing buildfile (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
loosebazooka authored Oct 5, 2020
1 parent 7d29959 commit 9f5aabc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static BuildFileSpec toBuildFileSpec(
* Read a buildfile from disk and generate a JibContainerBuilder instance. All parsing of files
* considers the directory the buildfile is located in as the working directory.
*
* @param projectRoot the root context directory of this build
* @param buildFilePath a file containing the build definition
* @param templateParameters a map of templating variables to apply on the file before parsing
* @return a {@link JibContainerBuilder} generated from the contents of {@code buildFilePath}
Expand All @@ -59,10 +60,9 @@ private static BuildFileSpec toBuildFileSpec(
* @throws InvalidImageReferenceException if the baseImage reference can not be parsed
*/
public static JibContainerBuilder toJibContainerBuilder(
Path buildFilePath, Map<String, String> templateParameters)
Path projectRoot, Path buildFilePath, Map<String, String> templateParameters)
throws InvalidImageReferenceException, IOException {
BuildFileSpec buildFile = toBuildFileSpec(buildFilePath, templateParameters);
Path projectRoot = buildFilePath.toAbsolutePath().getParent();

JibContainerBuilder containerBuilder;
if (buildFile.getFrom().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
Expand All @@ -50,10 +49,11 @@ public class BuildFilesTest {
@Test
public void testToJibContainerBuilder_allProperties()
throws URISyntaxException, IOException, InvalidImageReferenceException {
URL resource = Resources.getResource("buildfiles/projects/allProperties/jib.yaml");
Path buildfile =
Paths.get(Resources.getResource("buildfiles/projects/allProperties/jib.yaml").toURI());
Path projectRoot = buildfile.getParent();
JibContainerBuilder jibContainerBuilder =
BuildFiles.toJibContainerBuilder(Paths.get(resource.toURI()), ImmutableMap.of());
Path projectRoot = Paths.get(resource.toURI()).getParent();
BuildFiles.toJibContainerBuilder(projectRoot, buildfile, ImmutableMap.of());

ContainerBuildPlan resolved = jibContainerBuilder.toContainerBuildPlan();
Assert.assertEquals("ubuntu", resolved.getBaseImage());
Expand Down Expand Up @@ -90,9 +90,10 @@ public void testToJibContainerBuilder_allProperties()
@Test
public void testToJibContainerBuilder_requiredProperties()
throws URISyntaxException, IOException, InvalidImageReferenceException {
URL resource = Resources.getResource("buildfiles/projects/allDefaults/jib.yaml");
Path buildfile =
Paths.get(Resources.getResource("buildfiles/projects/allDefaults/jib.yaml").toURI());
JibContainerBuilder jibContainerBuilder =
BuildFiles.toJibContainerBuilder(Paths.get(resource.toURI()), ImmutableMap.of());
BuildFiles.toJibContainerBuilder(buildfile.getParent(), buildfile, ImmutableMap.of());

ContainerBuildPlan resolved = jibContainerBuilder.toContainerBuildPlan();
Assert.assertEquals("scratch", resolved.getBaseImage());
Expand All @@ -112,11 +113,13 @@ public void testToJibContainerBuilder_requiredProperties()
@Test
public void testToBuildFileSpec_withTemplating()
throws URISyntaxException, InvalidImageReferenceException, IOException {
URL resource = Resources.getResource("buildfiles/projects/templating/valid.yaml");
Path buildfile =
Paths.get(Resources.getResource("buildfiles/projects/templating/valid.yaml").toURI());

JibContainerBuilder jibContainerBuilder =
BuildFiles.toJibContainerBuilder(
Paths.get(resource.toURI()),
buildfile.getParent(),
buildfile,
ImmutableMap.of(
"unused", "ignored", // keys that are defined but not used do not throw an error
"key", "templateKey",
Expand All @@ -139,10 +142,11 @@ public void testToBuildFileSpec_withTemplating()
@Test
public void testToBuildFileSpec_failWithMissingTemplateVariable()
throws URISyntaxException, InvalidImageReferenceException, IOException {
URL resource = Resources.getResource("buildfiles/projects/templating/missingVar.yaml");
Path buildfile =
Paths.get(Resources.getResource("buildfiles/projects/templating/missingVar.yaml").toURI());

try {
BuildFiles.toJibContainerBuilder(Paths.get(resource.toURI()), ImmutableMap.of());
BuildFiles.toJibContainerBuilder(buildfile.getParent(), buildfile, ImmutableMap.of());
Assert.fail();
} catch (IllegalArgumentException iae) {
MatcherAssert.assertThat(
Expand All @@ -153,13 +157,36 @@ public void testToBuildFileSpec_failWithMissingTemplateVariable()
@Test
public void testToBuildFileSpec_templateMultiLineBehavior()
throws URISyntaxException, InvalidImageReferenceException, IOException {
URL resource = Resources.getResource("buildfiles/projects/templating/multiLine.yaml");
Path buildfile =
Paths.get(Resources.getResource("buildfiles/projects/templating/multiLine.yaml").toURI());

JibContainerBuilder jibContainerBuilder =
BuildFiles.toJibContainerBuilder(
Paths.get(resource.toURI()),
buildfile.getParent(),
buildfile,
ImmutableMap.of("replace" + System.lineSeparator() + "this", "creationTime: 1234"));
ContainerBuildPlan resolved = jibContainerBuilder.toContainerBuildPlan();
Assert.assertEquals(Instant.ofEpochMilli(1234), resolved.getCreationTime());
}

@Test
public void testToBuildFileSpec_alternativeRootContext()
throws URISyntaxException, InvalidImageReferenceException, IOException {
Path buildfile =
Paths.get(
Resources.getResource("buildfiles/projects/allProperties/altYamls/alt-jib.yaml")
.toURI());
Path projectRoot = buildfile.getParent().getParent();
JibContainerBuilder jibContainerBuilder =
BuildFiles.toJibContainerBuilder(projectRoot, buildfile, ImmutableMap.of());

ContainerBuildPlan resolved = jibContainerBuilder.toContainerBuildPlan();
Assert.assertEquals(
FileEntriesLayer.builder()
.addEntry(
projectRoot.resolve("project/script.sh"), AbsoluteUnixPath.get("/home/script.sh"))
.build()
.getEntries(),
((FileEntriesLayer) resolved.getLayers().get(0)).getEntries());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# this buildfile doesn't necessarily work, just useful for testing parsing and translation
apiVersion: jib/v1alpha1
kind: BuildFile

# "FROM" with detail for manifest lists or multiple architectures
from:
image: "ubuntu"
# optional: if missing, then defaults to `linux/amd64`
platforms:
- architecture: "arm"
os: "linux"
- architecture: "amd64"
os: "darwin"

# potentially simple form of "FROM" (based on ability to define schema)
# from: "gcr.io/distroless/java:8"

creationTime: 2000 # millis since epoch or iso8601 creation time
format: OCI # Docker or OCI

environment:
"KEY1": "v1"
"KEY2": "v2"
labels:
"label1": "l1"
"label2": "l2"
volumes:
- "/volume1"
- "/volume2"

exposedPorts:
- "123/udp"
- "456"
- "789/tcp"

user: "customUser"
workingDirectory: "/home"
entrypoint:
- "sh"
- "script.sh"
cmd:
- "--param"
- "param"

layers:
entries:
- name: "scripts"
files:
- src: "project/script.sh"
dest: "/home/script.sh"

0 comments on commit 9f5aabc

Please sign in to comment.