-
Notifications
You must be signed in to change notification settings - Fork 277
/
gradle-check.sh
executable file
·110 lines (90 loc) · 3.86 KB
/
gradle-check.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
#!/bin/bash
# Copyright OpenSearch Contributors
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
# This script is used in OpenSearch Core repo github actions
# To trigger Jenkins Gradle Check from a PR
JENKINS_URL="https://build.ci.opensearch.org"
TIMEPASS=0
TIMEOUT=7200
RESULT="null"
TRIGGER_TOKEN=$1
PR_TITLE_NEW=`echo $pr_title | tr -dc '[:alnum:] ' | tr '[:upper:]' '[:lower:]'`
PAYLOAD_JSON="{\"pr_from_sha\": \"$pr_from_sha\", \"pr_from_clone_url\": \"$pr_from_clone_url\", \"pr_to_clone_url\": \"$pr_to_clone_url\", \"pr_title\": \"$PR_TITLE_NEW\", \"pr_number\": \"$pr_number\", \"post_merge_action\": \"$post_merge_action\", \"pr_owner\": \"$pr_owner\"}"
perform_curl_and_process_with_jq() {
local url=$1
local jq_filter=$2
local max_retries=$3
local count=0
local success=false
while [ "$count" -lt "$max_retries" ]; do
response=$(curl -s -XGET "${url}api/json")
processed_response=$(echo "$response" | jq --raw-output "$jq_filter")
jq_exit_code=$?
if [ "$jq_exit_code" -eq "0" ]; then
success=true
echo "$processed_response"
break
else
echo "Attempt $((count+1))/$max_retries failed. The jq processing failed with exit code: $jq_exit_code. Retrying..."
fi
count=$((count+1))
sleep 10
done
if [ "$success" != "true" ]; then
echo "Failed to retrieve and process data after $max_retries attempts."
exit 1
fi
}
echo "Trigger Jenkins workflows"
JENKINS_REQ=`curl -s -XPOST \
-H "Authorization: Bearer $TRIGGER_TOKEN" \
-H "Content-Type: application/json" \
"$JENKINS_URL/generic-webhook-trigger/invoke" \
--data "$(echo $PAYLOAD_JSON)"`
echo $PAYLOAD_JSON
echo $JENKINS_REQ
QUEUE_URL=$(echo $JENKINS_REQ | jq --raw-output '.jobs."gradle-check".url')
echo QUEUE_URL $QUEUE_URL
echo "wait for jenkins to start workflow" && sleep 15
echo "Check if queue exist in Jenkins after triggering"
if [ -z "$QUEUE_URL" ] || [ "$QUEUE_URL" != "null" ]; then
while [ "$RESULT" = "null" ] && [ "$TIMEPASS" -le "$TIMEOUT" ]; do
echo "Use queue information to find build number in Jenkins if available"
WORKFLOW_URL=$(curl -s -XGET ${JENKINS_URL}/${QUEUE_URL}api/json | jq --raw-output .executable.url)
echo WORKFLOW_URL $WORKFLOW_URL
if [ -n "$WORKFLOW_URL" ] && [ "$WORKFLOW_URL" != "null" ]; then
RUNNING="true"
echo "Waiting for Jenkins to complete the run"
while [ "$RUNNING" = "true" ] && [ "$TIMEPASS" -le "$TIMEOUT" ]; do
echo "Still running, wait for another 30 seconds before checking again, max timeout $TIMEOUT"
echo "Jenkins Workflow Url: $WORKFLOW_URL"
TIMEPASS=$(( TIMEPASS + 30 )) && echo time pass: $TIMEPASS
sleep 30
RUNNING=$(perform_curl_and_process_with_jq "$WORKFLOW_URL" ".building" 10)
echo "Workflow running status :$RUNNING"
done
if [ "$RUNNING" = "true" ]; then
echo "Timed out"
RESULT="TIMEOUT"
else
echo "Complete the run, checking results now......"
RESULT=$(curl -s -XGET ${WORKFLOW_URL}api/json | jq --raw-output .result)
fi
else
echo "Job not started yet. Waiting for 60 seconds before next attempt."
TIMEPASS=$(( TIMEPASS + 60 )) && echo time pass: $TIMEPASS
sleep 60
fi
done
fi
echo "Please check jenkins url for logs: $WORKFLOW_URL"
echo "Result: $RESULT"
if [ "$RESULT" == "SUCCESS" ] || [ "$RESULT" == "UNSTABLE" ]; then
echo "Get codeCoverage.xml" && curl -SLO ${WORKFLOW_URL}artifact/codeCoverage.xml
else
exit 1
fi