Skip to content

Instantly share code, notes, and snippets.

@0xa
Created December 21, 2020 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xa/f8b72dd16bb0300b740f3140984b4a85 to your computer and use it in GitHub Desktop.
Save 0xa/f8b72dd16bb0300b740f3140984b4a85 to your computer and use it in GitHub Desktop.
#!/bin/python3
import yaml
import docker
from docker.types import Mount
import sys
import os
with open(".gitlab-ci.yml") as f:
config = yaml.safe_load(f)
key = sys.argv[1]
build = config[key]
cwd = os.getcwd()
root = "/" + os.path.basename(cwd)
wait_cmd = "sleep 600"
client = docker.from_env()
print("~", "running image", build['image'])
print("~", f"with {cwd} as {root}")
c = client.containers.run(build['image'], command=wait_cmd, detach=True, mounts=[
Mount(target=root, source=cwd, type='bind')
])
angry_token = "___IFUCKINGHATEDOCKERPICEOFSHITxXxFUCK___"
def run(s):
print(">", s)
r = c.exec_run(["sh", "-c", f"cd {root} && {s} || echo {angry_token}"], stream=True)
for line in r.output:
s = line.decode('utf-8', 'ignore')
if angry_token in s:
raise Exception("exited with an error")
print(s, end="")
if r.exit_code != 0 and r.exit_code is not None:
raise Exception(f"exited with code {r.exit_code}")
for s in config.get('before_script', []):
run(s)
for s in build['script']:
run(s)
c.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment