Skip to content

Commit 48c9ba5

Browse files
committed
2022.08.13:
* fixed: bash/github/print-*.sh: execution fixup * new: bash/github: init-print-workflow.sh, print-*.sh, *-print-annotation-group.sh: ability to group sequence of prints with the same annotation type into single annotation to prevent too many annotations because of the limit in the GitHub Actions job summary (currently is 10) * changed: bash/github/accum-rate-limits.sh: added usage of a print annotation group * refactor: bash/github/print-*.sh: code refactor
1 parent f603288 commit 48c9ba5

File tree

10 files changed

+407
-108
lines changed

10 files changed

+407
-108
lines changed

bash/github/accum-rate-limits.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ source "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/bash_tacklelib" ||
1414
tkl_include_or_abort "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/traplib.sh"
1515

1616
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-basic-workflow.sh"
17+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-print-workflow.sh"
1718
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-stats-workflow.sh"
1819
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-jq-workflow.sh"
1920
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-tacklelib-workflow.sh"
@@ -39,6 +40,11 @@ gh_print_notice_and_write_to_changelog_text_ln "current date/time: $current_date
3940

4041
current_date_utc="${current_date_time_utc/%T*}"
4142

43+
# begin local annotation print group
44+
gh_begin_print_annotation_group notice
45+
46+
tkl_push_trap 'gh_end_print_annotation_group' EXIT
47+
4248
# stats between previous/next script execution (dependent to the pipeline scheduler times)
4349

4450
IFS=$'\n' read -r -d '' \
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# NOTE:
4+
# This is a composite script to use from a composite GitHub action.
5+
#
6+
7+
# Script both for execution and inclusion.
8+
[[ -z "$BASH" || (-n "$SOURCE_GHWF_BEGIN_PRINT_ANNOTATION_GROUP_SH" && SOURCE_GHWF_BEGIN_PRINT_ANNOTATION_GROUP_SH -ne 0) ]] && return
9+
10+
SOURCE_GHWF_BEGIN_PRINT_ANNOTATION_GROUP_SH=1 # including guard
11+
12+
[[ -z "$GH_WORKFLOW_ROOT" ]] && {
13+
echo "$0: error: \`GH_WORKFLOW_ROOT\` variable must be defined." >&2
14+
exit 255
15+
}
16+
17+
source "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/bash_tacklelib" || exit $?
18+
19+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-print-workflow.sh"
20+
21+
22+
if [[ -z "$BASH_LINENO" || BASH_LINENO[0] -eq 0 ]]; then
23+
# Script was not included, then execute it.
24+
gh_begin_print_annotation_group "$@"
25+
fi
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# NOTE:
4+
# This is a composite script to use from a composite GitHub action.
5+
#
6+
7+
# Script both for execution and inclusion.
8+
[[ -z "$BASH" || (-n "$SOURCE_GHWF_END_PRINT_ANNOTATION_GROUP_SH" && SOURCE_GHWF_END_PRINT_ANNOTATION_GROUP_SH -ne 0) ]] && return
9+
10+
SOURCE_GHWF_END_PRINT_ANNOTATION_GROUP_SH=1 # including guard
11+
12+
[[ -z "$GH_WORKFLOW_ROOT" ]] && {
13+
echo "$0: error: \`GH_WORKFLOW_ROOT\` variable must be defined." >&2
14+
exit 255
15+
}
16+
17+
source "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/bash_tacklelib" || exit $?
18+
19+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-print-workflow.sh"
20+
21+
22+
if [[ -z "$BASH_LINENO" || BASH_LINENO[0] -eq 0 ]]; then
23+
# Script was not included, then execute it.
24+
gh_end_print_annotation_group "$@"
25+
fi

bash/github/init-print-workflow.sh

Lines changed: 132 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,25 @@ function init_print_workflow()
2929
gh_set_print_error_lag .025
3030

3131
if [[ -n "$GITHUB_ACTIONS" ]]; then
32-
if [[ -z "${GH_ANNOTATIONS_PRINT_BUF_STR:+x}" ]]; then # to save buffer between workflow steps
32+
# to save variables between workflow steps
33+
if [[ -z "${GH_ANNOTATIONS_PRINT_BUF_STR:+x}" ]]; then
3334
tkl_declare_global GH_ANNOTATIONS_PRINT_BUF_STR ''
3435

35-
# update GitHub pipeline variable
36-
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
36+
# update GitHub pipeline variables
37+
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
38+
fi
39+
40+
# to save variables between workflow steps
41+
if [[ -z "${GH_ANNOTATIONS_GROUP_ANNOT_TYPE:+x}" ]]; then
42+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_TYPE ''
43+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_PREFIX ''
44+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_MSG ''
45+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_INDEX 0
46+
47+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_TYPE "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE"
48+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_PREFIX "$GH_ANNOTATIONS_GROUP_ANNOT_PREFIX"
49+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_MSG "$GH_ANNOTATIONS_GROUP_ANNOT_MSG"
50+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_INDEX "$GH_ANNOTATIONS_GROUP_ANNOT_INDEX"
3751
fi
3852
fi
3953

@@ -56,27 +70,27 @@ function gh_enable_print_buffering()
5670

5771
function gh_flush_print_buffers()
5872
{
59-
local print_str
73+
local line
6074

6175
# notices
6276
if [[ -n "${PRINT_NOTICE_BUF_STR+x}" ]]; then
63-
print_str="${PRINT_NOTICE_BUF_STR}"
77+
line="${PRINT_NOTICE_BUF_STR}"
6478
unset PRINT_NOTICE_BUF_STR
65-
gh_print_notices_nobuf_noprefix "$print_str"
79+
gh_print_notices_buffer "$line"
6680
fi
6781

6882
# warnings
6983
if [[ -n "${PRINT_WARNING_BUF_STR+x}" ]]; then
70-
print_str="${PRINT_WARNING_BUF_STR}"
84+
line="${PRINT_WARNING_BUF_STR}"
7185
unset PRINT_WARNING_BUF_STR
72-
gh_print_warnings_nobuf_noprefix "$print_str"
86+
gh_print_warnings_buffer "$line"
7387
fi
7488

7589
# errors
7690
if [[ -n "${PRINT_ERROR_BUF_STR+x}" ]]; then
77-
print_str="${PRINT_ERROR_BUF_STR}"
91+
line="${PRINT_ERROR_BUF_STR}"
7892
unset PRINT_ERROR_BUF_STR
79-
gh_print_errors_nobuf_noprefix "$print_str"
93+
gh_print_errors_buffer "$line"
8094
fi
8195
}
8296

@@ -100,6 +114,7 @@ function gh_print_args()
100114

101115
IFS=$'\n'; for arg in "$@"; do
102116
gh_trim_trailing_line_return_chars "$arg"
117+
103118
echo "$RETURN_VALUE"
104119
done
105120
}
@@ -109,13 +124,19 @@ function gh_print_annotation()
109124
{
110125
[[ -z "$GITHUB_ACTIONS" ]] && return 0
111126

112-
local annot_type="$1"
113-
local annot_line="$2"
127+
local annot_type="$1" # required
128+
local annot_prefix="$2"
129+
local msg="$3"
130+
131+
#gh_decode_line_return_chars "$msg"
114132

115-
echo "::$annot_type $annot_line"
133+
# stdout redirection must be issued outside
134+
echo "::$annot_type $annot_prefix::$msg"
135+
136+
gh_process_annotation_print "$annot_type" "$annot_prefix" "$msg"
116137

117138
# duplicate output into `GH_ANNOTATIONS_PRINT_BUF_STR` variable to reuse later
118-
GH_ANNOTATIONS_PRINT_BUF_STR="${GH_ANNOTATIONS_PRINT_BUF_STR}${GH_ANNOTATIONS_PRINT_BUF_STR:+$'\r\n'}::$annot_type $annot_line"
139+
GH_ANNOTATIONS_PRINT_BUF_STR="${GH_ANNOTATIONS_PRINT_BUF_STR}${GH_ANNOTATIONS_PRINT_BUF_STR:+"${RETURN_VALUES[0]}"}${RETURN_VALUES[1]}"
119140

120141
# update GitHub pipeline variable
121142
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
@@ -133,6 +154,8 @@ function gh_flush_print_annotations()
133154
IFS=$'\n'; for line in "$GH_ANNOTATIONS_PRINT_BUF_STR"; do
134155
gh_trim_trailing_line_return_chars "$line"
135156

157+
#gh_decode_line_return_chars "$RETURN_VALUE"
158+
136159
IFS=':' read -r empty empty annot_type <<< "$RETURN_VALUE"
137160
IFS=$'\t ' read -r annot_type empty <<< "$annot_type"
138161

@@ -148,4 +171,99 @@ function gh_flush_print_annotations()
148171
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
149172
}
150173

174+
# NOTE: Groups only annotations with the same type.
175+
#
176+
# CAUTION:
177+
#
178+
# Because variable assignment works only between GitHub Actions job steps, then this will NOT work:
179+
#
180+
# - name: head annotations
181+
# shell: bash
182+
# run: |
183+
# $GH_WORKFLOW_ROOT/bash/github/begin-print-annotation-group.sh notice
184+
# $GH_WORKFLOW_ROOT/bash/github/print-notice.sh "111" "222"
185+
# $GH_WORKFLOW_ROOT/bash/github/end-print-annotation-group.sh
186+
#
187+
# This will work:
188+
#
189+
# - name: head annotations
190+
# shell: bash
191+
# run: |
192+
# $GH_WORKFLOW_ROOT/bash/github/begin-print-annotation-group.sh notice
193+
#
194+
# - name: head annotations
195+
# shell: bash
196+
# run: |
197+
# $GH_WORKFLOW_ROOT/bash/github/print-notice.sh "111" "222"
198+
# $GH_WORKFLOW_ROOT/bash/github/end-print-annotation-group.sh # must be last in the step
199+
#
200+
function gh_begin_print_annotation_group()
201+
{
202+
[[ -z "$GITHUB_ACTIONS" ]] && return 0
203+
[[ -n "${GH_ANNOTATIONS_GROUP_ANNOT_TYPE:+x}" ]] && return 0 # ignore if previous group is not closed/ended
204+
205+
local annot_type="$1" # required
206+
local annot_prefix="$2"
207+
local msg="$3"
208+
209+
[[ -z "$annot_type" ]] && return 0
210+
211+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_TYPE "$annot_type"
212+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_PREFIX "$annot_prefix"
213+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_MSG "$msg"
214+
tkl_declare_global GH_ANNOTATIONS_GROUP_ANNOT_INDEX 0
215+
216+
# update GitHub pipeline variable
217+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_TYPE "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE"
218+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_PREFIX "$GH_ANNOTATIONS_GROUP_ANNOT_PREFIX"
219+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_MSG "$GH_ANNOTATIONS_GROUP_ANNOT_MSG"
220+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_INDEX "$GH_ANNOTATIONS_GROUP_ANNOT_INDEX"
221+
}
222+
223+
function gh_end_print_annotation_group()
224+
{
225+
[[ -z "$GITHUB_ACTIONS" ]] && return 0
226+
227+
unset GH_ANNOTATIONS_GROUP_ANNOT_TYPE
228+
unset GH_ANNOTATIONS_GROUP_ANNOT_PREFIX
229+
unset GH_ANNOTATIONS_GROUP_ANNOT_MSG
230+
unset GH_ANNOTATIONS_GROUP_ANNOT_INDEX
231+
232+
# update GitHub pipeline variable
233+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_TYPE "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE"
234+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_PREFIX "$GH_ANNOTATIONS_GROUP_ANNOT_PREFIX"
235+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_MSG "$GH_ANNOTATIONS_GROUP_ANNOT_MSG"
236+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_INDEX "$GH_ANNOTATIONS_GROUP_ANNOT_INDEX"
237+
}
238+
239+
# prefixes a print message with annotation data
240+
function gh_process_annotation_print()
241+
{
242+
local annot_type="$1" # required
243+
local annot_prefix="$2"
244+
local msg="$3"
245+
246+
if [[ -n "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE" && "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE" == "$annot_type" ]]; then
247+
if (( GH_ANNOTATIONS_GROUP_ANNOT_INDEX )); then
248+
tkl_declare_global_array RETURN_VALUES "%0A" "$msg"
249+
else
250+
tkl_declare_global_array RETURN_VALUES $'\r\n' "::$GH_ANNOTATIONS_GROUP_ANNOT_TYPE $GH_ANNOTATIONS_GROUP_ANNOT_PREFIX::${GH_ANNOTATIONS_GROUP_ANNOT_MSG}${GH_ANNOTATIONS_GROUP_ANNOT_MSG:+"%0A"}$msg"
251+
fi
252+
253+
(( GH_ANNOTATIONS_GROUP_ANNOT_INDEX++ ))
254+
else
255+
if [[ -n "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE" ]]; then
256+
# reset index only to group next prints
257+
GH_ANNOTATIONS_GROUP_ANNOT_INDEX=0
258+
fi
259+
260+
tkl_declare_global_array RETURN_VALUES $'\r\n' "::$annot_type $annot_prefix::$msg"
261+
fi
262+
263+
if [[ -n "$GH_ANNOTATIONS_GROUP_ANNOT_TYPE" ]]; then
264+
# update GitHub pipeline variable
265+
gh_set_env_var GH_ANNOTATIONS_GROUP_ANNOT_INDEX "$GH_ANNOTATIONS_GROUP_ANNOT_INDEX"
266+
fi
267+
}
268+
151269
tkl_set_return

0 commit comments

Comments
 (0)