From 7150e1f8b01db84faa42c68fdf483ff7b7f621ea Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Tue, 12 Dec 2023 20:57:15 +0100 Subject: [PATCH] Group log output on GitHub Actions (#4961) --- Makefile | 5 ++++- README.md | 1 + lib/functions/log.sh | 44 ++++++++++++++++++++++++++++++++++++++++++ lib/linter.sh | 46 +++++++++++++++++++++++++++++--------------- 4 files changed, 80 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 9617c705..a8365391 100644 --- a/Makefile +++ b/Makefile @@ -112,8 +112,9 @@ test-find: ## Run super-linter on a subdirectory with USE_FIND_ALGORITHM=true docker run \ -e RUN_LOCAL=true \ -e ACTIONS_RUNNER_DEBUG=true \ - -e DEFAULT_BRANCH=main \ -e ERROR_ON_MISSING_EXEC_BIT=true \ + -e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true \ + -e DEFAULT_BRANCH=main \ -e USE_FIND_ALGORITHM=true \ -v "$(CURDIR)/.github":/tmp/lint \ $(SUPER_LINTER_TEST_CONTAINER_URL) @@ -124,6 +125,7 @@ lint-codebase: ## Lint the entire codebase -e RUN_LOCAL=true \ -e ACTIONS_RUNNER_DEBUG=true \ -e DEFAULT_BRANCH=main \ + -e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true \ -e ERROR_ON_MISSING_EXEC_BIT=true \ -e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \ -v "$(CURDIR):/tmp/lint" \ @@ -135,6 +137,7 @@ test-linters: ## Run the linters test suite -e ACTIONS_RUNNER_DEBUG=true \ -e ANSIBLE_DIRECTORY=.automation/test/ansible \ -e DEFAULT_BRANCH=main \ + -e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true \ -e ERROR_ON_MISSING_EXEC_BIT=true \ -e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \ -e RUN_LOCAL=true \ diff --git a/README.md b/README.md index c8676bb0..83e8f6e3 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ You can configure super-linter using the following environment variables: | **DISABLE_ERRORS** | `false` | Flag to have the linter complete with exit code 0 even if errors were detected. | | **DOCKERFILE_HADOLINT_FILE_NAME** | `.hadolint.yaml` | Filename for [hadolint configuration](https://github.com/hadolint/hadolint) (ex: `.hadolintlintrc.yaml`) | | **EDITORCONFIG_FILE_NAME** | `.ecrc` | Filename for [editorconfig-checker configuration](https://github.com/editorconfig-checker/editorconfig-checker) | +| **ENABLE_GITHUB_ACTIONS_GROUP_TITLE** | `false` if `RUN_LOCAL=true`, `true` otherwise | Flag to enable [GitHub Actions log grouping](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines). | | **ERROR_ON_MISSING_EXEC_BIT** | `false` | If set to `false`, the `bash-exec` linter will report a warning if a shell script is not executable. If set to `true`, the `bash-exec` linter will report an error instead. | | **EXPERIMENTAL_BATCH_WORKER** | `false` | Flag to enable experimental parallel and batched worker. As of current only `eslint` and `cfn-lint` are supported, if there is no support, original version is used as fallback | | **FILTER_REGEX_EXCLUDE** | `none` | Regular expression defining which files will be excluded from linting (ex: `.*src/test.*`) | diff --git a/lib/functions/log.sh b/lib/functions/log.sh index fbf0843a..cb95b0b9 100755 --- a/lib/functions/log.sh +++ b/lib/functions/log.sh @@ -94,3 +94,47 @@ fatal() { log "true" "$*" "FATAL" exit 1 } + +# shellcheck disable=SC2034 # Variable is referenced in other files +SUPER_LINTER_INITIALIZATION_LOG_GROUP_TITLE="Super-Linter initialization" +GITHUB_ACTIONS_LOG_GROUP_MARKER_START="start" +GITHUB_ACTIONS_LOG_GROUP_MARKER_END="end" + +writeGitHubActionsLogGroupMarker() { + local LOG_GROUP_MARKER_MODE="${1}" + shift + local GROUP_TITLE="${1}" + + if [ -z "${GROUP_TITLE}" ]; then + fatal "GitHub Actions log group title variable is empty." + fi + + if [ -z "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}" ]; then + fatal "GitHub Actions enable log group title variable is empty." + fi + + if [[ "${LOG_GROUP_MARKER_MODE}" != "${GITHUB_ACTIONS_LOG_GROUP_MARKER_START}" ]] && + [[ "${LOG_GROUP_MARKER_MODE}" != "${GITHUB_ACTIONS_LOG_GROUP_MARKER_END}" ]]; then + fatal "Unsupported LOG_GROUP_MARKER_MODE (${LOG_GROUP_MARKER_MODE}) for group: ${GROUP_TITLE}" + fi + + if [[ "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}" == "true" ]]; then + if [[ "${LOG_GROUP_MARKER_MODE}" == "${GITHUB_ACTIONS_LOG_GROUP_MARKER_START}" ]]; then + echo "::group::${GROUP_TITLE}" + debug "Started GitHub Actions log group: ${GROUP_TITLE}" + elif [[ "${LOG_GROUP_MARKER_MODE}" == "${GITHUB_ACTIONS_LOG_GROUP_MARKER_END}" ]]; then + debug "Ending GitHub Actions log group: ${GROUP_TITLE}" + echo "::endgroup::" + fi + else + debug "Skipped GitHub Actions log group ${LOG_GROUP_MARKER_MODE} for group: ${GROUP_TITLE}" + fi +} + +startGitHubActionsLogGroup() { + writeGitHubActionsLogGroupMarker "${GITHUB_ACTIONS_LOG_GROUP_MARKER_START}" "${1}" +} + +endGitHubActionsLogGroup() { + writeGitHubActionsLogGroupMarker "${GITHUB_ACTIONS_LOG_GROUP_MARKER_END}" "${1}" +} diff --git a/lib/linter.sh b/lib/linter.sh index 55356061..6c5a77a9 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -1,11 +1,5 @@ #!/usr/bin/env bash -################################################################################ -################################################################################ -########### Super-Linter (Lint all the code) @admiralawkbar #################### -################################################################################ -################################################################################ - ################################################################## # Debug Vars # # Define these early, so we can use debug logging ASAP if needed # @@ -69,6 +63,33 @@ for batch_worker_script in /action/lib/functions/experimental-batch-workers/*.sh source "$batch_worker_script" done +# Initialize RUN_LOCAL early because we need it for logging +DEFAULT_RUN_LOCAL='false' + +if [ -z "${RUN_LOCAL}" ]; then + RUN_LOCAL="${DEFAULT_RUN_LOCAL}" +fi + +# Convert string to lowercase +RUN_LOCAL="${RUN_LOCAL,,}" + +# Dynamically set the default behavior for GitHub Actions log markers because +# we want to give users a chance to enable this even when running locally, but +# we still want to provide a default value in case they don't want to explictly +# configure it. +if [[ "${RUN_LOCAL}" == "true" ]]; then + DEFAULT_ENABLE_GITHUB_ACTIONS_GROUP_TITLE="false" +else + DEFAULT_ENABLE_GITHUB_ACTIONS_GROUP_TITLE="true" +fi +# Let users configure GitHub Actions log markers regardless of running locally or not +ENABLE_GITHUB_ACTIONS_GROUP_TITLE="${ENABLE_GITHUB_ACTIONS_GROUP_TITLE:-"${DEFAULT_ENABLE_GITHUB_ACTIONS_GROUP_TITLE}"}" + +startGitHubActionsLogGroup "${SUPER_LINTER_INITIALIZATION_LOG_GROUP_TITLE}" + +debug "RUN_LOCAL: ${RUN_LOCAL}" +debug "ENABLE_GITHUB_ACTIONS_GROUP_TITLE: ${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}" + ########### # GLOBALS # ########### @@ -377,7 +398,6 @@ debug "IGNORE_GENERATED_FILES: ${IGNORE_GENERATED_FILES}" DEFAULT_VALIDATE_ALL_CODEBASE='true' # Default value for validate all files DEFAULT_SUPER_LINTER_WORKSPACE="/tmp/lint" # Fall-back value for the workspace DEFAULT_WORKSPACE="${DEFAULT_WORKSPACE:-${DEFAULT_SUPER_LINTER_WORKSPACE}}" # Default workspace if running locally -DEFAULT_RUN_LOCAL='false' # Default value for debugging locally DEFAULT_TEST_CASE_RUN='false' # Flag to tell code to run only test cases if [ -z "${TEST_CASE_RUN}" ]; then @@ -388,14 +408,6 @@ fi TEST_CASE_RUN="${TEST_CASE_RUN,,}" debug "TEST_CASE_RUN: ${TEST_CASE_RUN}" -if [ -z "${RUN_LOCAL}" ]; then - RUN_LOCAL="${DEFAULT_RUN_LOCAL}" -fi - -# Convert string to lowercase -RUN_LOCAL="${RUN_LOCAL,,}" -debug "RUN_LOCAL: ${RUN_LOCAL}" - ############################################################### # Default Vars that are called in Subs and need to be ignored # ############################################################### @@ -1057,7 +1069,10 @@ else EXPERIMENTAL_BATCH_WORKER="false" fi +endGitHubActionsLogGroup "${SUPER_LINTER_INITIALIZATION_LOG_GROUP_TITLE}" + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + startGitHubActionsLogGroup "${LANGUAGE}" debug "Running linter for the ${LANGUAGE} language..." VALIDATE_LANGUAGE_VARIABLE_NAME="VALIDATE_${LANGUAGE}" debug "Setting VALIDATE_LANGUAGE_VARIABLE_NAME to ${VALIDATE_LANGUAGE_VARIABLE_NAME}..." @@ -1106,6 +1121,7 @@ for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do debug "Invoking ${LINTER_NAME} linter. TEST_CASE_RUN: ${TEST_CASE_RUN}" LintCodebase "${LANGUAGE}" "${LINTER_NAME}" "${LINTER_COMMAND}" "${FILTER_REGEX_INCLUDE}" "${FILTER_REGEX_EXCLUDE}" "${TEST_CASE_RUN}" "${EXPERIMENTAL_BATCH_WORKER}" "${!LANGUAGE_FILE_ARRAY}" fi + endGitHubActionsLogGroup "${LANGUAGE}" done ##########