-
Notifications
You must be signed in to change notification settings - Fork 71
/
helper.sh
executable file
·180 lines (162 loc) · 5.69 KB
/
helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env bash
#set -x
awk=awk
sed=sed
grep=grep
if [ "$(uname)" == "Darwin" ] ; then
grep='ggrep'
awk='gawk'
sed='gsed'
fi
all_labels="test-all"
pipeline_id_file=pipeline_id
function variable_not_found() {
echo "Required variable not found: $1"
exit 1
}
function stop_gitlab_pipeline() {
test -z $GITLAB_PERSONAL_TOKEN && variable_not_found "GITLAB_PERSONAL_TOKEN"
if [ ! -f $pipeline_id_file ] ; then
echo "Pipeline ID file is not present, can't cancel Gitlab pipeline"
exit 1
fi
PIPELINE_ID=$(cat $pipeline_id_file)
echo "Stopping gitlab pipeline ID: $PIPELINE_ID"
curl -s -X POST -H "PRIVATE-TOKEN: $GITLAB_PERSONAL_TOKEN" "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/pipelines/$PIPELINE_ID/cancel" 1>/dev/null
}
function release() {
test -z $GITLAB_PROJECT_ID && variable_not_found "GITLAB_PROJECT_ID"
test -z $GITLAB_TOKEN && variable_not_found "GITLAB_TOKEN"
test -z $GITLAB_PERSONAL_TOKEN && variable_not_found "GITLAB_PERSONAL_TOKEN"
test -z $GITHUB_BRANCH && variable_not_found "GITHUB_BRANCH"
GITLAB_REF="main"
echo "Requesting Gitlab pipeline"
PIPELINE_ID=$(curl -s -X POST -F "token=$GITLAB_TOKEN" -F "ref=$GITLAB_REF" -F "variables[GITHUB_COMMIT_ID]=$GITHUB_COMMIT_ID" -F "variables[GITHUB_ENGINE_BRANCH_NAME]=$GITHUB_BRANCH" -F "variables[TESTS_TYPE]=$TESTS_TYPE" https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/trigger/pipeline | jq --raw-output '.id')
if [ $(echo $PIPELINE_ID | egrep -c '^[0-9]+$') -eq 0 ] ; then
echo "Pipeline ID is not correct, we expected a number and got: $PIPELINE_ID"
exit 1
fi
echo "Pipeline ID: $PIPELINE_ID"
}
function gh_tags_selector_for_gitlab() {
gh_json=$(curl -s -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/Qovery/engine/pulls?state=open")
gh_pr=$(echo $gh_json | jq --compact-output '.[] | {labels, ref: .head.ref}' | grep "$GITHUB_BRANCH")
num_labels=$(echo $gh_pr | jq '.labels | length')
if [ "$num_labels" != "0" ] ; then
all_labels=""
for i in $(echo $gh_pr | jq -r '.labels[].name' | grep 'test-') ; do
all_labels="$all_labels,$i"
done
all_labels=$(echo $all_labels | sed 's/^,//')
fi
echo $all_labels
}
function run_tests() {
TESTS_TYPE=$1
echo "Requested tests: $TESTS_TYPE"
test -z $GITLAB_PROJECT_ID && variable_not_found "GITLAB_PROJECT_ID"
test -z $GITLAB_TOKEN && variable_not_found "GITLAB_TOKEN"
test -z $GITLAB_PERSONAL_TOKEN && variable_not_found "GITLAB_PERSONAL_TOKEN"
test -z $GITHUB_BRANCH && variable_not_found "GITHUB_BRANCH"
GITLAB_REF="dev"
FORCE_CHECKOUT_CUSTOM_BRANCH='false'
TESTS_TO_RUN="-F \"variables[GITHUB_COMMIT_ID]=$GITHUB_COMMIT_ID\""
if [ $(curl -s --header "PRIVATE-TOKEN: $GITLAB_PERSONAL_TOKEN" "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/repository/branches/$GITHUB_BRANCH" | grep -c '404 Branch Not Found') -eq 0 ] ; then
echo "Same branch name detected on gitlab, requesting to use it instead of dev branch"
FORCE_CHECKOUT_CUSTOM_BRANCH='true'
fi
echo "Requesting Gitlab pipeline"
PIPELINE_ID=$(curl -s -X POST -F "token=$GITLAB_TOKEN" -F "ref=$GITLAB_REF" -F "variables[GITHUB_COMMIT_ID]=$GITHUB_COMMIT_ID" -F "variables[GITHUB_ENGINE_BRANCH_NAME]=$GITHUB_BRANCH" -F "variables[TESTS_TO_RUN]=$TESTS_TYPE" -F "variables[FORCE_CHECKOUT_CUSTOM_BRANCH]=$FORCE_CHECKOUT_CUSTOM_BRANCH" https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/trigger/pipeline | jq --raw-output '.id')
if [ $(echo $PIPELINE_ID | egrep -c '^[0-9]+$') -eq 0 ] ; then
echo "Pipeline ID is not correct, we expected a number and got: $PIPELINE_ID"
exit 1
fi
echo $PIPELINE_ID > $pipeline_id_file
trap "stop_gitlab_pipeline" SIGTERM SIGINT
sleep 2
pipeline_status=''
counter=0
max_unexpected_status=5
while [ $counter -le $max_unexpected_status ] ; do
current_status=$(curl -s -H "PRIVATE-TOKEN: $GITLAB_PERSONAL_TOKEN" https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/pipelines/$PIPELINE_ID | jq --raw-output '.detailed_status.text')
echo "Current pipeline id $PIPELINE_ID status: $current_status"
case $current_status in
"created")
((counter=$counter+1))
;;
"waiting_for_resource")
((counter=$counter+1))
;;
"preparing")
((counter=$counter+1))
;;
"pending")
((counter=$counter+1))
;;
"running")
counter=0
;;
"passed")
echo "Results: Congrats, functional tests succeeded!!!"
exit 0
;;
"success")
echo "Results: Congrats, functional tests succeeded!!!"
exit 0
;;
"failed")
echo "Results: Functional $TESTS_TYPE tests failed"
exit 1
;;
"canceled")
exit 1
;;
"skipped")
exit 1
;;
"manual")
exit 1
;;
"scheduled")
((counter=$counter+1))
;;
"null")
((counter=$counter+1))
;;
esac
sleep 5
done
echo "Results: functional tests failed due to a too high number ($max_unexpected_status) of unexpected status."
exit 1
}
#set -u
case $1 in
full_tests)
run_tests full
;;
release)
release
;;
autodetect)
tags=$(gh_tags_selector_for_gitlab)
run_tests $tags
;;
stop_gitlab_pipeline)
stop_gitlab_pipeline
;;
check_gh_tags)
if [ "$(gh_tags_selector_for_gitlab)" == "$all_labels" ] ; then
echo "All tests have been enabled"
exit 0
fi
echo "You need to enable all the tests to validate this PR"
exit 1
;;
*)
echo "Usage:"
echo "$0 autodetect: autodetect tests to run based on tags"
echo "$0 full_tests: run full tests (with cloud providers check)"
echo "$0 stop_gitlab_pipeline: stop gitlab pipeline"
echo "$0 check_gh_tags: get defined tags (only working if branch is a PR)"
;;
esac