Skip to content

Commit 7150e1f

Browse files
authored
Group log output on GitHub Actions (#4961)
1 parent ac4b767 commit 7150e1f

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ test-find: ## Run super-linter on a subdirectory with USE_FIND_ALGORITHM=true
112112
docker run \
113113
-e RUN_LOCAL=true \
114114
-e ACTIONS_RUNNER_DEBUG=true \
115-
-e DEFAULT_BRANCH=main \
116115
-e ERROR_ON_MISSING_EXEC_BIT=true \
116+
-e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true \
117+
-e DEFAULT_BRANCH=main \
117118
-e USE_FIND_ALGORITHM=true \
118119
-v "$(CURDIR)/.github":/tmp/lint \
119120
$(SUPER_LINTER_TEST_CONTAINER_URL)
@@ -124,6 +125,7 @@ lint-codebase: ## Lint the entire codebase
124125
-e RUN_LOCAL=true \
125126
-e ACTIONS_RUNNER_DEBUG=true \
126127
-e DEFAULT_BRANCH=main \
128+
-e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true \
127129
-e ERROR_ON_MISSING_EXEC_BIT=true \
128130
-e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \
129131
-v "$(CURDIR):/tmp/lint" \
@@ -135,6 +137,7 @@ test-linters: ## Run the linters test suite
135137
-e ACTIONS_RUNNER_DEBUG=true \
136138
-e ANSIBLE_DIRECTORY=.automation/test/ansible \
137139
-e DEFAULT_BRANCH=main \
140+
-e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true \
138141
-e ERROR_ON_MISSING_EXEC_BIT=true \
139142
-e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \
140143
-e RUN_LOCAL=true \

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ You can configure super-linter using the following environment variables:
161161
| **DISABLE_ERRORS** | `false` | Flag to have the linter complete with exit code 0 even if errors were detected. |
162162
| **DOCKERFILE_HADOLINT_FILE_NAME** | `.hadolint.yaml` | Filename for [hadolint configuration](https://github.com/hadolint/hadolint) (ex: `.hadolintlintrc.yaml`) |
163163
| **EDITORCONFIG_FILE_NAME** | `.ecrc` | Filename for [editorconfig-checker configuration](https://github.com/editorconfig-checker/editorconfig-checker) |
164+
| **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). |
164165
| **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. |
165166
| **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 |
166167
| **FILTER_REGEX_EXCLUDE** | `none` | Regular expression defining which files will be excluded from linting (ex: `.*src/test.*`) |

lib/functions/log.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,47 @@ fatal() {
9494
log "true" "$*" "FATAL"
9595
exit 1
9696
}
97+
98+
# shellcheck disable=SC2034 # Variable is referenced in other files
99+
SUPER_LINTER_INITIALIZATION_LOG_GROUP_TITLE="Super-Linter initialization"
100+
GITHUB_ACTIONS_LOG_GROUP_MARKER_START="start"
101+
GITHUB_ACTIONS_LOG_GROUP_MARKER_END="end"
102+
103+
writeGitHubActionsLogGroupMarker() {
104+
local LOG_GROUP_MARKER_MODE="${1}"
105+
shift
106+
local GROUP_TITLE="${1}"
107+
108+
if [ -z "${GROUP_TITLE}" ]; then
109+
fatal "GitHub Actions log group title variable is empty."
110+
fi
111+
112+
if [ -z "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}" ]; then
113+
fatal "GitHub Actions enable log group title variable is empty."
114+
fi
115+
116+
if [[ "${LOG_GROUP_MARKER_MODE}" != "${GITHUB_ACTIONS_LOG_GROUP_MARKER_START}" ]] &&
117+
[[ "${LOG_GROUP_MARKER_MODE}" != "${GITHUB_ACTIONS_LOG_GROUP_MARKER_END}" ]]; then
118+
fatal "Unsupported LOG_GROUP_MARKER_MODE (${LOG_GROUP_MARKER_MODE}) for group: ${GROUP_TITLE}"
119+
fi
120+
121+
if [[ "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}" == "true" ]]; then
122+
if [[ "${LOG_GROUP_MARKER_MODE}" == "${GITHUB_ACTIONS_LOG_GROUP_MARKER_START}" ]]; then
123+
echo "::group::${GROUP_TITLE}"
124+
debug "Started GitHub Actions log group: ${GROUP_TITLE}"
125+
elif [[ "${LOG_GROUP_MARKER_MODE}" == "${GITHUB_ACTIONS_LOG_GROUP_MARKER_END}" ]]; then
126+
debug "Ending GitHub Actions log group: ${GROUP_TITLE}"
127+
echo "::endgroup::"
128+
fi
129+
else
130+
debug "Skipped GitHub Actions log group ${LOG_GROUP_MARKER_MODE} for group: ${GROUP_TITLE}"
131+
fi
132+
}
133+
134+
startGitHubActionsLogGroup() {
135+
writeGitHubActionsLogGroupMarker "${GITHUB_ACTIONS_LOG_GROUP_MARKER_START}" "${1}"
136+
}
137+
138+
endGitHubActionsLogGroup() {
139+
writeGitHubActionsLogGroupMarker "${GITHUB_ACTIONS_LOG_GROUP_MARKER_END}" "${1}"
140+
}

lib/linter.sh

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
#!/usr/bin/env bash
22

3-
################################################################################
4-
################################################################################
5-
########### Super-Linter (Lint all the code) @admiralawkbar ####################
6-
################################################################################
7-
################################################################################
8-
93
##################################################################
104
# Debug Vars #
115
# 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
6963
source "$batch_worker_script"
7064
done
7165

66+
# Initialize RUN_LOCAL early because we need it for logging
67+
DEFAULT_RUN_LOCAL='false'
68+
69+
if [ -z "${RUN_LOCAL}" ]; then
70+
RUN_LOCAL="${DEFAULT_RUN_LOCAL}"
71+
fi
72+
73+
# Convert string to lowercase
74+
RUN_LOCAL="${RUN_LOCAL,,}"
75+
76+
# Dynamically set the default behavior for GitHub Actions log markers because
77+
# we want to give users a chance to enable this even when running locally, but
78+
# we still want to provide a default value in case they don't want to explictly
79+
# configure it.
80+
if [[ "${RUN_LOCAL}" == "true" ]]; then
81+
DEFAULT_ENABLE_GITHUB_ACTIONS_GROUP_TITLE="false"
82+
else
83+
DEFAULT_ENABLE_GITHUB_ACTIONS_GROUP_TITLE="true"
84+
fi
85+
# Let users configure GitHub Actions log markers regardless of running locally or not
86+
ENABLE_GITHUB_ACTIONS_GROUP_TITLE="${ENABLE_GITHUB_ACTIONS_GROUP_TITLE:-"${DEFAULT_ENABLE_GITHUB_ACTIONS_GROUP_TITLE}"}"
87+
88+
startGitHubActionsLogGroup "${SUPER_LINTER_INITIALIZATION_LOG_GROUP_TITLE}"
89+
90+
debug "RUN_LOCAL: ${RUN_LOCAL}"
91+
debug "ENABLE_GITHUB_ACTIONS_GROUP_TITLE: ${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}"
92+
7293
###########
7394
# GLOBALS #
7495
###########
@@ -377,7 +398,6 @@ debug "IGNORE_GENERATED_FILES: ${IGNORE_GENERATED_FILES}"
377398
DEFAULT_VALIDATE_ALL_CODEBASE='true' # Default value for validate all files
378399
DEFAULT_SUPER_LINTER_WORKSPACE="/tmp/lint" # Fall-back value for the workspace
379400
DEFAULT_WORKSPACE="${DEFAULT_WORKSPACE:-${DEFAULT_SUPER_LINTER_WORKSPACE}}" # Default workspace if running locally
380-
DEFAULT_RUN_LOCAL='false' # Default value for debugging locally
381401
DEFAULT_TEST_CASE_RUN='false' # Flag to tell code to run only test cases
382402

383403
if [ -z "${TEST_CASE_RUN}" ]; then
@@ -388,14 +408,6 @@ fi
388408
TEST_CASE_RUN="${TEST_CASE_RUN,,}"
389409
debug "TEST_CASE_RUN: ${TEST_CASE_RUN}"
390410

391-
if [ -z "${RUN_LOCAL}" ]; then
392-
RUN_LOCAL="${DEFAULT_RUN_LOCAL}"
393-
fi
394-
395-
# Convert string to lowercase
396-
RUN_LOCAL="${RUN_LOCAL,,}"
397-
debug "RUN_LOCAL: ${RUN_LOCAL}"
398-
399411
###############################################################
400412
# Default Vars that are called in Subs and need to be ignored #
401413
###############################################################
@@ -1057,7 +1069,10 @@ else
10571069
EXPERIMENTAL_BATCH_WORKER="false"
10581070
fi
10591071

1072+
endGitHubActionsLogGroup "${SUPER_LINTER_INITIALIZATION_LOG_GROUP_TITLE}"
1073+
10601074
for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
1075+
startGitHubActionsLogGroup "${LANGUAGE}"
10611076
debug "Running linter for the ${LANGUAGE} language..."
10621077
VALIDATE_LANGUAGE_VARIABLE_NAME="VALIDATE_${LANGUAGE}"
10631078
debug "Setting VALIDATE_LANGUAGE_VARIABLE_NAME to ${VALIDATE_LANGUAGE_VARIABLE_NAME}..."
@@ -1106,6 +1121,7 @@ for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
11061121
debug "Invoking ${LINTER_NAME} linter. TEST_CASE_RUN: ${TEST_CASE_RUN}"
11071122
LintCodebase "${LANGUAGE}" "${LINTER_NAME}" "${LINTER_COMMAND}" "${FILTER_REGEX_INCLUDE}" "${FILTER_REGEX_EXCLUDE}" "${TEST_CASE_RUN}" "${EXPERIMENTAL_BATCH_WORKER}" "${!LANGUAGE_FILE_ARRAY}"
11081123
fi
1124+
endGitHubActionsLogGroup "${LANGUAGE}"
11091125
done
11101126

11111127
##########

0 commit comments

Comments
 (0)