Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub workflow support #117

Merged
merged 3 commits into from
Nov 25, 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
62 changes: 1 addition & 61 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
project_name: k6registry
version: 2
env:
- IMAGE_OWNER=ghcr.io/grafana
before:
hooks:
- go mod tidy
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}}"
Expand Down Expand Up @@ -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"
11 changes: 0 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions Dockerfile.goreleaser

This file was deleted.

64 changes: 0 additions & 64 deletions action.yml

This file was deleted.

33 changes: 10 additions & 23 deletions cmd/k6registry/action.go → cmd/action.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand All @@ -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
}
Expand All @@ -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)

Expand All @@ -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
}
Expand All @@ -79,5 +68,3 @@ func isChanged(refURL string, localFile string) bool {

return !bytes.Equal(refData, localData)
}

const httpTimeout = 10 * time.Second
20 changes: 16 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type options struct {
api string
test []string
origin string
ref string
}

// New creates new cobra command for exec command.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
84 changes: 2 additions & 82 deletions cmd/k6registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Loading
Loading