Last active
November 26, 2021 10:20
-
-
Save mindflayer/90dc8d88db29e86f30a856c093d80423 to your computer and use it in GitHub Desktop.
Spawn a deploy for a DO Docker Registry image `tag` for a specific `app` on DigitalOcean App Platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import os | |
import subprocess | |
ENCODING = "utf-8" | |
def check_output(cmd, split_lines=False): | |
output = subprocess.check_output(cmd.split()).decode(ENCODING).strip() | |
return output.split("\n") if split_lines else output | |
parser = argparse.ArgumentParser(description="Automate deployments for DigitalOcean.") | |
parser.add_argument( | |
"--app", type=str, required=True, help="DigitalOcean app name (e.g. kiwi-back-stg)" | |
) | |
parser.add_argument( | |
"--tag", | |
type=str, | |
required=True, | |
help="Unique tag of the images you want to deploy (e.g. latest)", | |
) | |
force_all = False | |
try: | |
parser.add_argument( | |
"--services", default=True, action=argparse.BooleanOptionalAction | |
) | |
parser.add_argument( | |
"--workers", default=True, action=argparse.BooleanOptionalAction | |
) | |
parser.add_argument("--jobs", default=True, action=argparse.BooleanOptionalAction) | |
except AttributeError: | |
force_all = True | |
args = parser.parse_args() | |
app_lines = check_output("doctl apps list", split_lines=True) | |
for line in app_lines: | |
parts = line.split() | |
app_id, app_name, _ = parts[0], parts[1], parts[2:] | |
if args.app == app_name: | |
break | |
app_spec = json.loads(check_output(f"doctl apps spec get {app_id} --format json")) | |
if force_all or args.services: | |
for entry in app_spec["services"]: | |
if "image" in entry and entry["image"]["registry_type"] == "DOCR": | |
entry["image"]["tag"] = args.tag | |
if force_all or args.workers: | |
for entry in app_spec["workers"]: | |
if "image" in entry and entry["image"]["registry_type"] == "DOCR": | |
entry["image"]["tag"] = args.tag | |
if force_all or args.jobs: | |
for entry in app_spec["jobs"]: | |
if "image" in entry and entry["image"]["registry_type"] == "DOCR": | |
entry["image"]["tag"] = args.tag | |
new_app_spec_filename = f"{app_name}.spec.json" | |
with open(new_app_spec_filename, mode="w", encoding=ENCODING) as new_app_spec: | |
new_app_spec.write(json.dumps(app_spec)) | |
check_output(f"doctl apps update {app_id} --spec {new_app_spec_filename}") | |
os.unlink(new_app_spec_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment