Skip to content

Commit

Permalink
Write exit code to Action outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Nov 9, 2023
1 parent 77bad4f commit f8bc371
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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 }}
28 changes: 18 additions & 10 deletions cmd/action/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"context"
"errors"
"fmt"
"os"

"github.com/futurice/jalapeno/internal/cli"
"github.com/gofrs/uuid"
)

var (
Expand All @@ -15,24 +15,32 @@ 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"))
}

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) {
Expand Down
12 changes: 6 additions & 6 deletions cmd/jalapeno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions internal/cli/option/oci_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 0 additions & 6 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"time"

Expand All @@ -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 != "" {
Expand Down

0 comments on commit f8bc371

Please sign in to comment.