Skip to content

Commit f603288

Browse files
committed
2022.08.12:
* fixed: bash/github/print-*.sh: execution fixup * new: bash/github: init-print-workflow.sh, print-*.sh, flush-print-annotation.shs: print annotations buffering support * new: bash/github: init-basic-workflow.sh, print-*.sh: `CHANGELOG_BUF_STR` update between GitHub pipeline steps * changed: bash/github/print-*.sh: code cleanup * refactor: bash/github: code refactor
1 parent f12293e commit f603288

File tree

10 files changed

+302
-137
lines changed

10 files changed

+302
-137
lines changed
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_FLUSH_PRINT_ANNOTATIONS_SH" && SOURCE_GHWF_FLUSH_PRINT_ANNOTATIONS_SH -ne 0) ]] && return
9+
10+
SOURCE_GHWF_FLUSH_PRINT_ANNOTATIONS_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_flush_print_annotations "$@"
25+
fi

bash/github/init-basic-workflow.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ SOURCE_GHWF_INIT_BASIC_WORKFLOW_SH=1 # including guard
5050

5151
source "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/bash_tacklelib" || exit $?
5252

53-
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/print.sh"
53+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-print-workflow.sh"
54+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/utils.sh"
5455

5556

5657
function init_basic_workflow()
5758
{
58-
tkl_declare_global CHANGELOG_BUF_STR ''
59+
if [[ -z "${CHANGELOG_BUF_STR:+x}" ]]; then # to save buffer between workflow steps
60+
tkl_declare_global CHANGELOG_BUF_STR ''
5961

60-
gh_set_print_warning_lag .025 # 25 msec
61-
gh_set_print_error_lag .025
62+
# update GitHub pipeline variable
63+
gh_set_env_var CHANGELOG_BUF_STR "$CHANGELOG_BUF_STR"
64+
fi
6265

6366
[[ -z "$CONTINUE_ON_INVALID_INPUT" ]] && CONTINUE_ON_INVALID_INPUT=0
6467
[[ -z "$CONTINUE_ON_EMPTY_CHANGES" ]] && CONTINUE_ON_EMPTY_CHANGES=0
@@ -77,11 +80,6 @@ function init_basic_workflow()
7780
init_basic_workflow || exit $?
7881

7982

80-
function gh_set_env_var()
81-
{
82-
[[ -n "$GITHUB_ACTIONS" ]] && echo "$1=$2" >> $GITHUB_ENV
83-
}
84-
8583
function gh_prepend_changelog_file()
8684
{
8785
(( ! ENABLE_GENERATE_CHANGELOG_FILE )) && return 0
@@ -96,6 +94,9 @@ function gh_prepend_changelog_file()
9694
echo -n "$changelog_buf" > "$CHANGELOG_FILE"
9795

9896
CHANGELOG_BUF_STR=''
97+
98+
# update GitHub pipeline variable
99+
gh_set_env_var CHANGELOG_BUF_STR "$CHANGELOG_BUF_STR"
99100
}
100101

101102
tkl_set_return

bash/github/init-print-workflow.sh

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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_INIT_PRINT_WORKFLOW_SH" && SOURCE_GHWF_INIT_PRINT_WORKFLOW_SH -ne 0) ]] && return
9+
10+
SOURCE_GHWF_INIT_PRINT_WORKFLOW_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-basic-workflow.sh"
20+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/utils.sh"
21+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/print-notice.sh"
22+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/print-warning.sh"
23+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/print-error.sh"
24+
25+
26+
function init_print_workflow()
27+
{
28+
gh_set_print_warning_lag .025 # 25 msec
29+
gh_set_print_error_lag .025
30+
31+
if [[ -n "$GITHUB_ACTIONS" ]]; then
32+
if [[ -z "${GH_ANNOTATIONS_PRINT_BUF_STR:+x}" ]]; then # to save buffer between workflow steps
33+
tkl_declare_global GH_ANNOTATIONS_PRINT_BUF_STR ''
34+
35+
# update GitHub pipeline variable
36+
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
37+
fi
38+
fi
39+
40+
return 0
41+
}
42+
43+
init_print_workflow || exit $?
44+
45+
46+
function gh_enable_print_buffering()
47+
{
48+
local enable_print_notice_buffering=${1:-0} # by default is not buffered, printfs at first
49+
local enable_print_warning_buffering=${1:-1} # by default is buffered, prints at second
50+
local enable_print_error_buffering=${1:-1} # by default is buffered, prints at third
51+
52+
(( enable_print_notice_buffering )) && gh_enable_print_notice_buffering
53+
(( enable_print_warning_buffering )) && gh_enable_print_warning_buffering
54+
(( enable_print_error_buffering )) && gh_enable_print_error_buffering
55+
}
56+
57+
function gh_flush_print_buffers()
58+
{
59+
local print_str
60+
61+
# notices
62+
if [[ -n "${PRINT_NOTICE_BUF_STR+x}" ]]; then
63+
print_str="${PRINT_NOTICE_BUF_STR}"
64+
unset PRINT_NOTICE_BUF_STR
65+
gh_print_notices_nobuf_noprefix "$print_str"
66+
fi
67+
68+
# warnings
69+
if [[ -n "${PRINT_WARNING_BUF_STR+x}" ]]; then
70+
print_str="${PRINT_WARNING_BUF_STR}"
71+
unset PRINT_WARNING_BUF_STR
72+
gh_print_warnings_nobuf_noprefix "$print_str"
73+
fi
74+
75+
# errors
76+
if [[ -n "${PRINT_ERROR_BUF_STR+x}" ]]; then
77+
print_str="${PRINT_ERROR_BUF_STR}"
78+
unset PRINT_ERROR_BUF_STR
79+
gh_print_errors_nobuf_noprefix "$print_str"
80+
fi
81+
}
82+
83+
function gh_write_to_changelog_text_ln()
84+
{
85+
(( ! ENABLE_GENERATE_CHANGELOG_FILE )) && return
86+
87+
local changelog_msg="$1"
88+
89+
CHANGELOG_BUF_STR="${CHANGELOG_BUF_STR}${changelog_msg}"$'\r\n'
90+
91+
# update GitHub pipeline variable
92+
gh_set_env_var CHANGELOG_BUF_STR "$CHANGELOG_BUF_STR"
93+
}
94+
95+
function gh_print_args()
96+
{
97+
local IFS
98+
local arg
99+
local RETURN_VALUE
100+
101+
IFS=$'\n'; for arg in "$@"; do
102+
gh_trim_trailing_line_return_chars "$arg"
103+
echo "$RETURN_VALUE"
104+
done
105+
}
106+
107+
# always buffered, needs to call `gh_flush_print_annotations` to execute all prints
108+
function gh_print_annotation()
109+
{
110+
[[ -z "$GITHUB_ACTIONS" ]] && return 0
111+
112+
local annot_type="$1"
113+
local annot_line="$2"
114+
115+
echo "::$annot_type $annot_line"
116+
117+
# 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"
119+
120+
# update GitHub pipeline variable
121+
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
122+
}
123+
124+
function gh_flush_print_annotations()
125+
{
126+
local IFS
127+
local empty
128+
local annot_type
129+
local line
130+
131+
[[ -z "$GITHUB_ACTIONS" ]] && return 0
132+
133+
IFS=$'\n'; for line in "$GH_ANNOTATIONS_PRINT_BUF_STR"; do
134+
gh_trim_trailing_line_return_chars "$line"
135+
136+
IFS=':' read -r empty empty annot_type <<< "$RETURN_VALUE"
137+
IFS=$'\t ' read -r annot_type empty <<< "$annot_type"
138+
139+
case "$annot_type" in
140+
'notice') echo "$RETURN_VALUE";;
141+
*) echo "$RETURN_VALUE" >&2;;
142+
esac
143+
done
144+
145+
unset GH_ANNOTATIONS_PRINT_BUF_STR
146+
147+
# update GitHub pipeline variable
148+
gh_set_env_var GH_ANNOTATIONS_PRINT_BUF_STR "$GH_ANNOTATIONS_PRINT_BUF_STR"
149+
}
150+
151+
tkl_set_return

bash/github/print-error.sh

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SOURCE_GHWF_PRINT_ERROR_SH=1 # including guard
1616

1717
source "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/bash_tacklelib" || exit $?
1818

19-
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/print.sh"
19+
tkl_include_or_abort "$GH_WORKFLOW_ROOT/bash/github/init-print-workflow.sh"
2020

2121

2222
function gh_enable_print_error_buffering()
@@ -45,13 +45,13 @@ function gh_set_print_error_lag()
4545
#
4646
function gh_print_error_ln()
4747
{
48-
local IFS=$'\n'
48+
local IFS
4949
local line=''
5050
local arg
5151

5252
if [[ -n "${PRINT_ERROR_BUF_STR+x}" ]]; then
5353
if [[ -n "$GITHUB_ACTIONS" ]]; then
54-
for arg in "$@"; do
54+
IFS=$'\n'; for arg in "$@"; do
5555
line="${line}${line:+"%0D%0A"}$arg"
5656
done
5757
PRINT_ERROR_BUF_STR="${PRINT_ERROR_BUF_STR}${PRINT_ERROR_BUF_STR:+$'\r\n'}::error ::$line"
@@ -68,7 +68,7 @@ function gh_print_error_ln()
6868

6969
function gh_print_error_ln_nobuf_nolag()
7070
{
71-
local IFS=$'\n'
71+
local IFS
7272
local line=''
7373
local arg
7474

@@ -78,11 +78,11 @@ function gh_print_error_ln_nobuf_nolag()
7878
# fix GitHub log issue when a trailing line return charcter in the message does convert into blank line
7979

8080
if [[ -n "$GITHUB_ACTIONS" ]]; then
81-
for arg in "$@"; do
81+
IFS=$'\n'; for arg in "$@"; do
8282
line="${line}${line:+"%0D%0A"}$arg"
8383
done
8484
gh_trim_trailing_line_return_chars "$line"
85-
echo "::error ::$RETURN_VALUE" >&2
85+
gh_print_annotation error "::$RETURN_VALUE" >&2
8686
else
8787
gh_trim_trailing_line_return_chars "$*"
8888
echo "$*" >&2
@@ -91,7 +91,7 @@ function gh_print_error_ln_nobuf_nolag()
9191

9292
function gh_print_errors_nobuf_nolag()
9393
{
94-
local IFS=$'\n'
94+
local IFS
9595
local arg
9696

9797
# with check on integer value
@@ -100,12 +100,12 @@ function gh_print_errors_nobuf_nolag()
100100
# fix GitHub log issue when a trailing line return charcter in the message does convert into blank line
101101

102102
if [[ -n "$GITHUB_ACTIONS" ]]; then
103-
for arg in "$@"; do
103+
IFS=$'\n'; for arg in "$@"; do
104104
gh_trim_trailing_line_return_chars "$arg"
105-
echo "::error ::$RETURN_VALUE"
105+
gh_print_annotation error "::$RETURN_VALUE" >&2
106106
done >&2
107107
else
108-
for arg in "$@"; do
108+
IFS=$'\n'; for arg in "$@"; do
109109
gh_trim_trailing_line_return_chars "$arg"
110110
echo "$RETURN_VALUE"
111111
done >&2
@@ -114,24 +114,33 @@ function gh_print_errors_nobuf_nolag()
114114

115115
function gh_print_errors_nobuf_noprefix()
116116
{
117+
local IFS
118+
117119
# with check on integer value
118120
[[ -n "$PRINT_ERROR_LAG_FSEC" && -z "${PRINT_ERROR_LAG_FSEC//[0-9]/}" ]] && sleep $PRINT_ERROR_LAG_FSEC
119121

120122
gh_print_args "$@" >&2
123+
124+
if [[ -n "$GITHUB_ACTIONS" ]]; then
125+
IFS=$'\n'; for arg in "$@"; do
126+
gh_trim_trailing_line_return_chars "$arg"
127+
gh_print_annotation error "::$RETURN_VALUE" >&2
128+
done
129+
fi
121130
}
122131

123132
function gh_print_errors()
124133
{
125-
local IFS=$'\n'
134+
local IFS
126135
local arg
127136

128137
if [[ -n "${PRINT_ERROR_BUF_STR+x}" ]]; then
129138
if [[ -n "$GITHUB_ACTIONS" ]]; then
130-
for arg in "$@"; do
139+
IFS=$'\n'; for arg in "$@"; do
131140
PRINT_ERROR_BUF_STR="${PRINT_ERROR_BUF_STR}${PRINT_ERROR_BUF_STR:+$'\r\n'}::error ::$arg"
132141
done
133142
else
134-
for arg in "$@"; do
143+
IFS=$'\n'; for arg in "$@"; do
135144
PRINT_ERROR_BUF_STR="${PRINT_ERROR_BUF_STR}${PRINT_ERROR_BUF_STR:+$'\r\n'}$arg"
136145
done
137146
fi
@@ -154,6 +163,9 @@ function gh_write_error_to_changelog_text_bullet_ln()
154163
local changelog_msg="$1"
155164

156165
CHANGELOG_BUF_STR="${CHANGELOG_BUF_STR}* error: ${changelog_msg}"$'\r\n'
166+
167+
# update GitHub pipeline variable
168+
gh_set_env_var CHANGELOG_BUF_STR "$CHANGELOG_BUF_STR"
157169
}
158170

159171
# format: <message> | <error_message> <changelog_message>

0 commit comments

Comments
 (0)