Browse Source

fix support for additional_contexts with service sub-dependencies

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 months ago
parent
commit
1d34661e91

+ 5 - 1
pkg/compose/build.go

@@ -628,7 +628,11 @@ func parsePlatforms(service types.ServiceConfig) ([]specs.Platform, error) {
 func addBuildDependencies(services []string, project *types.Project) []string {
 	servicesWithDependencies := utils.NewSet(services...)
 	for _, service := range services {
-		b := project.Services[service].Build
+		s, ok := project.Services[service]
+		if !ok {
+			s = project.DisabledServices[service]
+		}
+		b := s.Build
 		if b != nil {
 			for _, target := range b.AdditionalContexts {
 				if s, found := strings.CutPrefix(target, types.ServicePrefix); found {

+ 16 - 0
pkg/e2e/build_test.go

@@ -564,3 +564,19 @@ func TestBuildDependentImage(t *testing.T) {
 	out = res.Combined()
 	assert.Check(t, strings.Contains(out, "secondbuild  Built"))
 }
+
+func TestBuildSubDependencies(t *testing.T) {
+	c := NewParallelCLI(t)
+
+	t.Cleanup(func() {
+		c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/sub-dependencies/compose.yaml", "down", "--rmi=local")
+	})
+
+	res := c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/sub-dependencies/compose.yaml", "build", "main")
+	out := res.Combined()
+	assert.Check(t, strings.Contains(out, "main  Built"))
+
+	res = c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/sub-dependencies/compose.yaml", "up", "--build", "main")
+	out = res.Combined()
+	assert.Check(t, strings.Contains(out, "main  Built"))
+}

+ 36 - 0
pkg/e2e/fixtures/build-test/sub-dependencies/compose.yaml

@@ -0,0 +1,36 @@
+services:
+  main:
+    build:
+      dockerfile_inline: |
+        FROM alpine
+      additional_contexts:
+        dep1: service:dep1
+        dep2: service:dep2
+    entrypoint: ["echo", "Hello from main"]
+
+  dep1:
+    build:
+      dockerfile_inline: |
+        FROM alpine
+      additional_contexts:
+        subdep1: service:subdep1
+        subdep2: service:subdep2
+    entrypoint: ["echo", "Hello from dep1"]
+
+  dep2:
+    build:
+      dockerfile_inline: |
+        FROM alpine
+    entrypoint: ["echo", "Hello from dep2"]
+
+  subdep1:
+    build:
+      dockerfile_inline: |
+        FROM alpine
+    entrypoint: ["echo", "Hello from subdep1"]
+
+  subdep2:
+    build:
+      dockerfile_inline: |
+        FROM alpine
+    entrypoint: ["echo", "Hello from subdep2"]