From 40581a74ee329ee099f7e8203264761d643f4e67 Mon Sep 17 00:00:00 2001 From: Galo Navarro Date: Wed, 1 Nov 2023 10:25:40 +0100 Subject: [PATCH] Allow error code on action failure (#102) 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 --- .github/workflows/build.yml | 1 + README.md | 17 ++++++++++++++--- cmd/action.go | 19 +++++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 033d39c..62992fb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/README.md b/README.md index aace1e0..3cc1925 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/action.go b/cmd/action.go index 45706ad..cbc2bf0 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -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") @@ -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 { @@ -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",