-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_docker_images.py
67 lines (59 loc) · 2.62 KB
/
update_docker_images.py
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
import os
import subprocess
import requests
import time
import re
token = os.environ['QUAY_TOKEN']
headers = {'Authorization':'Bearer %s' % token}
endpoint = 'https://quay.io/api/v1/repository'
def build(docker_repo, docker_tag, github_project, github_tag, docker_context, dockerfile_path = 'Dockerfile'):
# response = requests.get('%s/%s/trigger' % (endpoint, repo), headers=headers)
# response.raise_for_status()
#commit_sha = subprocess.check_output(['git', 'rev-parse', tag])
# trigger_id = response.json()['triggers'][0]['id']
# data = {'tag_name': tag}
# response = requests.post('%s/%s/trigger/%s/start' % (endpoint, repo, trigger_id), headers=headers, json=data)
# response.raise_for_status()
github_owner, github_repo = github_project.split('/')
match = re.match(r'v([0-9.]+)', github_tag)
if match:
tag = match.group(1)
else:
tag = github_tag
data = {
"archive_url": "https://github.com/%s/archive/%s.tar.gz" % (github_project, tag),
"docker_tags": [docker_tag],
"context": "/%s-%s%s" % (github_repo, tag, docker_context),
"dockerfile_path": "/%s-%s%s%s" % (github_repo, tag, docker_context, dockerfile_path)
}
response = requests.post('%s/%s/build/' % (endpoint, docker_repo), headers=headers, json=data)
response.raise_for_status()
build_id = response.json()['id']
print 'Started build https://quay.io/repository/%s/build/%s/' % (docker_repo, build_id)
print data
build_phase = ''
while build_phase != 'complete' and build_phase != 'error':
response = requests.get('%s/%s/build/%s/status' % (endpoint, docker_repo, build_id), headers=headers)
response.raise_for_status()
build_phase = response.json()['phase']
print build_phase
if build_phase == 'error' or build_phase == 'expired':
raise RuntimeError('Docker build failed!')
time.sleep(5)
try:
github_project = os.environ['TRAVIS_REPO_SLUG']
if os.environ['TRAVIS_EVENT_TYPE'] == 'cron':
github_tag = 'master'
else:
github_tag = os.getenv('TRAVIS_BRANCH')
i = 1
while os.environ.get('BUILD_IMAGE%d' % i, None):
docker_repo, docker_context, dockerfile_path, docker_tag = re.split(', *', os.environ['BUILD_IMAGE%d' % i])
build(docker_repo, docker_tag, github_project, github_tag, docker_context, dockerfile_path)
i += 1
except requests.exceptions.RequestException as e:
if e.response.headers['Content-Type'] == 'application/json':
detail = ', ' + e.response.json().get('detail', '')
else:
detail = ''
print '%s: %s%s' % (e.request.url, e.message, detail)