Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

ci: remove optional from dagger code #22

Merged
merged 1 commit into from
Jan 14, 2024
Merged
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
42 changes: 26 additions & 16 deletions ci/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ func (m *Build) containerImages(version string) []*Container {
variants := make([]*Container, 0, len(platforms))

for _, platform := range platforms {
variants = append(variants, m.containerImage(platform, Opt(version)))
variants = append(variants, m.containerImage(platform, version))
}

return variants
}

// Build a container image.
func (m *Build) ContainerImage(
// Platform in the format of OS/ARCH[/VARIANT] (eg. "darwin/arm64/v7")
platform Optional[Platform],
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
// +optional
platform Platform,
) *Container {
return m.containerImage(platform.GetOr(""), OptEmpty[string]())
return m.containerImage(platform, "")
}

func (m *Build) containerImage(platform Platform, version Optional[string]) *Container {
func (m *Build) containerImage(platform Platform, version string) *Container {
return dag.Container(ContainerOpts{Platform: platform}).
From(alpineBaseImage).
WithLabel("org.opencontainers.image.title", "benthos-openmeter").
Expand All @@ -50,8 +51,8 @@ func (m *Build) containerImage(platform Platform, version Optional[string]) *Con
WithLabel("org.opencontainers.image.source", "https://github.com/openmeterio/benthos-openmeter").
WithLabel("org.opencontainers.image.licenses", "Apache-2.0").
With(func(c *Container) *Container {
if v, ok := version.Get(); ok {
c = c.WithLabel("org.opencontainers.image.version", v)
if version != "" {
c = c.WithLabel("org.opencontainers.image.version", version)
}

return c
Expand All @@ -67,13 +68,18 @@ func (m *Build) containerImage(platform Platform, version Optional[string]) *Con

// Build a binary.
func (m *Build) Binary(
// Platform in the format of OS/ARCH[/VARIANT] (eg. "darwin/arm64/v7")
platform Optional[Platform],
// Target platform in "[os]/[platform]/[version]" format (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").
// +optional
platform Platform,
) *File {
return m.binary(platform.GetOr(""), OptEmpty[string]())
return m.binary(platform, "")
}

func (m *Build) binary(platform Platform, version Optional[string]) *File {
func (m *Build) binary(platform Platform, version string) *File {
if version == "" {
version = "unknown"
}

return dag.Go(GoOpts{
Version: goVersion,
}).
Expand All @@ -84,19 +90,23 @@ func (m *Build) binary(platform Platform, version Optional[string]) *File {
Trimpath: true,
RawArgs: []string{
"-ldflags",
"-s -w -X main.version=" + version.GetOr("unknown"),
"-s -w -X main.version=" + version,
},
})
}

func (m *Build) HelmChart(version Optional[string]) *File {
func (m *Build) HelmChart(
// Release version.
// +optional
version string,
) *File {
chart := helmChartDir(m.Source)

var opts HelmPackageOpts

if v, ok := version.Get(); ok {
opts.Version = strings.TrimPrefix(v, "v")
opts.AppVersion = v
if version != "" {
opts.Version = strings.TrimPrefix(version, "v")
opts.AppVersion = version
}

return dag.Helm(HelmOpts{Version: helmVersion}).Package(chart, opts)
Expand Down
11 changes: 8 additions & 3 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (m *Ci) Ci(ctx context.Context) error {

// TODO: run trivy scan on helm chart
group.Go(func() error {
_, err := m.Build().HelmChart(Opt("0.0.0")).Sync(ctx)
_, err := m.Build().HelmChart("0.0.0").Sync(ctx)

return err
})
Expand All @@ -116,7 +116,12 @@ func (m *Ci) Snapshot(ctx context.Context) error {
}

// Build and publish all release artifacts.
func (m *Ci) Release(ctx context.Context, version string) error {
func (m *Ci) Release(
ctx context.Context,

// Release version.
version string,
) error {
var group errgroup.Group

group.Go(func() error {
Expand All @@ -138,7 +143,7 @@ func (m *Ci) Release(ctx context.Context, version string) error {
return errors.New("registry password is required to push helm charts to ghcr.io")
}

chart := m.Build().HelmChart(Opt(version))
chart := m.Build().HelmChart(version)

_, err := dag.Helm(HelmOpts{Version: helmVersion}).
Login("ghcr.io", username, password).
Expand Down