-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement reuse of base images across jobs
- Loading branch information
1 parent
f35d4bc
commit 9156678
Showing
6 changed files
with
249 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Export bake artifacts as tar files for later reuse. | ||
./export_bake_artifacts.py --file <build definition> --target <build target or group> --output-path <output path> | ||
""" | ||
|
||
import argparse | ||
import json | ||
import subprocess | ||
from pathlib import Path | ||
|
||
PROJECT_DIR = Path(__file__).resolve().parents[1] | ||
|
||
|
||
parser = argparse.ArgumentParser( | ||
description="Export one or more bake artifacts to tar files for reuse" | ||
) | ||
parser.add_argument("--file", default="docker-bake.hcl") | ||
parser.add_argument("--target", default="default") | ||
parser.add_argument("--output-path", default=PROJECT_DIR / ".out") | ||
|
||
|
||
def get_bake_plan(bake_file="docker-bake.hcl", target="default"): | ||
cmd = ["docker", "buildx", "bake", "-f", str(PROJECT_DIR / bake_file), "--print", target] | ||
p = subprocess.run(cmd, capture_output=True) | ||
if p.returncode != 0: | ||
print(f"Failed to get bake plan: {p.stderr}") | ||
exit(1) | ||
return json.loads(p.stdout.decode("utf-8")) | ||
|
||
|
||
def build_export_command(target_name, target_spec, output_path): | ||
output_file = Path(output_path) / f"{target_name}.tar" | ||
cmd = [ | ||
"docker", | ||
"image", | ||
"save", | ||
"--output", | ||
f"{output_file}", | ||
" ".join(target_spec["tags"]), | ||
] | ||
return cmd | ||
|
||
|
||
def run_cmd(target_name, cmd): | ||
p = subprocess.run(" ".join(cmd), shell=True) | ||
if p.returncode != 0: | ||
print(f"{target_name} failed to export: {p.returncode}") | ||
return p.returncode | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
plan = get_bake_plan(args.file, args.target) | ||
output = args.output_path | ||
if not Path(output).exists(): | ||
Path(output).mkdir(parents=True) | ||
print(f"Exporting {len(plan['target'].keys())} targets: {plan['target'].keys()}") | ||
for target_name, target_spec in plan["target"].items(): | ||
print(f"Exporting {target_name}") | ||
cmd = build_export_command(target_name, target_spec, output) | ||
run_cmd(target_name, cmd) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Run tests against bake artifacts by group/target and build definition. | ||
./test_bake_artifacts.py --file <build definition> --target <build target or group> | ||
""" | ||
|
||
import argparse | ||
import json | ||
import subprocess | ||
from pathlib import Path | ||
|
||
PROJECT_DIR = Path(__file__).resolve().parents[1] | ||
|
||
|
||
parser = argparse.ArgumentParser( | ||
description="Import one or more bake artifacts from tar files for reuse" | ||
) | ||
parser.add_argument("--archive-path", type=Path, default=PROJECT_DIR / ".out") | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
if args.archive_path: | ||
for archive in args.archive_path.glob("*.tar"): | ||
print(f"Importing {archive}") | ||
cmd = ["docker", "image", "load", "--input", archive] | ||
p = subprocess.run(cmd) | ||
if p.returncode != 0: | ||
print(f"Failed to import {archive}: {p.returncode}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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