-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrun_gitlab_ci.py
executable file
·72 lines (60 loc) · 1.62 KB
/
run_gitlab_ci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
#
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/
# Written by Angelos Katharopoulos <[email protected]>
#
import argparse
import os
from os import path
from subprocess import call
import tempfile
import yaml
SCRIPT_TPL = """#!/bin/bash
git clone . {dir}/project
{commands}
"""
RESERVED_NAMES = [
"image",
"services",
"stages",
"types",
"before_script",
"after_script",
"variables",
"cache"
]
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Approximate running the scripts from the .gitlab-ci.yml"
)
parser.add_argument(
"--build_dir",
help="Set a build directory"
)
args = parser.parse_args()
# Make the temporary build directory
build_dir = tempfile.mkdtemp(dir=args.build_dir)
print("Building in", build_dir)
# Collect the commands from the yaml file
commands = []
pipeline = yaml.load(open(".gitlab-ci.yml"))
for stage in pipeline["stages"]:
for k in pipeline:
if k in RESERVED_NAMES:
continue
job = pipeline[k]
if not job.get("stage", "test") == stage:
continue
commands.append("cd {}/project".format(build_dir))
commands.extend(job["script"])
# Build the script
script = SCRIPT_TPL.format(
dir=build_dir,
commands="\n".join(commands)
)
handle, script_file = tempfile.mkstemp(dir=build_dir)
os.close(handle)
with open(script_file, "w") as f:
f.write(script)
# Execute it
call(["/bin/bash", script_file])