-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjenkins-curl-wrapper.sh
More file actions
executable file
·71 lines (61 loc) · 2.18 KB
/
Copy pathjenkins-curl-wrapper.sh
File metadata and controls
executable file
·71 lines (61 loc) · 2.18 KB
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
#!/usr/bin/env bash
# jenkins-curl-wrapper.sh - Loads Jenkins authentication information and runs curl, passing in command-line arguments.
#
# Re-use this to call curl on Jenkins servers.
# By default gets and inserts a CSRF crumb in header.
# Pass in '-XGET' or '-XPOST' followed by the rest of your arguments, depending
# on the API calls you're making.
#
#set -Eeuo pipefail
set -eo pipefail
[ -n "$DEBUG" ] && set -x
function define () { IFS='\n' read -r -d '' ${1} || true; }
function _get_crumb () {
curl -s -u "${JENKINS_USER}:${JENKINS_TOKEN}" "${JENKINS_SERVER_URL}"'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
}
function _usage () {
cat <<EOUSAGE
Usage: $0 CURL_ARGUMENTS [..]
Loads jenkins authentication information and runs curl with arguments provided
on the command-line.
Requires the following environment variables or files:
JENKINS_USER, $HOME/.jenkins-user Jenkins user to login with
JENKINS_TOKEN, $HOME/.jenkins-token Jenkins API token or TOKEN
JENKINS_SERVER_URL, $HOME/.jenkins-url Base Jenkins url for CSRF crumb
Note that you still need to pass at least a full URL for curl to get as a command-line
argument. Example:
$0 -XPOST --data-urlencode "script@foo.groovy" http://localhost:8080/scriptText
EOUSAGE
exit 1
}
if [ -z "$JENKINS_USER" ] && [ -r $HOME/.jenkins-user ] ; then
JENKINS_USER="$(cat $HOME/.jenkins-user)"
fi
if [ -z "$JENKINS_TOKEN" ] && [ -r $HOME/.jenkins-token ] ; then
JENKINS_TOKEN="$(cat $HOME/.jenkins-token)"
fi
if [ -z "$JENKINS_SERVER_URL" ] && [ -r $HOME/.jenkins-url ] ; then
JENKINS_SERVER_URL="$(cat $HOME/.jenkins-url)"
fi
if [ -z "${JENKINS_USER}" ] || [ -z "$JENKINS_TOKEN" ] ; then
echo "Error: set JENKINS_USER and JENKINS_TOKEN environment variables"
exit 1
fi
if [ -z "$JENKINS_SERVER_URL" ] ; then
echo "Error: set JENKINS_SERVER_URL environment variable"
exit 1
fi
if [ $# -lt 1 ] ; then
_usage
fi
JENKINS_CRUMB=$(_get_crumb)
set +e
curl \
-s -H "${JENKINS_CRUMB}" \
-u "${JENKINS_USER}:${JENKINS_TOKEN}" \
"$@"
RET=$?
if [ $RET -ne 0 ] ; then
echo "$0: Error: curl returned an error; set DEBUG=1 for more information"
exit $RET
fi