diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 972bf0d..4d64dec 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,7 +1,5 @@ project_name: k6registry version: 2 -env: - - IMAGE_OWNER=ghcr.io/grafana before: hooks: - go mod tidy @@ -9,7 +7,7 @@ dist: build/dist builds: - env: - CGO_ENABLED=0 - goos: ["darwin", "linux", "windows"] + goos: ["linux", "windows"] goarch: ["amd64", "arm64"] ldflags: - "-s -w -X main.version={{.Version}} -X main.appname={{.ProjectName}}" @@ -39,61 +37,3 @@ changelog: - "^chore:" - "^docs:" - "^test:" - -dockers: - - id: amd64 - dockerfile: Dockerfile.goreleaser - use: buildx - image_templates: - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-amd64" - - build_flag_templates: - - "--platform=linux/amd64" - - "--pull" - - "--label=org.opencontainers.image.created={{.Date}}" - - "--label=org.opencontainers.image.title={{.ProjectName}}" - - "--label=org.opencontainers.image.revision={{.FullCommit}}" - - "--label=org.opencontainers.image.version={{.Version}}" - - "--label=org.opencontainers.image.licenses=AGPL-3.0-only" - - id: arm64 - dockerfile: Dockerfile.goreleaser - use: buildx - image_templates: - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-arm64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-arm64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-arm64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-arm64" - - build_flag_templates: - - "--platform=linux/arm64" - - "--pull" - - "--label=org.opencontainers.image.created={{.Date}}" - - "--label=org.opencontainers.image.title={{.ProjectName}}" - - "--label=org.opencontainers.image.revision={{.FullCommit}}" - - "--label=org.opencontainers.image.version={{.Version}}" - - "--label=org.opencontainers.image.licenses=AGPL-3.0-only" - -docker_manifests: - - id: tag - name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}" - image_templates: - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-arm64" - - id: major - name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}" - image_templates: - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-arm64" - - id: major-minor - name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}" - image_templates: - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-arm64" - - id: latest - name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest" - image_templates: - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-amd64" - - "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-arm64" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8adc6b6..991d852 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -127,17 +127,6 @@ goreleaser build --snapshot --clean --single-target -o k6registry [snapshot]: <#snapshot---creating-an-executable-binary-with-a-snapshot-version> -### docker - Build docker image - -Building a Docker image. Before building the image, it is advisable to perform a snapshot build using goreleaser. To build the image, it is advisable to use the same `Docker.goreleaser` file that `goreleaser` uses during release. - -Requires -: snapshot - -```bash -docker build -t k6registry -f Dockerfile.goreleaser . -``` - ### clean - Delete the build directory ```bash diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser deleted file mode 100644 index e1bd890..0000000 --- a/Dockerfile.goreleaser +++ /dev/null @@ -1,10 +0,0 @@ -FROM golang:1.23.3-alpine3.20 - -RUN addgroup -g 127 -S docker && adduser -D -u 1001 -G docker runner && \ - apk update && apk add git - -COPY k6registry /usr/bin/ - -USER runner - -ENTRYPOINT ["k6registry"] diff --git a/action.yml b/action.yml deleted file mode 100644 index eb05bf9..0000000 --- a/action.yml +++ /dev/null @@ -1,64 +0,0 @@ -name: k6registry -description: k6 extension registry generator -author: Grafana Labs - -branding: - icon: settings - color: purple - -inputs: - in: - description: input file name - required: true - - out: - description: output file name - required: false - - api: - description: api directory name - required: false - - quiet: - description: no output, only validation - required: false - - verbose: - description: verbose logging - required: false - - loose: - description: skip JSON schema validation - required: false - - lint: - description: enable built-in linter - required: false - - compact: - description: compact instead of pretty-printed output - required: false - - catalog: - description: generate catalog to the specified file - required: false - - origin: - description: external registry URL for default values - required: false - - ref: - description: reference output URL for change detection - required: false - - test: - description: api path(s) to test after generation - required: false - -outputs: - changed: - description: "true if the output has changed compared to ref" - -runs: - using: docker - image: docker://ghcr.io/grafana/k6registry:v0 diff --git a/cmd/k6registry/action.go b/cmd/action.go similarity index 60% rename from cmd/k6registry/action.go rename to cmd/action.go index f5df8aa..5ae195b 100644 --- a/cmd/k6registry/action.go +++ b/cmd/action.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "bytes" @@ -9,27 +9,16 @@ import ( "net/http" "os" "path/filepath" - "time" ) //nolint:forbidigo -func emitOutput() error { - out := getenv("INPUT_OUT", "") - if len(out) == 0 { - api := getenv("INPUT_API", "") - if len(api) == 0 { - return nil - } - - out = filepath.Join(api, "registry.json") - } - - ref := getenv("INPUT_REF", "") - if len(ref) == 0 { - return nil - } +func isGitHubAction() bool { + return os.Getenv("GITHUB_ACTIONS") == "true" +} - ghOutput := getenv("GITHUB_OUTPUT", "") +//nolint:forbidigo +func emitOutput(ctx context.Context, out string, ref string) error { + ghOutput := os.Getenv("GITHUB_OUTPUT") if len(ghOutput) == 0 { return nil } @@ -39,7 +28,7 @@ func emitOutput() error { return err } - changed := isChanged(ref, out) + changed := isChanged(ctx, ref, out) slog.Debug("Detect change", "changed", changed, "ref", ref) @@ -52,10 +41,10 @@ func emitOutput() error { } //nolint:forbidigo -func isChanged(refURL string, localFile string) bool { +func isChanged(ctx context.Context, refURL string, localFile string) bool { client := &http.Client{Timeout: httpTimeout} - req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, refURL, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, refURL, nil) if err != nil { return true } @@ -79,5 +68,3 @@ func isChanged(refURL string, localFile string) bool { return !bytes.Equal(refData, localData) } - -const httpTimeout = 10 * time.Second diff --git a/cmd/cmd.go b/cmd/cmd.go index cda42db..de9b892 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -29,6 +29,7 @@ type options struct { api string test []string origin string + ref string } // New creates new cobra command for exec command. @@ -73,6 +74,7 @@ func New(levelVar *slog.LevelVar) (*cobra.Command, error) { flags.StringVarP(&opts.out, "out", "o", "", "write output to file instead of stdout") flags.StringVar(&opts.api, "api", "", "write outputs to directory instead of stdout") flags.StringVar(&opts.origin, "origin", "", "external registry URL for default values") + flags.StringVar(&opts.ref, "ref", "", "reference output URL for change detection") flags.StringSliceVar(&opts.test, "test", []string{}, "test api path(s) (example: /registry.json,/catalog.json)") flags.BoolVarP(&opts.quiet, "quiet", "q", false, "no output, only validation") flags.BoolVar(&opts.loose, "loose", false, "skip JSON schema validation") @@ -146,8 +148,12 @@ func run(ctx context.Context, args []string, opts *options) (result error) { return err } + return postRun(ctx, registry, output, opts) +} + +func postRun(ctx context.Context, registry k6registry.Registry, output io.Writer, opts *options) error { if len(opts.api) != 0 { - if err = writeAPI(registry, opts.api); err != nil { + if err := writeAPI(registry, opts.api); err != nil { return err } @@ -160,11 +166,17 @@ func run(ctx context.Context, args []string, opts *options) (result error) { } } - if opts.quiet { - return nil + if !opts.quiet { + if err := writeOutput(registry, output, opts.compact, false); err != nil { + return err + } + } + + if isGitHubAction() && len(opts.out) > 0 && len(opts.ref) > 0 { + return emitOutput(ctx, opts.out, opts.ref) } - return writeOutput(registry, output, opts.compact, false) + return nil } //nolint:forbidigo diff --git a/cmd/k6registry/main.go b/cmd/k6registry/main.go index 7045f58..d7d69d5 100644 --- a/cmd/k6registry/main.go +++ b/cmd/k6registry/main.go @@ -5,9 +5,7 @@ import ( "log" "log/slog" "os" - "strings" - "github.com/google/shlex" "github.com/grafana/k6registry/cmd" sloglogrus "github.com/samber/slog-logrus/v2" "github.com/sirupsen/logrus" @@ -28,11 +26,12 @@ func initLogging() *slog.LevelVar { return levelVar } +//nolint:forbidigo func main() { log.SetFlags(0) log.Writer() - runCmd(newCmd(getArgs(), initLogging())) + runCmd(newCmd(os.Args[1:], initLogging())) } func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command { @@ -51,83 +50,4 @@ func runCmd(cmd *cobra.Command) { if err := cmd.Execute(); err != nil { log.Fatal(formatError(err)) } - - if isGitHubAction() { - if err := emitOutput(); err != nil { - log.Fatal(formatError(err)) - } - } -} - -//nolint:forbidigo -func isGitHubAction() bool { - return os.Getenv("GITHUB_ACTIONS") == "true" -} - -//nolint:forbidigo -func getArgs() []string { - if !isGitHubAction() { - return os.Args[1:] - } - - var args []string - - if getenv("INPUT_QUIET", "false") == "true" { - args = append(args, "--quiet") - } - - if getenv("INPUT_VERBOSE", "false") == "true" { - args = append(args, "--verbose") - } - - if getenv("INPUT_LOOSE", "false") == "true" { - args = append(args, "--loose") - } - - if getenv("INPUT_LINT", "false") == "true" { - args = append(args, "--lint") - } - - if getenv("INPUT_COMPACT", "false") == "true" { - args = append(args, "--compact") - } - - if catalog := getenv("INPUT_CATALOG", ""); len(catalog) != 0 { - args = append(args, "--catalog", catalog) - } - - if api := getenv("INPUT_API", ""); len(api) != 0 { - args = append(args, "--api", api) - } - - if out := getenv("INPUT_OUT", ""); len(out) != 0 { - args = append(args, "--out", out) - } - - if paths := getenv("INPUT_TEST", ""); len(paths) != 0 { - parts, err := shlex.Split(paths) - if err == nil { - paths = strings.Join(parts, ",") - } - - args = append(args, "--test", paths) - } - - if origin := getenv("INPUT_ORIGIN", ""); len(origin) != 0 { - args = append(args, "--origin", origin) - } - - args = append(args, getenv("INPUT_IN", "")) - - return args -} - -//nolint:forbidigo -func getenv(name string, defval string) string { - value, found := os.LookupEnv(name) - if found { - return value - } - - return defval } diff --git a/go.mod b/go.mod index b4cde0f..0554495 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/cli/go-gh/v2 v2.11.0 github.com/go-git/go-git/v5 v5.12.0 github.com/google/go-github/v62 v62.0.0 - github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/grafana/clireadme v0.1.0 github.com/grafana/k6lint v0.3.1 github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59 diff --git a/go.sum b/go.sum index 78d18f8..bb28485 100644 --- a/go.sum +++ b/go.sum @@ -75,8 +75,6 @@ github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwM github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/grafana/clireadme v0.1.0 h1:KYEYSnYdSzmHf3bufaK6fQZ5j4dzvM/T+G6Ba+qNnAM= github.com/grafana/clireadme v0.1.0/go.mod h1:Wy4KIG2ZBGMYAYyF9l7qAy+yoJVasqk/txsRgoRI3gc= github.com/grafana/k6foundry v0.3.0 h1:C+6dPbsOv7Uq4hEhBFNuYqmTdE9jQ0VqhXqBDtMkVTE= diff --git a/releases/v0.2.0.md b/releases/v0.2.0.md new file mode 100644 index 0000000..604e719 --- /dev/null +++ b/releases/v0.2.0.md @@ -0,0 +1,5 @@ +k6registry `v0.2.0` is here 🎉! + +This is an internal maintenance release. +- removed GitHub Actions mode and Docker image support +- still supports "changed" GitHub output variable if running inside of workflow