Skip to content

Commit 359eabf

Browse files
Merge pull request #3 from gruntwork-io/2025-03-11_log_pretty
Remove log noise
2 parents c7ac0d1 + efe024d commit 359eabf

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

scripts/init.sh

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

33
set -euo pipefail
4-
5-
set -x
4+
log_level="${PIPELINES_LOG_LEVEL:-info}"
5+
log_level="${log_level,,}" # Convert to lowercase
6+
if [[ "$log_level" == "debug" || "$log_level" == "trace" ]]; then
7+
set -x
8+
fi
69

710
: "${APERTURE_OIDC_TOKEN:?"APERTURE_OIDC_TOKEN must be set"}"
811
: "${API_BASE_URL:?"API_BASE_URL must be set"}"
@@ -19,6 +22,8 @@ CI_MERGE_REQUEST_IID="${CI_MERGE_REQUEST_IID:-}"
1922
GITLAB_TOKEN=$PIPELINES_GITLAB_TOKEN
2023
export GITLAB_TOKEN
2124

25+
echo "Initializing Gruntwork Pipelines"
26+
2227
get_merge_request_id() {
2328
if [[ -n "$CI_MERGE_REQUEST_IID" ]]; then
2429
echo "$CI_MERGE_REQUEST_IID"
@@ -38,16 +43,22 @@ get_merge_request_id() {
3843
fi
3944
}
4045

46+
echo -n "Fetching merge request ID... "
4147
merge_request_id=$(get_merge_request_id)
48+
echo "done."
4249

4350
# Turn off command tracing before fetching notes
4451
set +x
4552
merge_request_notes="[]"
4653
if [[ -n "$merge_request_id" ]]; then
54+
echo -n "Fetching existing merge request notes... "
4755
merge_request_notes="$(glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes" --paginate 2>/dev/null)"
56+
echo "done."
4857
fi
4958
# Turn command tracing back on if needed
50-
set -x
59+
if [[ "$log_level" == "debug" || "$log_level" == "trace" ]]; then
60+
set -x
61+
fi
5162

5263
collapse_older_pipelines_notes() {
5364
if [[ "$merge_request_notes" == "[]" ]]; then
@@ -72,7 +83,7 @@ collapse_older_pipelines_notes() {
7283
if [[ "$note_body" =~ "<details open>" ]]; then
7384
echo "Removing open directive from note body"
7485
collapsed_body=$(sed 's/<details open>/<details>/' <<<"$note_body")
75-
glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes/$note_id" --method PUT --raw-field "body=$collapsed_body"
86+
glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes/$note_id" --method PUT --raw-field "body=$collapsed_body" --silent
7687
fi
7788
fi
7889
done <<<"$notes_to_collapse"
@@ -87,9 +98,9 @@ $body"
8798
local -r existing_note_id=$(echo "$merge_request_notes" | jq -r --arg sticky_header "$sticky_header" '. | map(select(.body | startswith($sticky_header))) | .[].id')
8899

89100
if [[ -n "$existing_note_id" ]]; then
90-
glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes/$existing_note_id" --method PUT --raw-field "body=$sticky_body"
101+
glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes/$existing_note_id" --method PUT --raw-field "body=$sticky_body" --silent
91102
else
92-
glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes" --raw-field "body=$sticky_body"
103+
glab api "projects/$CI_PROJECT_ID/merge_requests/$merge_request_id/notes" --raw-field "body=$sticky_body" --silent
93104
fi
94105
}
95106

@@ -103,10 +114,11 @@ report_error() {
103114
echo "$message"
104115
}
105116

117+
credentials_log=$(mktemp -t pipelines-credentials-XXXXXXXX.log)
106118
get_gruntwork_read_token() {
107119
export PIPELINES_TOKEN_PATH="pipelines-read/gruntwork-io"
108120
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
109-
node "$SCRIPT_DIR/pipelines-credentials.mjs" >&2
121+
node "$SCRIPT_DIR/pipelines-credentials.mjs" > "$credentials_log" 2>&1
110122
# The node script writes the token to a file, so we need to source it to make it available
111123
set -a
112124
source credentials.sh
@@ -115,45 +127,61 @@ get_gruntwork_read_token() {
115127
}
116128

117129
# Exchange the APERTURE_OIDC_TOKEN for a Gruntwork Read token
130+
echo -n "Authenticating with Gruntwork API... "
118131
set +e
119132
PIPELINES_GRUNTWORK_READ_TOKEN=$(get_gruntwork_read_token)
120133
get_gruntwork_read_token_exit_code=$?
121134
set -e
122135

136+
echo -n "" # gitlab seems to eat the next echo
137+
123138
if [[ $get_gruntwork_read_token_exit_code -ne 0 ]]; then
139+
cat "$credentials_log"
124140
report_error "Failed to authenticate with the Gruntwork API"
125141
exit 1
126142
fi
143+
echo "done."
127144

128145
# Make the token available to other sections in the rest of the current job
129146
export PIPELINES_GRUNTWORK_READ_TOKEN
130147
echo "PIPELINES_GRUNTWORK_READ_TOKEN=$PIPELINES_GRUNTWORK_READ_TOKEN" >>"$GITLAB_ENV"
131148
echo "PIPELINES_GRUNTWORK_READ_TOKEN=$PIPELINES_GRUNTWORK_READ_TOKEN" >>build.env
132149

150+
echo -n "Cloning pipelines-actions repository... "
133151
# Clone the pipelines-actions repository
152+
clone_log=$(mktemp -t pipelines-clone-XXXXXXXX.log)
134153
set +e
135-
git clone -b "$GRUNTWORK_PIPELINES_ACTIONS_REF" "https://oauth2:$PIPELINES_GRUNTWORK_READ_TOKEN@github.com:/gruntwork-io/pipelines-gitlab-actions.git" /tmp/pipelines-actions
154+
git clone -b "$GRUNTWORK_PIPELINES_ACTIONS_REF" \
155+
"https://oauth2:$PIPELINES_GRUNTWORK_READ_TOKEN@github.com:/gruntwork-io/pipelines-gitlab-actions.git" /tmp/pipelines-actions \
156+
> "$clone_log" 2>&1
136157
clone_exit_code=$?
137158
set -e
138159

139160
if [[ $clone_exit_code -ne 0 ]]; then
161+
cat "$clone_log"
140162
report_error "Failed to clone the pipelines-actions repository"
141163
exit 1
142164
fi
165+
echo "done."
166+
143167

168+
echo -n "Installing Pipelines CLI... "
144169
# Install the Pipelines CLI
170+
install_log=$(mktemp -t pipelines-install-XXXXXXXX.log)
145171
set +e
146-
/tmp/pipelines-actions/scripts/install-pipelines.sh
172+
/tmp/pipelines-actions/scripts/install-pipelines.sh > "$install_log" 2>&1
147173
install_exit_code=$?
148174
set -e
149175

150176
if [[ $install_exit_code -ne 0 ]]; then
177+
cat "$install_log"
151178
report_error "Failed to install the Pipelines CLI"
152179
exit 1
153180
fi
181+
echo "done."
154182

155183
if [[ -n "$merge_request_id" ]]; then
156-
echo "Attempting to collapse pipeline notes for previous commits"
184+
echo -n "Collapsing pipeline notes for previous commits... "
157185
collapse_older_pipelines_notes
158-
echo "Finished attempting to collapse pipeline notes for previous commits"
186+
echo "done."
159187
fi

0 commit comments

Comments
 (0)