|
6 | 6 | import subprocess |
7 | 7 | import sys |
8 | 8 |
|
| 9 | + |
9 | 10 | def get_image_digest(image_name, tag): |
10 | 11 | result = subprocess.run( |
11 | 12 | ["docker", "buildx", "imagetools", "inspect", f"{image_name}:{tag}"], |
12 | | - stdout=subprocess.PIPE, check=True, text=True |
| 13 | + stdout=subprocess.PIPE, |
| 14 | + check=True, |
| 15 | + text=True, |
13 | 16 | ) |
14 | 17 | for line in result.stdout.splitlines(): |
15 | 18 | if line.strip().startswith("Digest:"): |
16 | 19 | return line.strip().split(":", 1)[1].strip() |
17 | 20 | return None |
18 | 21 |
|
| 22 | + |
19 | 23 | def build_and_push_image(dest_dir, image_name, tag): |
20 | 24 | # Build |
21 | | - subprocess.run([ |
22 | | - "docker", "buildx", "build", "--platform", "linux/amd64", "-t", f"{image_name}:{tag}", dest_dir |
23 | | - ], check=True) |
| 25 | + subprocess.run( |
| 26 | + ["docker", "buildx", "build", "--platform", "linux/amd64", "-t", f"{image_name}:{tag}", dest_dir], check=True |
| 27 | + ) |
24 | 28 | # Push |
25 | | - subprocess.run([ |
26 | | - "docker", "push", f"{image_name}:{tag}" |
27 | | - ], check=True) |
| 29 | + subprocess.run(["docker", "push", f"{image_name}:{tag}"], check=True) |
28 | 30 | print(f"Pushed {image_name}:{tag}") |
29 | 31 | digest = get_image_digest(image_name, tag) |
30 | 32 | print(f"Image digest: {digest}") |
31 | 33 | with open("/tmp/digest.txt", "w") as f: |
32 | 34 | f.write(digest) |
33 | 35 |
|
| 36 | + |
34 | 37 | if __name__ == "__main__": |
35 | 38 | if len(sys.argv) != 3: |
36 | 39 | print("Usage: python build_and_publish_docker.py <ghcr_username/repo> <tag>") |
|
0 commit comments