Skip to content

Commit d853d19

Browse files
committed
chore: have presubmit ignore builds which can fail.
1 parent 5d40396 commit d853d19

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

scripts/ci/after-script.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ echo '---------------------'
1010
echo '-- WAIT FOR OTHERS --'
1111
echo '---------------------'
1212

13-
curl -Lo travis_after_all.py https://raw.github.com/jbdeboer/travis_after_all/master/travis_after_all.py
14-
python travis_after_all.py
13+
python ./scripts/ci/travis_after_all.py
1514
. .to_export_back
1615

1716
echo BUILD_LEADER=$BUILD_LEADER

scripts/ci/travis_after_all.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import os
2+
import json
3+
import time
4+
import logging
5+
6+
try:
7+
import urllib.request as urllib2
8+
except ImportError:
9+
import urllib2
10+
11+
log = logging.getLogger("travis.leader")
12+
log.addHandler(logging.StreamHandler())
13+
log.setLevel(logging.INFO)
14+
15+
TRAVIS_JOB_NUMBER = 'TRAVIS_JOB_NUMBER'
16+
TRAVIS_BUILD_ID = 'TRAVIS_BUILD_ID'
17+
POLLING_INTERVAL = 'LEADER_POLLING_INTERVAL'
18+
19+
build_id = os.getenv(TRAVIS_BUILD_ID)
20+
polling_interval = int(os.getenv(POLLING_INTERVAL, '5'))
21+
22+
#assume, first job is the leader
23+
is_leader = lambda job_number: job_number.endswith('.1')
24+
25+
if not os.getenv(TRAVIS_JOB_NUMBER):
26+
# seems even for builds with only one job, this won't get here
27+
log.fatal("Don't use defining leader for build without matrix")
28+
exit(1)
29+
elif is_leader(os.getenv(TRAVIS_JOB_NUMBER)):
30+
log.info("This is a leader")
31+
else:
32+
#since python is subprocess, env variables are exported back via file
33+
with open(".to_export_back", "w") as export_var:
34+
export_var.write("BUILD_MINION=YES")
35+
log.info("This is a minion")
36+
exit(0)
37+
38+
39+
class MatrixElement(object):
40+
def __init__(self, json_raw):
41+
self.allow_failure = json_raw['allow_failure']
42+
self.is_finished = json_raw['finished_at'] is not None
43+
self.is_succeeded = json_raw['result'] == 0
44+
self.number = json_raw['number']
45+
self.is_leader = is_leader(self.number)
46+
47+
48+
def matrix_snapshot():
49+
"""
50+
:return: Matrix List
51+
"""
52+
response = urllib2.build_opener().open("https://api.travis-ci.org/builds/{0}".format(build_id)).read()
53+
raw_json = json.loads(response)
54+
matrix_without_leader = [MatrixElement(element) for element in raw_json["matrix"]]
55+
return matrix_without_leader
56+
57+
58+
def wait_others_to_finish():
59+
def others_finished():
60+
"""
61+
Dumps others to finish
62+
Leader cannot finish, it is working now
63+
:return: tuple(True or False, List of not finished jobs)
64+
"""
65+
snapshot = matrix_snapshot()
66+
finished = [el.is_finished for el in snapshot if not (el.is_leader or el.allow_failure)]
67+
return reduce(lambda a, b: a and b, finished), [el.number for el in snapshot if
68+
not el.is_leader and not el.is_finished]
69+
70+
while True:
71+
finished, waiting_list = others_finished()
72+
if finished: break
73+
log.info("Leader waits for minions {0}...".format(waiting_list)) # just in case do not get "silence timeout"
74+
time.sleep(polling_interval)
75+
76+
77+
try:
78+
wait_others_to_finish()
79+
80+
final_snapshot = matrix_snapshot()
81+
log.info("Final Results: {0}".format([(e.number, e.is_succeeded, e.allow_failure) for e in final_snapshot]))
82+
83+
BUILD_AGGREGATE_STATUS = 'BUILD_AGGREGATE_STATUS'
84+
others_snapshot = [el for el in final_snapshot if not (el.is_leader or el.allow_failure)]
85+
if reduce(lambda a, b: a and b, [e.is_succeeded for e in others_snapshot]):
86+
os.environ[BUILD_AGGREGATE_STATUS] = "others_succeeded"
87+
elif reduce(lambda a, b: a and b, [not e.is_succeeded for e in others_snapshot]):
88+
log.error("Others Failed")
89+
os.environ[BUILD_AGGREGATE_STATUS] = "others_failed"
90+
else:
91+
log.warn("Others Unknown")
92+
os.environ[BUILD_AGGREGATE_STATUS] = "unknown"
93+
#since python is subprocess, env variables are exported back via file
94+
with open(".to_export_back", "w") as export_var:
95+
export_var.write("BUILD_LEADER=YES {0}={1}".format(BUILD_AGGREGATE_STATUS, os.environ[BUILD_AGGREGATE_STATUS]))
96+
97+
except Exception as e:
98+
log.fatal(e)

0 commit comments

Comments
 (0)