-
Notifications
You must be signed in to change notification settings - Fork 0
/
newjenkinsjob
executable file
·88 lines (65 loc) · 2.5 KB
/
newjenkinsjob
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
#!/bin/bash
# Script to copy a dummy Jenkins job and build a new one.
# Takes a set of arguments to build the new job with relevant parameters
TO_PROJECT=$1
PROFILE=$2
AEGIRSERVER=$3
WEBSERVER=$4
DBSERVER=$5
MAKEFILE=$6
REPO=$7
BRANCH=$8
if [[ -z $TO_PROJECT ]] || [[ -z $PROFILE ]] || [[ -z $AEGIRSERVER ]] || [[ -z $WEBSERVER ]] || [[ -z $WEBSERVER ]] || [[ -z $DBSERVER ]] || [[ -z $MAKEFILE ]] || [[ -z $REPO ]] || [[ -z $BRANCH ]]
then
echo "Missing args!"
exit 1
fi
JENKINS_URL="http://example.com:8080"
USERNAME="jenkinsbot"
PASSWORD="changeme"
TMP_DIR="/tmp/jenkinsJobs"
CURL="curl --user $USERNAME:$PASSWORD -s "
FROM_PROJECT="Dummy_Deploy_Job"
FROM_CONFIG_URL="$JENKINS_URL/job/$FROM_PROJECT/config.xml"
TO_CONFIG_URL="$JENKINS_URL/job/$TO_PROJECT/config.xml"
FROM_CONFIG_XML="$TMP_DIR/$FROM_PROJECT-config.xml"
TO_CONFIG_XML="$TMP_DIR/$TO_PROJECT-config.xml"
# Make our temp dir
mkdir -p $TMP_DIR
# check for existing job
`$CURL -f -o /dev/null $TO_CONFIG_URL`
if [ $? -eq 0 ]; then
echo "WARN: $TO_PROJECT already exists on the jenkins server. No action taken."
exit 1
fi
# download the config.xml for the job, continue if it doesn't exist
`$CURL -o $FROM_CONFIG_XML $FROM_CONFIG_URL`
grep "Error 404" "$FROM_CONFIG_XML" > /dev/null
if [ $? -eq 0 ]; then
echo "FAIL: $FROM_CONFIG_URL is not a valid project url"
exit 1
fi
# replace some details
# Aegir server
sed "s/dummyaegirserver/$AEGIRSERVER/g" "$FROM_CONFIG_XML" > "$TO_CONFIG_XML"
# Profile
sed -i "s/dummyprofile/$PROFILE/g" $TO_CONFIG_XML
# Web server
sed -i "s/dummywebserver/$WEBSERVER/g" "$TO_CONFIG_XML"
# DB server
sed -i "s/dummydbserver/$DBSERVER/g" "$TO_CONFIG_XML"
# Makefile
sed -i "s/dummymakefile/$(echo $MAKEFILE | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" "$TO_CONFIG_XML"
# Repo name
sed -i "s/git\@dummyrepo\.com\:dummy/$(echo $REPO | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" "$TO_CONFIG_XML"
# Branch name
sed -i "s/dummybranch/$BRANCH/g" "$TO_CONFIG_XML"
# create a new job based on the existing job
`$CURL -o /dev/null --data " " "$JENKINS_URL/createItem?name=${TO_PROJECT}&mode=copy&from=${FROM_PROJECT}"`
# upload new job data
`$CURL -o /dev/null --data "@$TO_CONFIG_XML" "$JENKINS_URL/job/$TO_PROJECT/config.xml"`
# enable the job, there seems to be a bug where we need to first disable and then enable
`$CURL -o /dev/null --data disable "$JENKINS_URL/job/$TO_PROJECT/disable"`
`$CURL -o /dev/null --data enable "$JENKINS_URL/job/$TO_PROJECT/enable"`
# Remove our temp dir
rm -rf $TMP_DIR