-
Notifications
You must be signed in to change notification settings - Fork 1
/
promotion.sh
executable file
·123 lines (105 loc) · 2.88 KB
/
promotion.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
#! /bin/bash
# description.sh
########################################################################
# CHANGE JENKINS BUILD DESCRIPTION
#
USAGE="
promotion.sh [-u <JenkinsURL>] -j <JenkinsJob> \\
-b <jenkinsBuild> -p <promotionName>
or
promotion.sh -h
where:
-h: Show this help text.
-u: Full URL of Jenkins server. If not given, it will be taken
from value embedded in program.
-j: Jenkins 'Job' name (aka 'project).
-b: Build Number.
-p: Name of the promotion.
"
#
########################################################################
########################################################################
# FUNCTIONS
#
function error {
printf "ERROR: $1\n$USAGE\n" 1>&2
exit 2
}
#
########################################################################
########################################################################
# MAIN PROGRAM
#
jenkins_url="http://buildl02.tcprod.local/jenkins"
readonly USER="xxxxxxx"
readonly PASSWORD="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
readonly promotion_url="promotion/promotionProcess"
readonly promotion_plugin_url="promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve"
#
# Set options
#
is_extglob_set=$(shopt -q extglob)
[ "$is_extglob_set" ] || shopt -s extglob
while getopts :hu:j:b:p: option
do
case $option in
p) promotion_name="$OPTARG";;
u) jenkins_url="$OPTARG";;
j) job_name="$OPTARG";;
b) build_number="$OPTARG"
if [[ $build_number != +([0-9]) ]]
then
error "Build Number must be numeric"
fi
;;
h) printf "\n$USAGE\n"
exit 0;;
*) error "Invalid Argument";;
esac
done
shift $(( $OPTIND - 1 ))
[ "$is_extglob_set" ] || shopt -u extglob
#
# Verify options
#
[ -z "$promotion_name" ] && error "Missing parameter '-p': Promotion"
[ -z "$build_number" ] && error "Missing parameter '-b': Build Number"
[ -z "$job_name" ] && error "Missing parameter '-j': Job Name"
#
# Get old description (if -a or -p flag)
#
#
# Encode Promotion Name
#
encoded_promotion_name=$(perl url-encode.pl -input "$promotion_name");
if [ $? -ne 0 ]
then
echo "ERROR: Promotion name '$promotion_name' Couldn't be URL encoded" 1>&2
exit 2
fi
#
# Now do the promotion
#
output=$(curl -s -u "$USER:$PASSWORD" --data "json={}" --data "Submit=Approve" \
"$jenkins_url/job/$job_name/$build_number/$promotion_url/$encoded_promotion_name/$promotion_plugin_url/")
if [ $? -ne 0 ]
then
echo "ERROR: Could not execute 'curl' command or could not contact server '$jenkins_url'" 1>&2
exit 2
fi
#
# If there was any text returned by the curl command, there was an error.
#
if [ -n "$output" ]
then
error=$(sed 's/<[^>]*>//g' <<<"$output")
printf "There was some sort of error:\n$error\n" 1>&2
exit 2
fi
#
# Everything worked out!
#
echo "Build Successfully promoted"
#
# DONE
########################################################################