Skip to content

Commit

Permalink
Merge pull request #5 from grafana/4-fix-k6-permission-in-docker-image
Browse files Browse the repository at this point in the history
fix k6 permission in docker image
  • Loading branch information
szkiba authored Oct 7, 2024
2 parents 0ed3b60 + 60367c0 commit 8d06286
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Dockerfile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM alpine:3.18 AS release

RUN adduser -D -u 12345 -g 12345 k6

COPY {{.Executable}} /usr/bin/k6
COPY --chmod=755 {{.Executable}} /usr/bin/k6

USER k6
WORKDIR /home/k6
Expand All @@ -15,7 +15,7 @@ FROM release AS with-browser

USER root

COPY --from=release /usr/bin/k6 /usr/bin/k6
COPY --chmod=755 --from=release /usr/bin/k6 /usr/bin/k6
RUN apk --no-cache add chromium-swiftshader

USER k6
Expand Down
2 changes: 1 addition & 1 deletion NOTES.md.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
🎉 **{{.Name}}** `{{.Version}}` is here!
🎉 {{.Name}} `{{.Version}}` is here!

## Contents

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ branding:
color: orange

inputs:
in:
args:
description: input file name
required: true

Expand Down
66 changes: 27 additions & 39 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,57 @@ import (
"os"
"path/filepath"
"strings"

"github.com/google/shlex"
"github.com/spf13/pflag"
)

// AddGitHubArgs adds GitHub input parameters as arguments.
func AddGitHubArgs(args []string) []string {
func AddGitHubArgs(args []string, flags *pflag.FlagSet) ([]string, error) {
if !isGitHubAction() {
return args
return args, nil
}

args = ghinput(args, "distro_name", "--distro-name")
args = ghinput(args, "distro_version", "--distro-version")
args = ghinput(args, "executable", "--executable")
args = ghinput(args, "archive", "--archive")
args = ghinput(args, "docker", "--docker")
args = ghinput(args, "docker_template", "--docker-template")
args = ghinput(args, "notes", "--notes")
args = ghinput(args, "notes_template", "--notes-template")
args = ghinput(args, "notes_latest", "--notes-latest")
args = ghinput(args, "readme", "--readme")
args = ghinput(args, "license", "--license")
args = ghinput(args, "platform", "--platform")

if getenv("INPUT_VERBOSE", "false") == "true" {
args = append(args, "--verbose")
}
flags.Visit(func(flag *pflag.Flag) {
args = ghinput(args, flag)
})

if getenv("INPUT_QUIET", "false") == "true" {
args = append(args, "--quiet")
}
if iargs := os.Getenv("INPUT_ARGS"); len(iargs) > 0 { //nolint:forbidigo
items, err := shlex.Split(iargs)
if err != nil {
return nil, err
}

if in := getenv("INPUT_IN", ""); len(in) > 0 {
args = append(args, in)
args = append(args, items...)
}

return args
return args, nil
}

//nolint:forbidigo
func isGitHubAction() bool {
return os.Getenv("GITHUB_ACTIONS") == "true"
}

//nolint:forbidigo
func getenv(name string, defval string) string {
value, found := os.LookupEnv(name)
if found {
return value
}

return defval
}

func ghinput(args []string, name string, flag string) []string {
val := getenv("INPUT_"+strings.ToUpper(name), "")
if len(val) > 0 {
args = append(args, flag, val)
func ghinput(args []string, flag *pflag.Flag) []string {
name := "INPUT_" + strings.ToUpper(strings.ReplaceAll(flag.Name, "-", "_"))

if val := os.Getenv(name); len(val) > 0 { //nolint:forbidigo
if flag.Value.Type() == "bool" {
if val == "true" {
args = append(args, "--"+flag.Name)
}
} else {
args = append(args, "--"+flag.Name, val)
}
}

return args
}

//nolint:forbidigo
func emitOutput(changed bool, version string) error {
ghOutput := getenv("GITHUB_OUTPUT", "")
ghOutput := os.Getenv("GITHUB_OUTPUT")
if len(ghOutput) == 0 {
return nil
}
Expand Down
28 changes: 16 additions & 12 deletions cmd/k6dist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,29 @@ func initLogging() *slog.LevelVar {
}

func main() {
runCmd(newCmd(getArgs(), initLogging()))
root, err := newCmd(os.Args[1:], initLogging()) //nolint:forbidigo
checkErr(err)
checkErr(root.Execute())
}

func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command {
cmd := cmd.New(levelVar)
func newCmd(args []string, levelVar *slog.LevelVar) (*cobra.Command, error) {
root := cmd.New(levelVar)

cmd.Version = version
cmd.SetArgs(args)
root.Version = version

return cmd
args, err := cmd.AddGitHubArgs(args, root.Flags())
if err != nil {
return nil, err
}

root.SetArgs(args)

return root, nil
}

func runCmd(cmd *cobra.Command) {
if err := cmd.Execute(); err != nil {
func checkErr(err error) {
if err != nil {
slog.Error(err.Error())
os.Exit(1) //nolint:forbidigo
}
}

func getArgs() []string {
return cmd.AddGitHubArgs(os.Args[1:]) //nolint:forbidigo
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ go 1.22.4

require (
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
github.com/grafana/k6foundry v0.2.0
github.com/samber/slog-logrus/v2 v2.5.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/samber/lo v1.44.0 // indirect
github.com/samber/slog-common v0.17.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
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.2.0 h1:+aE5wuCP0XNGNsxM7UiPj9hyw4RdWeW929PuGwLWIlg=
Expand Down
12 changes: 12 additions & 0 deletions releases/v0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
🎉 k6dist `v0.1.2` is here!

## Fixes

- The executable bit of the k6 binary was removed for some reason while copying to the Docker image. Now the executable bit is explicitly set during copying.

## Refactors

- GitHub Action input environment variables are now handled automatically based on the command flag. The name of the environment variable is generated from the name of the flag. In this way, all environment variables belonging to flags are processed.

- The GitHub Action parameter corresponding to the positional argument is now `args`, i.e. the corresponding environment variable `INPUT_ARGS`. Its value is split using the shlex package.

0 comments on commit 8d06286

Please sign in to comment.