factorio-docker/build.py
2021-09-02 14:57:59 +02:00

72 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import json
import subprocess
import shutil
import sys
import tempfile
from jinja2 import Environment, FileSystemLoader
jinja_env = Environment(
loader=FileSystemLoader('templates')
)
def build_dockerfile(sha1, version, tags):
build_dir = tempfile.mktemp()
shutil.copytree("docker", build_dir)
template = jinja_env.get_template("Dockerfile.jinja2")
dockerfile_content = template.render(sha1=sha1, version=version)
with open(os.path.join(build_dir, "Dockerfile"), "w") as dockerfile:
dockerfile.write(dockerfile_content)
build_command = ["docker", "build", "."]
for tag in tags:
build_command.extend(["-t", f"factoriotools/factorio:{tag}"])
try:
subprocess.run(build_command, cwd=build_dir, check=True)
except subprocess.CalledProcessError:
print("Build of image failed")
exit(1)
def login():
try:
username = os.environ["DOCKER_USERNAME"]
password = os.environ["DOCKER_PASSWORD"]
subprocess.run(["docker", "login", "-u", username, "-p", password], check=True)
except KeyError:
print("Username and password need to be given")
exit(1)
except subprocess.CalledProcessError:
print("Docker login failed")
exit(1)
def main(push_tags=False):
with open(os.path.join(os.path.dirname(__file__), "buildinfo.json")) as file_handle:
builddata = json.load(file_handle)
if push_tags:
login()
for version, buildinfo in builddata.items():
sha1 = buildinfo["sha1"]
tags = buildinfo["tags"]
build_dockerfile(sha1, version, tags)
if not push_tags:
continue
for tag in tags:
try:
subprocess.run(["docker", "push", f"factoriotools/factorio:{tag}"],
check=True)
except subprocess.CalledProcessError:
print("Docker push failed")
exit(1)
if __name__ == '__main__':
push_tags = len(sys.argv) > 1 and sys.argv[1] == "--push-tags"
main(push_tags)