Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions maven/dagger.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "maven",
"engineVersion": "v0.18.19",
"engineVersion": "v0.19.9",
"sdk": {
"source": "go"
}
},
"disableDefaultFunctionCaching": true
}
4 changes: 2 additions & 2 deletions maven/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down
8 changes: 4 additions & 4 deletions maven/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
71 changes: 41 additions & 30 deletions maven/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ 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,
// +optional
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
}
Expand All @@ -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"},
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -155,36 +169,33 @@ 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 {
return nil, fmt.Errorf("error getting registry password: %w", err)
}
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
}
52 changes: 21 additions & 31 deletions maven/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
}

Expand All @@ -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