From f8bc3715009d5fe3d9cbfd7045c370b1d8ad2410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Kivim=C3=A4ki?= Date: Thu, 9 Nov 2023 13:59:58 +0200 Subject: [PATCH] Write exit code to Action outputs --- action.yml | 19 ++++++++++++++++++ cmd/action/main.go | 28 +++++++++++++++++---------- cmd/jalapeno/main.go | 12 ++++++------ internal/cli/option/oci_repository.go | 4 ++-- internal/cli/root.go | 6 ------ 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/action.yml b/action.yml index 77c43ebb..7c06ee59 100644 --- a/action.yml +++ b/action.yml @@ -1,8 +1,27 @@ name: "Jalapeno" description: "Run Jalapeno check on the workspace" +inputs: + insecure: + description: "Allow connections to SSL registry without certs" + default: "false" + required: false + plain-http: + description: "Allow insecure connections to registry without SSL check" + default: "false" + required: false + ca-file: + description: "Server certificate authority file for the remote registry" + required: false +outputs: + exit-code: + description: "Exit code of the operation" runs: using: "docker" image: "docker://ghcr.io/futurice/jalapeno:v0-githubaction" args: - check - --dir=/github/workspace + - --detailed-exitcode + - --insecure=${{ inputs.insecure }} + - --plain-http=${{ inputs.plain-http }} + - --ca-file=${{ inputs.ca-file }} diff --git a/cmd/action/main.go b/cmd/action/main.go index 3e616051..eea79b57 100644 --- a/cmd/action/main.go +++ b/cmd/action/main.go @@ -1,12 +1,12 @@ package main import ( + "context" "errors" "fmt" "os" "github.com/futurice/jalapeno/internal/cli" - "github.com/gofrs/uuid" ) var ( @@ -15,6 +15,10 @@ var ( ) func main() { + if os.Getenv("GITHUB_ACTIONS") != "true" { + checkErr(errors.New("this image only works on Github Actions")) + } + filename := os.Getenv("GITHUB_OUTPUT") if filename == "" { checkErr(errors.New("GITHUB_OUTPUT environment variable not set")) @@ -22,17 +26,21 @@ func main() { output, err := os.OpenFile(filename, os.O_APPEND, 0644) checkErr(err) + defer output.Close() cmd := cli.NewRootCmd(version) - delimiter := uuid.Must(uuid.NewV4()).String() - fmt.Fprintf(output, "result<<%s\n", delimiter) - - // TODO: Add outputs - - fmt.Fprintf(output, "%s\n", delimiter) - - err = cmd.Execute() - checkErr(err) + err = cmd.ExecuteContext(context.Background()) + + exitCode, isExitCodeSet := cmd.Context().Value(cli.ExitCodeContextKey{}).(int) + if !isExitCodeSet { + if err == nil { + exitCode = 0 + } else { + exitCode = 1 + } + } + fmt.Fprintf(output, "exit-code=%d\n", exitCode) + os.Exit(exitCode) } func checkErr(err error) { diff --git a/cmd/jalapeno/main.go b/cmd/jalapeno/main.go index 38795861..b0ce1784 100644 --- a/cmd/jalapeno/main.go +++ b/cmd/jalapeno/main.go @@ -16,14 +16,14 @@ func main() { cmd := cli.NewRootCmd(version) err := cmd.ExecuteContext(context.Background()) - exitCode := cmd.Context().Value(cli.ExitCodeContextKey{}) - if code, ok := exitCode.(int); ok { // Make sure that the exit code is still an int - os.Exit(code) // Exit with the exit code defined by a subcommand - } else { + exitCode, isExitCodeSet := cmd.Context().Value(cli.ExitCodeContextKey{}).(int) + if !isExitCodeSet { if err == nil { - os.Exit(0) + exitCode = 0 } else { - os.Exit(1) + exitCode = 1 } } + + os.Exit(exitCode) } diff --git a/internal/cli/option/oci_repository.go b/internal/cli/option/oci_repository.go index 706ea6d7..60dab585 100644 --- a/internal/cli/option/oci_repository.go +++ b/internal/cli/option/oci_repository.go @@ -21,8 +21,8 @@ type OCIRepository struct { } func (opts *OCIRepository) ApplyFlags(fs *pflag.FlagSet) { - fs.StringVarP(&opts.Username, "username", "u", "", "Registry username") - fs.StringVarP(&opts.Password, "password", "p", "", "Registry password or identity token") + fs.StringVarP(&opts.Username, "username", "u", "", "Username used to log against the Docker registry") + fs.StringVarP(&opts.Password, "password", "p", "", "Password or personal access token used to log against the Docker registry") fs.BoolVarP(&opts.UseInsecure, "insecure", "", false, "Allow connections to SSL registry without certs") fs.BoolVarP(&opts.UsePlainHTTP, "plain-http", "", false, "Allow insecure connections to registry without SSL check") fs.StringVarP(&opts.CACertFilePath, "ca-file", "", "", "Server certificate authority file for the remote registry") diff --git a/internal/cli/root.go b/internal/cli/root.go index 2b217d87..866293f1 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -1,7 +1,6 @@ package cli import ( - "context" "fmt" "time" @@ -18,11 +17,6 @@ func NewRootCmd(version string) *cobra.Command { Short: "Create, manage and share spiced up project templates", Long: "Create, manage and share spiced up project templates.", SilenceUsage: true, - PreRun: func(cmd *cobra.Command, args []string) { - if cmd.Context() != nil { - cmd.SetContext(context.Background()) - } - }, } if version != "" {