1
1
#! /usr/bin/env bash
2
2
3
3
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
6
9
7
10
: " ${APERTURE_OIDC_TOKEN:? " APERTURE_OIDC_TOKEN must be set" } "
8
11
: " ${API_BASE_URL:? " API_BASE_URL must be set" } "
@@ -19,6 +22,8 @@ CI_MERGE_REQUEST_IID="${CI_MERGE_REQUEST_IID:-}"
19
22
GITLAB_TOKEN=$PIPELINES_GITLAB_TOKEN
20
23
export GITLAB_TOKEN
21
24
25
+ echo " Initializing Gruntwork Pipelines"
26
+
22
27
get_merge_request_id () {
23
28
if [[ -n " $CI_MERGE_REQUEST_IID " ]]; then
24
29
echo " $CI_MERGE_REQUEST_IID "
@@ -38,16 +43,22 @@ get_merge_request_id() {
38
43
fi
39
44
}
40
45
46
+ echo -n " Fetching merge request ID... "
41
47
merge_request_id=$( get_merge_request_id)
48
+ echo " done."
42
49
43
50
# Turn off command tracing before fetching notes
44
51
set +x
45
52
merge_request_notes=" []"
46
53
if [[ -n " $merge_request_id " ]]; then
54
+ echo -n " Fetching existing merge request notes... "
47
55
merge_request_notes=" $( glab api " projects/$CI_PROJECT_ID /merge_requests/$merge_request_id /notes" --paginate 2> /dev/null) "
56
+ echo " done."
48
57
fi
49
58
# Turn command tracing back on if needed
50
- set -x
59
+ if [[ " $log_level " == " debug" || " $log_level " == " trace" ]]; then
60
+ set -x
61
+ fi
51
62
52
63
collapse_older_pipelines_notes () {
53
64
if [[ " $merge_request_notes " == " []" ]]; then
@@ -72,7 +83,7 @@ collapse_older_pipelines_notes() {
72
83
if [[ " $note_body " =~ " <details open>" ]]; then
73
84
echo " Removing open directive from note body"
74
85
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
76
87
fi
77
88
fi
78
89
done <<< " $notes_to_collapse"
87
98
local -r existing_note_id=$( echo " $merge_request_notes " | jq -r --arg sticky_header " $sticky_header " ' . | map(select(.body | startswith($sticky_header))) | .[].id' )
88
99
89
100
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
91
102
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
93
104
fi
94
105
}
95
106
@@ -103,10 +114,11 @@ report_error() {
103
114
echo " $message "
104
115
}
105
116
117
+ credentials_log=$( mktemp -t pipelines-credentials-XXXXXXXX.log)
106
118
get_gruntwork_read_token () {
107
119
export PIPELINES_TOKEN_PATH=" pipelines-read/gruntwork-io"
108
120
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
110
122
# The node script writes the token to a file, so we need to source it to make it available
111
123
set -a
112
124
source credentials.sh
@@ -115,45 +127,61 @@ get_gruntwork_read_token() {
115
127
}
116
128
117
129
# Exchange the APERTURE_OIDC_TOKEN for a Gruntwork Read token
130
+ echo -n " Authenticating with Gruntwork API... "
118
131
set +e
119
132
PIPELINES_GRUNTWORK_READ_TOKEN=$( get_gruntwork_read_token)
120
133
get_gruntwork_read_token_exit_code=$?
121
134
set -e
122
135
136
+ echo -n " " # gitlab seems to eat the next echo
137
+
123
138
if [[ $get_gruntwork_read_token_exit_code -ne 0 ]]; then
139
+ cat " $credentials_log "
124
140
report_error " Failed to authenticate with the Gruntwork API"
125
141
exit 1
126
142
fi
143
+ echo " done."
127
144
128
145
# Make the token available to other sections in the rest of the current job
129
146
export PIPELINES_GRUNTWORK_READ_TOKEN
130
147
echo " PIPELINES_GRUNTWORK_READ_TOKEN=$PIPELINES_GRUNTWORK_READ_TOKEN " >> " $GITLAB_ENV "
131
148
echo " PIPELINES_GRUNTWORK_READ_TOKEN=$PIPELINES_GRUNTWORK_READ_TOKEN " >> build.env
132
149
150
+ echo -n " Cloning pipelines-actions repository... "
133
151
# Clone the pipelines-actions repository
152
+ clone_log=$( mktemp -t pipelines-clone-XXXXXXXX.log)
134
153
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
136
157
clone_exit_code=$?
137
158
set -e
138
159
139
160
if [[ $clone_exit_code -ne 0 ]]; then
161
+ cat " $clone_log "
140
162
report_error " Failed to clone the pipelines-actions repository"
141
163
exit 1
142
164
fi
165
+ echo " done."
166
+
143
167
168
+ echo -n " Installing Pipelines CLI... "
144
169
# Install the Pipelines CLI
170
+ install_log=$( mktemp -t pipelines-install-XXXXXXXX.log)
145
171
set +e
146
- /tmp/pipelines-actions/scripts/install-pipelines.sh
172
+ /tmp/pipelines-actions/scripts/install-pipelines.sh > " $install_log " 2>&1
147
173
install_exit_code=$?
148
174
set -e
149
175
150
176
if [[ $install_exit_code -ne 0 ]]; then
177
+ cat " $install_log "
151
178
report_error " Failed to install the Pipelines CLI"
152
179
exit 1
153
180
fi
181
+ echo " done."
154
182
155
183
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... "
157
185
collapse_older_pipelines_notes
158
- echo " Finished attempting to collapse pipeline notes for previous commits "
186
+ echo " done. "
159
187
fi
0 commit comments