Added portainer webhook for deployment

This commit is contained in:
Michael Chen 2022-11-23 14:29:28 +01:00
parent edaacf8b0a
commit 54eeea998b
Signed by: cnml
GPG Key ID: 5845BF3F82D5F629

View File

@ -1,9 +1,10 @@
from pathlib import Path from pathlib import Path
from shutil import rmtree from shutil import rmtree
from subprocess import PIPE, run from subprocess import PIPE, run
from requests import post
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): def buildx(repository: str, tags: list[str], build_platforms: list[str], dockerfile: str | Path = "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: if build_args is None:
build_args = dict() build_args = dict()
labels = [f"{repository}:{tag}" for tag in tags] labels = [f"{repository}:{tag}" for tag in tags]
@ -11,7 +12,7 @@ def buildx(repository: str, tags: list[str], build_platforms: list[str], dockerf
"--platform", ",".join(build_platforms), "--platform", ",".join(build_platforms),
*[t for (key, value) in build_args.items() *[t for (key, value) in build_args.items()
for t in ("--build-arg", f"{key}={value}")], for t in ("--build-arg", f"{key}={value}")],
"--file", dockerfile, "--file", str(dockerfile),
*[t for label in labels for t in ("--tag", label)], *[t for label in labels for t in ("--tag", label)],
f"--progress={progress}", f"--progress={progress}",
"--pull" if pull else None, "--pull" if pull else None,
@ -21,11 +22,20 @@ def buildx(repository: str, tags: list[str], build_platforms: list[str], dockerf
print(" ".join(command)) print(" ".join(command))
run(command, check=True) run(command, check=True)
def get_webhook():
try:
with open("portainer-webhook.txt", "r") as f:
return f.read().strip()
except Exception:
return None
if __name__ == '__main__': if __name__ == '__main__':
output_path = Path("dist") output_path = Path("dist")
if output_path.exists(): if output_path.exists():
rmtree(output_path) rmtree(output_path)
output_path.mkdir() output_path.mkdir()
dockerfile = Path("Dockerfile.local").resolve()
run(["python", "createreadmes.py"], check=True) run(["python", "createreadmes.py"], check=True)
run(["bundle.bat", "exec", "jekyll", "build", "--destination", output_path], check=True) run(["bundle.bat", "exec", "jekyll", "build", "--destination", output_path], check=True)
branch = run(["git", "branch", "--show-current"], branch = run(["git", "branch", "--show-current"],
@ -34,4 +44,20 @@ if __name__ == '__main__':
stdout=PIPE, check=True).stdout.decode().strip() stdout=PIPE, check=True).stdout.decode().strip()
platforms = ['linux/amd64', 'linux/arm/v6', 'linux/arm/v7', platforms = ['linux/amd64', 'linux/arm/v6', 'linux/arm/v7',
'linux/arm64/v8', 'linux/386', 'linux/ppc64le', 'linux/s390x'] 'linux/arm64/v8', 'linux/386', 'linux/ppc64le', 'linux/s390x']
buildx("chenio/code2dfd", [branch, short_sha], platforms, dockerfile="Dockerfile.local") buildx("chenio/code2dfd", [branch, short_sha], platforms, dockerfile=dockerfile)
webhook_id = get_webhook()
if webhook_id is not None:
print("Updating portainer stack...")
resp = post(f"https://docker.cnml.de/api/stacks/webhooks/{webhook_id}")
if not resp.ok:
try:
try:
error = resp.json()
except Exception:
error = resp.content.decode()
raise Exception(error)
raise Exception(f"{error['message']} ({error['details']})")
except Exception as e:
print("Failed to update:", e)
else:
print("Stack successfully updated!")