-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_docker
executable file
·101 lines (76 loc) · 3.07 KB
/
build_docker
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
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
import sys
import os
import argparse
import subprocess
import re
REPO_HEAD_DIR = os.path.dirname(os.path.abspath(__file__))
DOCKER_IMAGE_NAME = 'quay.io/mauraisa/s3_client'
def get_git_info(cwd=None):
'''
Get information about a git repository status.
Parameters
----------
cwd: str
The git repo working directory.
Returns
-------
dict
'''
result = subprocess.run(['git', 'status', '--porcelain'],
capture_output=True, cwd=cwd, check=True)
is_dirty = result.stdout.decode('utf-8').strip() != ''
result = subprocess.run(['git', 'log', '-1', '--format=%cd', '--date=local'],
capture_output=True, cwd=cwd, check=True)
git_date = result.stdout.decode('utf-8').strip()
result = subprocess.run(['git', 'rev-parse', '--verify', 'HEAD'],
capture_output=True, cwd=cwd, check=True)
git_hash = result.stdout.decode('utf-8').strip()
# parse branch name
result = subprocess.run(['git', 'branch'],
capture_output=True, cwd=cwd, check=True)
git_branches = [x.strip() for x in result.stdout.decode('utf-8').strip().split('\n')]
git_branch = None
for branch in git_branches:
if branch[0] == '*':
git_branch = branch[1:].strip()
break
# parse repo url
result = subprocess.run(['git', 'remote', '-v'],
capture_output=True, cwd=cwd, check=True)
repos = [re.split(r'\s+', x.strip()) for x in result.stdout.decode('utf-8').strip().split('\n')]
repo = None
for r in repos:
if len(r) >= 3:
if r[0] == 'origin' and r[2] == '(fetch)':
repo = r[1]
break
return {'hash': git_hash,
'last_commit': git_date,
'uncommitted_changes': is_dirty,
'branch': git_branch,
'repo': repo}
def main():
parser = argparse.ArgumentParser(description=f'Build {DOCKER_IMAGE_NAME} docker image from {REPO_HEAD_DIR}')
parser.add_argument('-t', '--tag', type=str, default=None, help='Docker image tag')
args = parser.parse_args()
git_info = get_git_info(cwd=REPO_HEAD_DIR)
docker_command = ['docker', 'build', '-t', DOCKER_IMAGE_NAME]
if args.tag:
docker_command[-1] += f':{args.tag}'
for key, value in git_info.items():
if value is not None:
docker_command.append('--build-arg')
docker_command.append(f"GIT_{key.upper()}={value}")
docker_command.append('--build-arg')
docker_command.append(f"GIT_SHORT_HASH={git_info['hash'][:8]}")
docker_command.append('--build-arg')
docker_command.append(f'DOCKER_IMAGE={DOCKER_IMAGE_NAME}')
docker_command.append('--build-arg')
docker_command.append(f'DOCKER_TAG={args.tag}')
docker_command.append(REPO_HEAD_DIR)
sys.stdout.write(f"{' '.join(docker_command)}\n")
result = subprocess.run(docker_command, cwd=REPO_HEAD_DIR, shell=False, check=False)
sys.exit(result.returncode)
if __name__ == '__main__':
main()