Added docker build script with local pre-build stage

This commit is contained in:
Michael Chen 2022-11-23 14:10:44 +01:00
parent 15ed7a5c73
commit edaacf8b0a
Signed by: cnml
GPG Key ID: 5845BF3F82D5F629
2 changed files with 39 additions and 0 deletions

2
Dockerfile.local Normal file
View File

@ -0,0 +1,2 @@
FROM httpd:alpine
COPY dist/ /usr/local/apache2/htdocs/

37
build.py Normal file
View File

@ -0,0 +1,37 @@
from pathlib import Path
from shutil import rmtree
from subprocess import PIPE, run
def buildx(repository: str, tags: list[str], build_platforms: list[str], dockerfile: str = "Dockerfile", build_args: dict[str, str] | None = None, directory: str = ".", push: bool = True, pull: bool = False, progress: str = "auto", write_command: bool = False):
if build_args is None:
build_args = dict()
labels = [f"{repository}:{tag}" for tag in tags]
command = list(filter(None, ["docker", "buildx", "build",
"--platform", ",".join(build_platforms),
*[t for (key, value) in build_args.items()
for t in ("--build-arg", f"{key}={value}")],
"--file", dockerfile,
*[t for label in labels for t in ("--tag", label)],
f"--progress={progress}",
"--pull" if pull else None,
"--push" if push else None,
directory]))
if write_command:
print(" ".join(command))
run(command, check=True)
if __name__ == '__main__':
output_path = Path("dist")
if output_path.exists():
rmtree(output_path)
output_path.mkdir()
run(["python", "createreadmes.py"], check=True)
run(["bundle.bat", "exec", "jekyll", "build", "--destination", output_path], check=True)
branch = run(["git", "branch", "--show-current"],
stdout=PIPE, check=True).stdout.decode().strip()
short_sha = run(["git", "rev-parse", "--short", "HEAD"],
stdout=PIPE, check=True).stdout.decode().strip()
platforms = ['linux/amd64', 'linux/arm/v6', 'linux/arm/v7',
'linux/arm64/v8', 'linux/386', 'linux/ppc64le', 'linux/s390x']
buildx("chenio/code2dfd", [branch, short_sha], platforms, dockerfile="Dockerfile.local")