forked from griddynamics/openstack-rhel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers
62 lines (55 loc) · 2.28 KB
/
triggers
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
run_trigger() {
GitDevBranchName="master"
if [[ "$dirname" =~ /jobs/.*/workspace$ ]]
then
JobName="$(echo $dirname | sed 's/^.*\/jobs\/\([^/]\+\)\/workspace$/\1/')"
# Query job status
JobInQueue="$(curl -s http://$jenkins/job/$JobName/api/json | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'inQueue'}")"
if [[ "$JobInQueue" == 1 ]];
then
# Job sits in queue, exiting
exit 0
fi
LastBuildFailed=$(curl -s http://$jenkins/job/$JobName/api/json | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'lastBuild'}{'number'} == \$d->{'lastUnsuccessfulBuild'}{'number'} ? 1 : 0")
if [[ "$LastBuildFailed" == 1 ]];
then
# Last job failed, user intervention required
# Usually that happens when upstream changed
# something that breaks our build/patches, etc.
exit 0
fi
else
echo "This script is designed to run from within Jenkins workspace"
exit -1
fi
# We are not going to build by default
FireNewBuild=0
# Obtaining current branch name from our local git repo
GitCurBranch="$(git branch|grep '*'|cut -f2 -d' ')"
# Getting git revisions of specfile for comparition:
# remote:
GitRev="$(curl -s http://github.com/api/v2/json/commits/list/$GithubUserProject/$GitCurBranch/$SpecOrig | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'commits'}[0]->{'id'}")"
# local:
OurRev=$(git log --pretty=format:"%H" -1 $SpecOrig)
if [[ "$GitRev" != "$OurRev" ]]; then
# Git revision was updated in upstream repository, need to build it
FireNewBuild=1
fi
if [[ "$GitCurBranch" == "$GitDevBranchName" ]]; then
# We are on development version (trunk)
# That means that we need also to consider
# upstream tarballs as trigger for build
SpecVer=$(grep '^Version:' $SpecOrig | sed 's/^Version:\s\+//')
SpecRelease=$(grep '^Release:' $SpecOrig | sed 's/^Release:\s\+//' | sed 's/%{?dist}$//')
Build="$(curl -s $TarballsHome'/?C=M;O=D' | grep $SpecVer | grep bzr | perl -pi -e 's/^.*bzr(\d+).*$/$1/' | head -n 1)"
OurBuild=$(echo "$SpecRelease" | cut -d. -f3 | sed 's/bzr//')
if [[ "$Build" -ne "$OurBuild" ]]; then
# We are behind trunk - need to fire a new build
FireNewBuild=1
fi
fi
if [[ "$FireNewBuild" == 1 ]];
then
curl -s "http://$jenkins/job/$JobName/build"
fi
}