Skip to content

Commit

Permalink
Group log output on GitHub Actions (#4961)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrarimarco authored Dec 12, 2023
1 parent ac4b767 commit 7150e1f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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" \
Expand All @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*`) |
Expand Down
44 changes: 44 additions & 0 deletions lib/functions/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
46 changes: 31 additions & 15 deletions lib/linter.sh
Original file line number Diff line number Diff line change
@@ -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 #
Expand Down Expand Up @@ -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 #
###########
Expand Down Expand Up @@ -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
Expand All @@ -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 #
###############################################################
Expand Down Expand Up @@ -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}..."
Expand Down Expand Up @@ -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

##########
Expand Down

0 comments on commit 7150e1f

Please sign in to comment.