mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	bake: support compose service as build context
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
		@@ -3,7 +3,6 @@ package bake
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"maps"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"slices"
 | 
						"slices"
 | 
				
			||||||
@@ -92,7 +91,12 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf
 | 
				
			|||||||
			var additionalContexts map[string]string
 | 
								var additionalContexts map[string]string
 | 
				
			||||||
			if s.Build.AdditionalContexts != nil {
 | 
								if s.Build.AdditionalContexts != nil {
 | 
				
			||||||
				additionalContexts = map[string]string{}
 | 
									additionalContexts = map[string]string{}
 | 
				
			||||||
				maps.Copy(additionalContexts, s.Build.AdditionalContexts)
 | 
									for k, v := range s.Build.AdditionalContexts {
 | 
				
			||||||
 | 
										if strings.HasPrefix(v, "service:") {
 | 
				
			||||||
 | 
											v = strings.Replace(v, "service:", "target:", 1)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										additionalContexts[k] = v
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			var shmSize *string
 | 
								var shmSize *string
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -798,6 +798,37 @@ services:
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestServiceContext(t *testing.T) {
 | 
				
			||||||
 | 
						dt := []byte(`
 | 
				
			||||||
 | 
					services:
 | 
				
			||||||
 | 
					  base:
 | 
				
			||||||
 | 
					    build:
 | 
				
			||||||
 | 
					      dockerfile: baseapp.Dockerfile
 | 
				
			||||||
 | 
					    command: ./entrypoint.sh
 | 
				
			||||||
 | 
					  webapp:
 | 
				
			||||||
 | 
					    build:
 | 
				
			||||||
 | 
					      context: ./dir
 | 
				
			||||||
 | 
					      additional_contexts:
 | 
				
			||||||
 | 
					        base: service:base
 | 
				
			||||||
 | 
					`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						c, err := ParseCompose([]composetypes.ConfigFile{{Content: dt}}, nil)
 | 
				
			||||||
 | 
						require.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						require.Equal(t, 1, len(c.Groups))
 | 
				
			||||||
 | 
						require.Equal(t, "default", c.Groups[0].Name)
 | 
				
			||||||
 | 
						sort.Strings(c.Groups[0].Targets)
 | 
				
			||||||
 | 
						require.Equal(t, []string{"base", "webapp"}, c.Groups[0].Targets)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						require.Equal(t, 2, len(c.Targets))
 | 
				
			||||||
 | 
						sort.Slice(c.Targets, func(i, j int) bool {
 | 
				
			||||||
 | 
							return c.Targets[i].Name < c.Targets[j].Name
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						require.Equal(t, "webapp", c.Targets[1].Name)
 | 
				
			||||||
 | 
						require.Equal(t, map[string]string{"base": "target:base"}, c.Targets[1].Contexts)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// chdir changes the current working directory to the named directory,
 | 
					// chdir changes the current working directory to the named directory,
 | 
				
			||||||
// and then restore the original working directory at the end of the test.
 | 
					// and then restore the original working directory at the end of the test.
 | 
				
			||||||
func chdir(t *testing.T, dir string) {
 | 
					func chdir(t *testing.T, dir string) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user