-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.py
72 lines (61 loc) · 2.32 KB
/
deploy.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
import subprocess
import sys
from argparse import ArgumentParser
def create_git_tag(version):
tag_name = f"v{version}"
result = subprocess.run(["git", "tag", tag_name])
if result.returncode != 0:
raise RuntimeError("Failed to create git tag.")
result = subprocess.run(["git", "push", "origin", tag_name])
if result.returncode != 0:
raise RuntimeError("Failed to push git tag.")
else:
print(f"Git tag {tag_name} created and pushed.")
def build_package():
res = subprocess.run(["python", "-m", "build"])
if res.returncode != 0:
raise RuntimeError("Failed to build package.")
print("Package built successfully.")
def publish_package(version):
res = subprocess.run(["twine", "upload", f"dist/*{version}*"])
if res.returncode != 0:
raise RuntimeError("Failed to publish package.")
print("Package published successfully.")
def main():
parser = ArgumentParser()
parser.add_argument("version", type=str, help="The version to deploy")
parser.add_argument(
"--publish",
action="store_true",
help="Only publish the package to PyPI, do not create a git tag",
default=False,
)
args = parser.parse_args()
version = args.version
res = subprocess.run(["git", "tag", "-l", "v*"], capture_output=True, text=True)
last_tag = res.stdout.strip().split("\n")[-1]
print(f"Last tag: {last_tag}")
confirm = input(f"Deploying version {version}. Would you like to continue? (y/n)")
if confirm.lower() != "y":
print("Deployment cancelled.")
return
if "v" in version:
raise ValueError("Version should not contain 'v'.")
if not args.publish:
if last_tag == "v" + version:
overwrite = input(
"Git tag already exists, would you like to overwrite it? (y/n) "
)
if overwrite.lower() != "y":
print("Deployment cancelled.")
return
else:
print("Overwriting git tag.")
subprocess.run(["git", "tag", "-d", last_tag])
subprocess.run(["git", "push", "origin", "tag", "-d", last_tag])
print("Git tag overwritten.")
create_git_tag(version)
build_package()
publish_package(version)
if __name__ == "__main__":
main()