47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
#!python
|
|
import subprocess
|
|
import argparse
|
|
from itertools import chain
|
|
import re
|
|
|
|
dockercmd = 'docker'
|
|
|
|
parser = argparse.ArgumentParser(description='Create custom recumock images.')
|
|
parser.add_argument('tags', metavar='TAG', nargs='*', help='Version tags to build.')
|
|
|
|
args = parser.parse_args()
|
|
|
|
platforms = ['linux/amd64', 'linux/arm64', 'linux/arm/v7']
|
|
|
|
def pull(image):
|
|
subprocess.run([dockercmd, 'pull', baseimage], check=True)
|
|
|
|
def build(images, directory, platforms, build_args = None):
|
|
if build_args is None:
|
|
build_args = []
|
|
build_args = list(chain.from_iterable(['--build-arg', f'{arg}={val}'] for (arg, val) in build_args))
|
|
tags = list(chain.from_iterable(['-t', image] for image in images))
|
|
platformlist = ','.join(platforms)
|
|
command = [dockercmd, 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platformlist, *tags] + build_args + ['--push', directory]
|
|
print(' '.join(command))
|
|
subprocess.run(command, check=True)
|
|
|
|
def version_from_project():
|
|
with open(r'MinecraftDiscordBot\MinecraftDiscordBot.csproj', 'r') as f:
|
|
project = f.read()
|
|
|
|
regex = r"<Version>\s*([^<]*?)\s*<\/Version>"
|
|
matches = re.search(regex, project, re.IGNORECASE)
|
|
if not matches:
|
|
raise Exception("Could not read version from project file!")
|
|
return matches.group(1)
|
|
|
|
if len(args.tags) == 0:
|
|
args.tags.append(version_from_project())
|
|
|
|
for version in args.tags:
|
|
parts = version.split('.')
|
|
tags = list('.'.join(parts[:i]) for i in range(1, len(parts) + 1))
|
|
tags.append('latest')
|
|
build([f'chenio/mcdiscordbot:{tag}' for tag in tags], '.', platforms)
|