Skip to content

Commit

Permalink
Allow error code on action failure (#102)
Browse files Browse the repository at this point in the history
Explained in the readme. This is mainly useful for our own CI, but can
also be used when running the action.

Signed-off-by: Galo Navarro <anglorvaroa@gmail.com>
  • Loading branch information
srvaroa authored Nov 1, 2023
1 parent d36f0e7 commit 40581a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_CONFIG_PATH: ./.github/labeler.yml
INPUT_FAIL_ON_ERROR: true
run: ./action

- name: Check that the docker image builds
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@ will read the config file from the local checkout.

## Troubleshooting

This action will avoid failing in all cases, so if you're experiencing
unexpected behaviour it's worth looking at execution logs. Typical
errors are related to non-existing configuration file or invalid yaml.
To avoid blocking CI pipelines, the action will never return an error
code and just log information about the problem. Typical errors are
related to non-existing configuration file or invalid yaml.

If you wish to make the action fail the pipeline, you can override this
behaviour thus:

steps:
- uses: srvaroa/labeler@master
with:
fail_on_error: true

When `fail_on_error` is enabled, any failure inside the action will
exit the action process with an error code.

## Configuring matching rules

Expand Down
19 changes: 15 additions & 4 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ import (

func main() {

// Determine if we want the action to fail on error, or be silent to
// prevent blocking Ci pipelines
failCode := 0
failOnError, err := strconv.ParseBool(os.Getenv("INPUT_FAIL_ON_ERROR"))
if err != nil {
log.Printf("INPUT_FAIL_ON_ERROR not set, defaulting to silent failures")
} else if failOnError {
log.Printf("INPUT_FAIL_ON_ERROR enabled, the action will exit with an error code on failure")
failCode = 1
}
gh, err := getGithubClient()
if err != nil {
log.Printf("Failed to retrieve a GitHub client: %+v", err)
return
os.Exit(failCode)
}
eventPayload := getEventPayload()
eventName := os.Getenv("GITHUB_EVENT_NAME")
Expand All @@ -39,7 +49,7 @@ func main() {
contents, err := ioutil.ReadFile(configFile)
if err != nil {
log.Printf("Error reading configuration from local file: %s", err)
return
os.Exit(failCode)
}
configRaw = &contents
} else {
Expand All @@ -58,14 +68,15 @@ func main() {

if err != nil {
log.Printf("Error reading configuration from default branch: %s", err)
return
os.Exit(failCode)
}

}

config, err := getLabelerConfigV1(configRaw)
if err != nil {
return
log.Printf("Unable to parse configuration")
os.Exit(failCode)
}

log.Printf("Re-evaluating labels on %s@%s",
Expand Down

0 comments on commit 40581a7

Please sign in to comment.