-
Notifications
You must be signed in to change notification settings - Fork 10
/
jenkins_remote_trigger.sh
executable file
·166 lines (148 loc) · 3.53 KB
/
jenkins_remote_trigger.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
#!/bin/bash
# *************************************************************
function success() {
prompt="$1"
echo -e -n "\033[1;32m$prompt"
echo -e -n '\033[0m'
echo -e -n "\n"
}
function error() {
prompt="$1"
echo -e -n "\033[1;31m$prompt"
echo -e -n '\033[0m'
echo -e -n "\n"
}
function info() {
prompt="$1"
echo -e -n "\033[1;36m$prompt"
echo -e -n '\033[0m'
echo -e -n "\n"
}
# *************************************************************
usage()
{
cat << USAGE >&2
Usage:
-h HOST | --host=HOST Jenkins host
-j JOBNAME | --jobname=test-build-job The name of the jenkins job to trigger
-p JOBPARAM | --jobparam=environment=uat&test=1 Jenkins job paramiters
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT Timeout in minutes, zero for no timeout
USAGE
exit 1
}
# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
-q | --quiet)
QUIET=1
shift 1
;;
-h)
HOST="$2"
if [[ $HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
HOST="${1#*=}"
shift 1
;;
-t)
TIMEOUT="$2"
if [[ $TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
-j)
JOBNAME="$2"
if [[ $JOBNAME == "" ]]; then break; fi
shift 2
;;
--jobname=*)
JOBNAME="${1#*=}"
shift 1
;;
-p)
JOBPARAM="$2"
if [[ $JOBPARAM == "" ]]; then break; fi
shift 2
;;
--jobparam=*)
JOBPARAM="${1#*=}"
shift 1
;;
--)
shift
CLI="$@"
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done
TIMEOUT=${TIMEOUT:-30}
QUIET=${QUIET:-0}
TRIGGERURL="${HOST}/job/${JOBNAME}/buildWithParameters?${JOBPARAM}"
if [ $QUIET -eq 0 ];then
info "Making request to trigger $JOBNAME job."
fi
TMP=`curl -s -D - -X POST "$TRIGGERURL"`
QID=`echo "$TMP" | grep Location | cut -d "/" -f 6`
QUEUE_URL="${HOST}/queue/item/${QID}/api/json?pretty=true"
sleep 1
while curl -v $QUEUE_URL 2>&1 | egrep -q "BlockedItem|WaitingItem";
do
if [ $QUIET -eq 0 ];then
info "Waiting for queued job to start.."
fi
sleep 5
done
JOBID=$(curl -s "$QUEUE_URL" | jq --raw-output '.executable.number')
JOBURL=$(curl -s "$QUEUE_URL" | jq --raw-output '.executable.url')
if [ -z "$JOBID" ];
then
if [ $QUIET -eq 0 ];then
error "Error creating job."
fi
exit 1
fi
if [ $QUIET -eq 0 ];then
success ""
success "Jenkins job $JOBID created, waiting to complete.."
success ""
fi
STATUS=""
while [ "$STATUS" != 200 ]
do
sleep 1
STATUS=`curl -s -o /dev/null -w "%{http_code}" "${JOBURL}"consoleText`
done
JOBURLJSON="$JOBURL"api/json?pretty=true
BUILDING=$(curl -s "$JOBURLJSON" |jq --raw-output '.building')
while $BUILDING; do
BUILDING=$(curl -s "$JOBURLJSON" |jq --raw-output '.building')
if [ $QUIET -eq 0 ];then
info "Building.."
fi
sleep 10
done
JOBSTATUS=$(curl -s "$JOBURLJSON" |jq --raw-output '.result')
if [ $QUIET -eq 0 ];then
NOTIFY=error
if [ "$JOBSTATUS" == "SUCCESS" ]; then
NOTIFY=success
fi
$NOTIFY ""
$NOTIFY "Job $JOBID finished with status: $JOBSTATUS"
$NOTIFY ""
fi
[[ "$JOBSTATUS" == "SUCCESS" ]]