feat: process image templates in compose services, set default git-based tags if not specified

This commit is contained in:
Pasha Sviderski
2025-11-01 16:06:52 +10:00
parent 328ace1fd1
commit 26c3699a9e
3 changed files with 19 additions and 4 deletions
+5
View File
@@ -53,6 +53,11 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
return nil, err
}
// Process image templates in services to expand Go template expressions using git repo state.
if project, err = ProcessImageTemplates(project); err != nil {
return nil, err
}
return project, nil
}
+7 -1
View File
@@ -20,7 +20,8 @@ import (
"github.com/stretchr/testify/require"
)
// loadProjectFromContent loads a compose project from YAML content
// loadProjectFromContent loads a compose project from YAML content.
// Keep the implementation in sync with LoadProject.
func loadProjectFromContent(t *testing.T, content string) (*types.Project, error) {
t.Helper()
ctx := context.Background()
@@ -62,6 +63,11 @@ func loadProjectFromContent(t *testing.T, content string) (*types.Project, error
return nil, err
}
// Process image templates in services to expand Go template expressions using git repo state.
if project, err = ProcessImageTemplates(project); err != nil {
return nil, err
}
return project, nil
}
+7 -3
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"path"
"strconv"
"strings"
"testing"
composecli "github.com/compose-spec/compose-go/v2/cli"
@@ -99,7 +100,7 @@ func TestComposeBuild(t *testing.T) {
require.NoError(t, err)
servicesToBuild, err := cliInternal.ServicesThatNeedBuild(project, nil, false)
require.NoError(t, err)
serviceImage1 := fmt.Sprintf("127.0.0.1:%d/service-first", registryHostPort)
serviceImage1 := project.Services["service-first"].Image // contains auto-generated default tag
serviceImage2 := fmt.Sprintf("127.0.0.1:%d/service-second:version2", registryHostPort)
t.Cleanup(func() {
// Remove the images after the test
@@ -146,12 +147,15 @@ func TestComposeBuild(t *testing.T) {
cli.BuildServices(context.Background(), servicesToBuild, buildOpts)
// Check the image of the first service
ref1, err := name.NewRepository(fmt.Sprintf("127.0.0.1:%d/service-first", registryHostPort))
tagSeparatorIdx := strings.LastIndex(serviceImage1, ":")
serviceRepo1, serviceTag1 := serviceImage1[:tagSeparatorIdx], serviceImage1[tagSeparatorIdx+1:]
ref1, err := name.NewRepository(serviceRepo1)
require.NoError(t, err)
tags, err := remote.List(ref1)
require.NoError(t, err)
assert.Equal(t, tags, []string{"latest"}, "Tags for service service-first do not match")
assert.Equal(t, tags, []string{serviceTag1}, "Tags for service service-first do not match")
// Check the image of the second service
ref2, err := name.NewRepository(fmt.Sprintf("127.0.0.1:%d/service-second", registryHostPort))