def metadata(cwd, goos='', goarch=''):
md = {
'commit_hash': subprocess.check_output('git rev-parse --verify HEAD', shell=True, cwd=cwd).strip(),
'git_branch': subprocess.check_output('git rev-parse --abbrev-ref HEAD', shell=True, cwd=cwd).strip(),
# http://stackoverflow.com/a/1404862/3476121
'git_tag': subprocess.check_output('git describe --exact-match --abbrev=0 2>/dev/null || echo ""', shell=True,
cwd=cwd).strip(),
'commit_timestamp': datetime.datetime.utcfromtimestamp(
int(subprocess.check_output('git show -s --format=%ct', shell=True, cwd=cwd).strip())).isoformat(),
'build_timestamp': datetime.datetime.utcnow().isoformat(),
'build_host': socket.gethostname(),
'build_host_os': GOENV["GOHOSTOS"],
'build_host_arch': GOENV["GOHOSTARCH"]
}
if md['git_tag']:
md['version'] = md['git_tag']
md['version_strategy'] = 'tag'
elif not md['git_branch'] in ['master', 'HEAD'] and not md['git_branch'].startswith('release-'):
md['version'] = md['git_branch']
md['version_strategy'] = 'branch'
else:
hash_ver = subprocess.check_output('git describe --tags --always --dirty', shell=True, cwd=cwd).strip()
md['version'] = hash_ver
md['version_strategy'] = 'commit_hash'
if goos:
md['os'] = goos
if goarch:
md['arch'] = goarch
return md
评论列表
文章目录