Skip to content

Commit befc91c

Browse files
Roll back tag on release error
So release can be retried. * Extract upload/push side-effects from "verify" function
1 parent c74ef97 commit befc91c

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

scripts/release.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Prerequisites:
1414
- This must be run from the root of the repository.
1515
- The repo must have a clean git working tree.
16-
- The user must have the GITHUB_TOKEN environment variable set to a valid GitHub personal access token.
16+
- The user must have the GITHUB_TOKEN environment variable set to a GitHub personal access token with repository "Contents" read and write permission.
1717
- The user will need credentials for the PyPI repository, which the user will be prompted for during the upload step. The user will need to paste the token manually from a password manager or similar.
1818
- The CHANGELOG.md file must already contain an entry for the version being released.
1919
- Install requirements with: pip install --upgrade --editable '.[release]'
@@ -22,19 +22,27 @@
2222

2323
from __future__ import annotations
2424

25+
import contextlib
2526
import os
2627
import re
2728
import subprocess
2829
import sys
30+
from collections.abc import Iterator
2931
from pathlib import Path
3032

3133
import requests
3234

3335

34-
def add_git_tag_for_version(version: str) -> None:
36+
@contextlib.contextmanager
37+
def add_git_tag_for_version(version: str) -> Iterator[None]:
3538
"""Add a git tag for the given version."""
3639
subprocess.run(["git", "tag", "-a", version, "-m", version], check=True)
3740
print(f"Version {version} tag added successfully.")
41+
try:
42+
yield
43+
except Exception:
44+
subprocess.run(["git", "tag", "-d", version])
45+
raise
3846

3947

4048
def remove_previous_dist() -> None:
@@ -68,8 +76,6 @@ def verify_build(is_test: str) -> None:
6876
confirmation = input("Does the build look correct? (y/n): ")
6977
if confirmation == "y":
7078
print("Build verified successfully.")
71-
upload_build_to_pypi(is_test)
72-
push_git_tags()
7379
else:
7480
raise Exception("Could not verify. Build was not uploaded.")
7581

@@ -227,10 +233,12 @@ def main() -> None:
227233
is_test = get_is_test_response()
228234
version_number = input("Enter the version number: ")
229235

230-
add_git_tag_for_version(version_number)
231-
remove_previous_dist()
232-
create_build()
233-
verify_build(is_test)
236+
with add_git_tag_for_version(version_number):
237+
remove_previous_dist()
238+
create_build()
239+
verify_build(is_test)
240+
upload_build_to_pypi(is_test)
241+
push_git_tags()
234242
create_github_release_draft(github_token, version_number)
235243

236244

0 commit comments

Comments
 (0)