From 2bf06d1498e2f5edc474300d66681c7314b90a92 Mon Sep 17 00:00:00 2001 From: Cedric Lamalle Date: Thu, 15 Jan 2026 08:28:53 -0300 Subject: [PATCH] Adjust Maven pipeline for new build way * The project version is now set dynamically * Standards opencontainer labels are now added (created, revision, version) --- maven/dagger.json | 5 ++-- maven/go.mod | 4 +-- maven/go.sum | 8 +++--- maven/main.go | 71 +++++++++++++++++++++++++++-------------------- maven/models.go | 52 ++++++++++++++-------------------- 5 files changed, 71 insertions(+), 69 deletions(-) diff --git a/maven/dagger.json b/maven/dagger.json index 5a73e65..e5f7b74 100644 --- a/maven/dagger.json +++ b/maven/dagger.json @@ -1,7 +1,8 @@ { "name": "maven", - "engineVersion": "v0.18.19", + "engineVersion": "v0.19.9", "sdk": { "source": "go" - } + }, + "disableDefaultFunctionCaching": true } diff --git a/maven/go.mod b/maven/go.mod index 41346f5..8eabc05 100644 --- a/maven/go.mod +++ b/maven/go.mod @@ -3,7 +3,7 @@ module dagger/maven go 1.24.3 require ( - github.com/99designs/gqlgen v0.17.79 + github.com/99designs/gqlgen v0.17.81 github.com/Khan/genqlient v0.8.1 github.com/vektah/gqlparser/v2 v2.5.30 go.opentelemetry.io/otel v1.38.0 @@ -21,7 +21,7 @@ require ( go.opentelemetry.io/otel/trace v1.38.0 go.opentelemetry.io/proto/otlp v1.8.0 golang.org/x/sync v0.17.0 - google.golang.org/grpc v1.75.1 + google.golang.org/grpc v1.76.0 ) require ( diff --git a/maven/go.sum b/maven/go.sum index 4cf9d6c..03dea85 100644 --- a/maven/go.sum +++ b/maven/go.sum @@ -1,5 +1,5 @@ -github.com/99designs/gqlgen v0.17.79 h1:RTsJZtdzcfROeWdt42NGMIabIbiBn69YyVmLEAuxtnA= -github.com/99designs/gqlgen v0.17.79/go.mod h1:vgNcZlLwemsUhYim4dC1pvFP5FX0pr2Y+uYUoHFb1ig= +github.com/99designs/gqlgen v0.17.81 h1:kCkN/xVyRb5rEQpuwOHRTYq83i0IuTQg9vdIiwEerTs= +github.com/99designs/gqlgen v0.17.81/go.mod h1:vgNcZlLwemsUhYim4dC1pvFP5FX0pr2Y+uYUoHFb1ig= github.com/Khan/genqlient v0.8.1 h1:wtOCc8N9rNynRLXN3k3CnfzheCUNKBcvXmVv5zt6WCs= github.com/Khan/genqlient v0.8.1/go.mod h1:R2G6DzjBvCbhjsEajfRjbWdVglSH/73kSivC9TLWVjU= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= @@ -81,8 +81,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20250825161204-c5933d9347a5 h1: google.golang.org/genproto/googleapis/api v0.0.0-20250825161204-c5933d9347a5/go.mod h1:j3QtIyytwqGr1JUDtYXwtMXWPKsEa5LtzIFN1Wn5WvE= google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5 h1:eaY8u2EuxbRv7c3NiGK0/NedzVsCcV6hDuU5qPX5EGE= google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5/go.mod h1:M4/wBTSeyLxupu3W3tJtOgB14jILAS/XWPSSa3TAlJc= -google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI= -google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ= +google.golang.org/grpc v1.76.0 h1:UnVkv1+uMLYXoIz6o7chp59WfQUYA2ex/BXQ9rHZu7A= +google.golang.org/grpc v1.76.0/go.mod h1:Ju12QI8M6iQJtbcsV+awF5a4hfJMLi4X0JLo94ULZ6c= google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/maven/main.go b/maven/main.go index ff316f7..d273461 100644 --- a/maven/main.go +++ b/maven/main.go @@ -5,12 +5,17 @@ import ( "context" "dagger/maven/internal/dagger" "fmt" + "time" ) // FullBuildModules orchestrates build, test, Sonar analysis, and image publishing for each module in order. func (m *Maven) FullBuildModules( ctx context.Context, source *dagger.Directory, + // Digest do commit atual (sha completo) + commitSha string, + // Versão da aplicação no formato CalVer.BuildNumber + version string, modules []string, // +optional sonarConfig *SonarConfig, @@ -18,7 +23,7 @@ func (m *Maven) FullBuildModules( dockerConfig *DockerBuildConfig) ([]*ModuleBuildResult, error) { results := make([]*ModuleBuildResult, 0) for _, module := range modules { - result, err := m.FullBuild(ctx, source, module, sonarConfig, dockerConfig) + result, err := m.FullBuild(ctx, source, module, commitSha, version, sonarConfig, dockerConfig) if err != nil { return nil, err } @@ -30,11 +35,23 @@ func (m *Maven) FullBuildModules( // FullBuild executes the three-stage pipeline (build/test, Sonar, Docker publish) for a single Maven module. func (m *Maven) FullBuild(ctx context.Context, source *dagger.Directory, + // Module name module string, + // Digest do commit atual (sha completo) + commitSha string, + // Versão da aplicação no formato CalVer.BuildNumber + version string, + // +optional sonarConfig *SonarConfig, + // +optional dockerConfig *DockerBuildConfig) (*ModuleBuildResult, error) { stages := []PipelineStage{ + { + DisplayName: "Set version", + Goals: []string{"versions:set"}, + Options: []string{"-DnewVersion=" + version, "-DgenerateBackupPoms=false"}, + }, { DisplayName: "Build and Test", Goals: []string{"clean", "verify"}, @@ -51,15 +68,15 @@ func (m *Maven) FullBuild(ctx context.Context, imageUrl := "" if dockerConfig != nil { - dockerStage, image, err := m.configureDockerPublish(ctx, source, module, dockerConfig) + dockerStage, err := m.configureDockerPublish(ctx, dockerConfig, commitSha, version) if err != nil { return nil, err } stages = append(stages, dockerStage) - imageUrl = image + imageUrl = dockerConfig.fullImageReference() } - buildResult, err := m.executeStages(ctx, source.Directory(module), module, stages) + buildResult, err := m.executeStages(ctx, source, module, stages) if err != nil { return nil, err } @@ -69,23 +86,20 @@ func (m *Maven) FullBuild(ctx context.Context, func (m *Maven) configureDockerPublish( ctx context.Context, - source *dagger.Directory, - module string, - dockerConfig *DockerBuildConfig) (PipelineStage, string, error) { - moduleDockerConfig := *dockerConfig - // TODO Vamos ter que passar - moduleDockerConfig.Image = module - // TODO Vamos ter que passar o commitSha aqui - moduleDockerConfig.Tag = m.GetVersionOrDefault(ctx, source, "latest") - dockerOptions, err := m.buildDockerOptions(ctx, &moduleDockerConfig) + dockerConfig *DockerBuildConfig, + // Digest do commit atual (sha completo) + commitSha string, + // Versão da aplicação no formato CalVer.BuildNumber + version string) (PipelineStage, error) { + dockerOptions, err := m.buildDockerOptions(ctx, dockerConfig, commitSha, version) if err != nil { - return PipelineStage{}, "", err + return PipelineStage{}, err } return PipelineStage{ DisplayName: "Docker Build and Push", Goals: []string{"jib:build"}, Options: dockerOptions, - }, moduleDockerConfig.imageReference("latest"), nil + }, nil } func (m *Maven) configureSonar(ctx context.Context, sonarConfig *SonarConfig, module string) (PipelineStage, error) { @@ -155,25 +169,24 @@ func (m *Maven) executeStage( } // buildDockerOptions materializes the Maven command-line arguments needed to run the Jib plugin. -func (m *Maven) buildDockerOptions(ctx context.Context, config *DockerBuildConfig) ([]string, error) { +func (m *Maven) buildDockerOptions( + ctx context.Context, + config *DockerBuildConfig, + // Digest do commit atual (sha completo) + commitSha string, + // Versão da aplicação no formato CalVer.BuildNumber + version string) ([]string, error) { if config == nil { return nil, nil } - var options []string - - imageURL := config.imageReference("latest") - - if imageURL != "" { - options = append(options, fmt.Sprintf("-Djib.to.image=%s", imageURL)) - // Separar Tag + options = append(options, fmt.Sprintf("-Djib.to.image=%s", config.Image)) + if config.Tag != "" { options = append(options, fmt.Sprintf("-Djib.to.tag=%s", config.Tag)) } - if config.Username != "" { options = append(options, fmt.Sprintf("-Djib.to.auth.username=%s", config.Username)) } - if config.PasswordSecret != nil { password, err := config.PasswordSecret.Plaintext(ctx) if err != nil { @@ -181,10 +194,8 @@ func (m *Maven) buildDockerOptions(ctx context.Context, config *DockerBuildConfi } options = append(options, fmt.Sprintf("-Djib.to.auth.password=%s", password)) } - - if config.Options != nil { - options = append(options, config.Options...) - } - + created := time.Now().Format(time.RFC3339) + options = append(options, fmt.Sprintf("-Djib.container.labels=org.opencontainers.image.revision=%s,"+ + "org.opencontainers.image.version=%s,org.opencontainers.image.created=%s", commitSha, version, created)) return options, nil } diff --git a/maven/models.go b/maven/models.go index 80ac584..d63319d 100644 --- a/maven/models.go +++ b/maven/models.go @@ -34,18 +34,22 @@ type PipelineStage struct { // DockerBuildConfig contains the information required to execute the Jib Maven plugin and push images. type DockerBuildConfig struct { - // Remover - Registry string - // Remover - Group string - // Vai conter nome base sem tag: registry/group/name + // Image contains the image name without tag, for instance: registry/group/name Image string - // Pode ser um array de string - Tag string - Username string + // Tag image tag + Tag string + // Username to connect to a private registry + Username string + // Password for the user to connect to a private registry PasswordSecret *dagger.Secret - Options []string - Labels map[string]string +} + +func (r DockerBuildConfig) fullImageReference() string { + ref := r.Image + if r.Tag != "" { + ref = fmt.Sprintf("%s:%s", ref, r.Tag) + } + return ref } // SonarConfig stores the data required to invoke SonarQube analysis for a module. @@ -58,13 +62,16 @@ type SonarConfig struct { } // NewDockerBuildConfig creates a DockerBuildConfig tailored for Maven builds. -func (m *Maven) NewDockerBuildConfig(registry, group, username string, passwordSecret *dagger.Secret, options []string) DockerBuildConfig { +func (m *Maven) NewDockerBuildConfig( + image string, + tag string, + username string, + passwordSecret *dagger.Secret) DockerBuildConfig { return DockerBuildConfig{ - Registry: registry, - Group: group, + Image: image, + Tag: tag, Username: username, PasswordSecret: passwordSecret, - Options: options, } } @@ -83,20 +90,3 @@ func (m *Maven) NewSonarConfig(host string, tokenSecret *dagger.Secret, waitForQ ExtraOptions: extraOptions, }, nil } - -func (c *DockerBuildConfig) imageReference(defaultTag string) string { - ref := c.Registry - if c.Group != "" { - ref = fmt.Sprintf("%s/%s", ref, c.Group) - } - if c.Image != "" { - ref = fmt.Sprintf("%s/%s", ref, c.Image) - } - tag := c.Tag - if tag == "" { - tag = defaultTag - } - return fmt.Sprintf("%s:%s", ref, tag) -} - -//type WithContainerFunc func(r *Container) *Container