Skip to content

Commit

Permalink
Merge pull request #9 from grafana/8-use-semantic-versioning
Browse files Browse the repository at this point in the history
feat: use semantic versioning
  • Loading branch information
szkiba authored Oct 9, 2024
2 parents 18275c9 + e288d34 commit 909d786
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 90 deletions.
17 changes: 4 additions & 13 deletions .github/workflows/distro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
# A new release is made if a new version of k6 or one or more of the extensions
# has been released since the last distribution release.
#
# The distro release workflow should be scheduled once a day.
# More than one distribution release cannot be made in one day,
# because the release version is the date.
#
# Usage
# -----
#
Expand All @@ -21,7 +17,7 @@
# ```
# on:
# schedule:
# - cron: "0 0 * * *"
# - cron: "10 */2 * * *"
#
# jobs:
# distro:
Expand Down Expand Up @@ -134,17 +130,12 @@ jobs:
- name: Latest Release Notes
id: latest
run: |
if gh api /repos/${{github.repository}}/releases/tag/$(date +%y.%m.%d) > /dev/null 2>/dev/null; then
echo "today=true" >> "$GITHUB_OUTPUT"
else
if gh api --jq .body /repos/${{github.repository}}/releases/latest > ${{env.NOTES_LATEST}} 2>/dev/null; then
echo "notes=${{env.NOTES_LATEST}}" >> "$GITHUB_OUTPUT"
fi
if gh api --jq .body /repos/${{github.repository}}/releases/latest > ${{env.NOTES_LATEST}} 2>/dev/null; then
echo "notes=${{env.NOTES_LATEST}}" >> "$GITHUB_OUTPUT"
fi
- name: Build Distro
uses: grafana/[email protected]
if: ${{ steps.latest.outputs.today != 'true' }}
uses: grafana/[email protected]
id: build
with:
args: "${{ inputs.args }}"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ k6dist [flags] [registry-location]
```
--distro-name string distro name (default detect)
--distro-version string distro version (default current date in YY.MM.DD format)
--distro-version string distro version (default generated)
--platform strings target platforms (default [linux/amd64,darwin/amd64,windows/amd64])
--executable string executable file name (default "dist/{{.Name}}_{{.OS}}_{{.Arch}}/k6{{.ExeExt}}")
--archive string archive file name (default "dist/{{.Name}}_{{.Version}}_{{.OS}}_{{.Arch}}{{.ZipExt}}")
Expand Down
59 changes: 34 additions & 25 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ import (
"path/filepath"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/grafana/k6dist/internal/registry"
"github.com/grafana/k6foundry"
)

func detectChange(registry registry.Registry, latest string) (bool, error) {
var initialVersion = semver.MustParse("v0.1.0") //nolint:gochecknoglobals

func detectChange(reg registry.Registry, latest string) (bool, *semver.Version, error) {
if len(latest) == 0 {
return true, nil
return true, initialVersion, nil
}

contents, err := os.ReadFile(filepath.Clean(latest)) //nolint:forbidigo
if err != nil {
return false, err
return false, nil, err
}

found, modules, err := parseNotes(contents)
found, version, modules, err := parseNotes(contents)
if err != nil {
return false, err
return false, nil, err
}

if !found {
return false, nil
return true, initialVersion, nil
}

return registry.AddLatest(modules), nil
bumped := reg.AddLatest(modules, version)

return !bumped.Equal(version), bumped, nil
}

func newBuilder(ctx context.Context, modules registry.Modules) (k6foundry.Builder, error) {
Expand All @@ -58,75 +63,79 @@ func newBuilder(ctx context.Context, modules registry.Modules) (k6foundry.Builde
}

// Build builds k6 binaries and archives based on opts parameter.
func Build(ctx context.Context, opts *Options) (bool, error) {
func Build(ctx context.Context, opts *Options) (bool, *semver.Version, error) {
opts.setDefaults()

registry, err := registry.LoadRegistry(ctx, opts.Registry)
if err != nil {
return false, err
return false, nil, err
}

changed, err := detectChange(registry, opts.NotesLatest)
changed, version, err := detectChange(registry, opts.NotesLatest)
if err != nil {
return false, err
return false, nil, err
}

if !changed {
return false, nil
return false, nil, nil
}

if opts.Version != nil {
version = opts.Version
}

notes, err := expandNotes(opts.Name, opts.Version, registry, opts.NotesTemplate)
notes, err := expandNotes(opts.Name, version, registry, opts.NotesTemplate)
if err != nil {
return false, err
return false, nil, err
}

filename, err := expandAsTargetPath("notes", opts.Notes, newInstsanceData(opts.Name, opts.Version, &Platform{}))
filename, err := expandAsTargetPath("notes", opts.Notes, newInstsanceData(opts.Name, version, &Platform{}))
if err != nil {
return false, err
return false, nil, err
}

if err := os.WriteFile(filename, []byte(notes), 0o600); err != nil { //nolint:forbidigo
return false, err
return false, nil, err
}

modules := registry.ToModules()
k6Version, mods := modules.ToFoundry()

builder, err := newBuilder(ctx, modules)
if err != nil {
return false, err
return false, nil, err
}

for _, platform := range opts.Platforms {
data := newInstsanceData(opts.Name, opts.Version, platform)
data := newInstsanceData(opts.Name, version, platform)

filename, err := expandAsTargetPath("executable", opts.Executable, data)
if err != nil {
return false, err
return false, nil, err
}

err = buildExecutable(ctx, builder, platform, k6Version, mods, filename)
if err != nil {
return false, err
return false, nil, err
}

err = createDockerfile(filename, opts.DockerfileTemplate, opts.Dockerfile)
if err != nil {
return false, err
return false, nil, err
}

archive, err := expandAsTargetPath("archive", opts.Archive, data)
if err != nil {
return false, err
return false, nil, err
}

err = buildArchive(archive, filename, opts.Readme, opts.License)
if err != nil {
return false, err
return false, nil, err
}
}

return true, nil
return true, version, nil
}

func buildExecutable(
Expand Down
13 changes: 8 additions & 5 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/google/shlex"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func ghinput(args []string, flag *pflag.Flag) []string {
}

//nolint:forbidigo
func emitOutput(changed bool, version string) error {
func emitOutput(changed bool, version *semver.Version) error {
ghOutput := os.Getenv("GITHUB_OUTPUT")
if len(ghOutput) == 0 {
return nil
Expand All @@ -75,11 +76,13 @@ func emitOutput(changed bool, version string) error {
return err
}

slog.Debug("Emit version", "version", version)
if version != nil {
slog.Debug("Emit version", "version", version.Original())

_, err = fmt.Fprintf(file, "version=%s\n", version)
if err != nil {
return err
_, err = fmt.Fprintf(file, "version=%s\n", version.Original())
if err != nil {
return err
}
}

return file.Close()
Expand Down
34 changes: 17 additions & 17 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"path/filepath"
"runtime"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/grafana/k6dist"
"github.com/spf13/cobra"
)
Expand All @@ -25,6 +25,7 @@ type options struct {
verbose bool
single bool
platforms []string
version string
}

var defaultPlatforms = []string{ //nolint:gochecknoglobals
Expand All @@ -50,12 +51,12 @@ func New(levelVar *slog.LevelVar) *cobra.Command {
return preRun(args, levelVar, opts)
},
RunE: func(cmd *cobra.Command, _ []string) error {
changed, err := run(cmd, opts)
changed, version, err := run(cmd, opts)
if err != nil {
return err
}

return emitOutput(changed, opts.Version)
return emitOutput(changed, version)
},
}

Expand All @@ -66,7 +67,7 @@ func New(levelVar *slog.LevelVar) *cobra.Command {
flags.SortFlags = false

flags.StringVar(&opts.Name, "distro-name", "", "distro name (default detect)")
flags.StringVar(&opts.Version, "distro-version", "", "distro version (default current date in YY.MM.DD format)")
flags.StringVar(&opts.version, "distro-version", "", "distro version (default generated)")
flags.StringSliceVar(&opts.platforms, "platform", defaultPlatforms, "target platforms")
flags.StringVar(&opts.Executable, "executable", k6dist.DefaultExecutableTemplate, "executable file name")
flags.StringVar(&opts.Archive, "archive", k6dist.DefaultArchiveTemplate, "archive file name")
Expand Down Expand Up @@ -107,8 +108,13 @@ func preRun(args []string, levelVar *slog.LevelVar, opts *options) error {
opts.Name = guessName(opts.Registry)
}

if len(opts.Version) == 0 {
opts.Version = defaultDate()
if len(opts.version) > 0 {
ver, err := semver.NewVersion(opts.version)
if err != nil {
return err
}

opts.Version = ver
}

if len(opts.Readme) == 0 {
Expand All @@ -133,22 +139,22 @@ func preRun(args []string, levelVar *slog.LevelVar, opts *options) error {
return nil
}

func run(_ *cobra.Command, opts *options) (bool, error) {
func run(_ *cobra.Command, opts *options) (bool, *semver.Version, error) {
if len(opts.Name) == 0 {
cwd, err := os.Getwd() //nolint:forbidigo
if err != nil {
return false, err
return false, nil, err
}

opts.Name = filepath.Base(cwd)
}

changed, err := k6dist.Build(context.TODO(), &opts.Options)
changed, version, err := k6dist.Build(context.TODO(), &opts.Options)
if err != nil {
return false, err
return false, nil, err
}

return changed, nil
return changed, version, nil
}

func guessName(source string) string {
Expand Down Expand Up @@ -196,12 +202,6 @@ func findLicense() string {
return findTextFile("LICENSE")
}

func defaultDate() string {
now := time.Now()

return now.Format("06.01.02")
}

func parsePlatforms(values []string) ([]*k6dist.Platform, error) {
platforms := make([]*k6dist.Platform, 0, len(values))

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/grafana/k6dist
go 1.22.4

require (
github.com/Masterminds/semver/v3 v3.3.0
github.com/go-task/slim-sprig/v3 v3.0.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/grafana/clireadme v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
Loading

0 comments on commit 909d786

Please sign in to comment.