Skip to content

Commit

Permalink
✨(release) add python script to prepare release
Browse files Browse the repository at this point in the history
Change versions, update changelog and create branch and commit
to submit release informations.
Give following instructions to do after.
  • Loading branch information
sdemagny committed Sep 6, 2024
1 parent 4fe7473 commit 5164427
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions release_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import datetime
import os
import subprocess
import sys


def run_command(cmd, msg=None, shell=False, cwd='.', stdout=None):
if msg is None:
msg = f"# Running: {cmd}"
if stdout is not None:
stdout.write(msg + '\n')
if stdout != sys.stdout:
print(msg)
subprocess.check_call(cmd, shell=shell, cwd=cwd, stdout=stdout, stderr=stdout)
if stdout is not None:
stdout.flush()


def update_all_file_version(release):
# pyproject.toml
print("Update pyproject.toml...")
path = "src/backend/pyproject.toml"
with open(path, 'r') as file:
lines = file.readlines()
for index, line in enumerate(lines):
if line.startswith("version = "):
last_release = line.split("=")[-1]
new_line = line.replace(last_release, f' "{release}"\n')
lines[index] = new_line
with open(path, 'w+') as file:
file.writelines(lines)

# helm files
print("Update helm files...")
for env in ["preprod", "production"]:
path = f"src/helm/env.d/{env}/values.desk.yaml.gotmpl"
with open(path, 'r') as file:
lines = file.readlines()
for index, line in enumerate(lines):
if line.startswith(" tag: "):
lines[index] = f' tag: "v{release}"\n'
with open(path, 'w+') as file:
file.writelines(lines)

# frontend package.json
print("Update package.json...")
files_to_modify = []
filename = "package.json"
for root, _dir, files in os.walk("src/frontend"):
if filename in files and "node_modules" not in root and ".next" not in root:
files_to_modify.append(os.path.join(root, filename))

for path in files_to_modify:
with open(path, 'r') as file:
lines = file.readlines()
for index, line in enumerate(lines):
if line.startswith(' "version": '):
lines[index] = f' "version": "{release}",\n'
with open(path, 'w+') as file:
file.writelines(lines)
return


def tag_release(release):
return


def update_changelog_with_release_info(path, release):
"""...
"""
print("Update CHANGELOG...")
with open(path, 'r') as file:
lines = file.readlines()
for index, line in enumerate(lines):
if "## [Unreleased]" in line:
today = datetime.date.today()
lines.insert(index + 1, f"\n## [{release}] - {today}\n")
if line.startswith("[unreleased]"):
last_release = lines[index + 1].split("]")[0][1:]
new_unreleased_line = line.replace(last_release, release)
new_release_line = lines[index + 1].replace(last_release, release)
lines[index] = new_unreleased_line
# last_comparison = lines[index + 1]
# new = last_comparison.replace(last_release, release)
# release_line = "/".join([*new.split("/")[:-1], f"v{last_release}...v{release}\n"])
lines.insert(index + 1, new_release_line)
break

with open(path, 'w+') as file:
file.writelines(lines)


if __name__ == "__main__":
print('Enter your release version:')
version = input()
print('Enter kind of release (p:patch, m:minor, mj:major):')
kind = input()
print('Let\'s go to deployment branch to release')
branch_to_release = f"release/{version}"
run_command(f"git checkout -b {branch_to_release}", shell=True)
run_command("git pull --rebase origin main", shell=True)

update_changelog_with_release_info("CHANGELOG.md", version)
update_all_file_version(version)

run_command("git add CHANGELOG.md", shell=True)
run_command("git add src/", shell=True)
release_kind = {'p': 'patch', 'm': 'minor', 'mj': 'major'}
message = f"""🔖({release_kind[kind]}) release version {version}
Update all version files and changelog for release {release_kind[kind]}."""
run_command(f"git ci -m '{message}'", shell=True)
check_ok = input(f"\nNEXT COMMAND: \n>> git push origin {branch_to_release}\nContinue ? (y,n) ")
if check_ok == 'y':
print(f"git push origin {branch_to_release}")
run_command(f"git push origin {branch_to_release}", shell=True)
print(f"""
PLEASE DO THE FOLLOWING INSTRUCTIONS:
--> Please submit PR and merge code to main than tag version
>> git tag -a v{version}
>> git push origin v{version}
--> Please check docker image: https://hub.docker.com/r/lasuite/people-backend/tags"
>> git tag -d preprod
>> git tag preprod
>> git push -f origin preprod
--> Now please generate release on github interface for the current tag v{version}"
""")

0 comments on commit 5164427

Please sign in to comment.