Skip to content

Commit

Permalink
Update k8s-snap pre-release branch names
Browse files Browse the repository at this point in the history
We'll use a single branch for all pre-releases of a given risk level,
e.g. v1.33.0-alpha.0 -> autoupdate/v1.33.0-alpha.

This commit also removes unused k8s_release.py functions that were
inherited from the k8s-snap script.
  • Loading branch information
petrutlucian94 committed Jan 8, 2025
1 parent f6964be commit f4ef36f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update-pre-release-branches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
echo "preRelease=$preRelease" >> "$GITHUB_OUTPUT"
if [[ -n "$preRelease" ]]; then
gitBranch="autoupdate/$preRelease"
gitBranch=`python3 ./scripts/k8s_release.py get_prerelease_git_branch --prerelease $preRelease`
fi
echo "gitBranch=$gitBranch" >> "$GITHUB_OUTPUT"
handle-pre-release:
Expand Down
5 changes: 4 additions & 1 deletion scripts/ensure_snap_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ def ensure_lp_recipe(
f"Unsupported pre-release flavour: {flavour}, only 'classic' "
"pre-releases are supported."
)
# Use a single branch for all pre-releases of a given risk level,
# e.g. v1.33.0-alpha.0 -> autoupdate/v1.33.0-alpha
prerelease = ver.prerelease.split(".")[0]
flavor_branch = (
f"autoupdate/v{ver.major}.{ver.minor}.{ver.patch}-{ver.prerelease}"
f"autoupdate/v{ver.major}.{ver.minor}.{ver.patch}-{prerelease}"
)
elif tip:
flavor_branch = "main" if flavour == "classic" else f"autoupdate/{flavour}"
Expand Down
38 changes: 14 additions & 24 deletions scripts/k8s_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
import json
import logging
import os
import re
import subprocess
from typing import List, Optional

Expand Down Expand Up @@ -98,24 +98,16 @@ def _branch_exists(
return branch_name in stdout


def clean_obsolete_git_branches(project_basedir: Optional[str] = None, remote="origin"):
"""Remove obsolete pre-release git branches."""
latest_release = get_latest_release()
LOG.info("Latest k8s release: %s", latest_release)

_exec(["git", "fetch", remote], cwd=project_basedir)

obsolete_prereleases = get_obsolete_prereleases()
for outstanding_prerelease in obsolete_prereleases:
branch = f"autoupdate/{outstanding_prerelease}"
def get_prerelease_git_branch(prerelease: str):
"""Retrieve the name of the k8s-snap git branch for a given k8s pre-release."""
prerelease_re = r"v\d+\.\d+\.\d-(?:alpha|beta|rc)\.\d+"
if not re.match(prerelease_re, prerelease):
raise ValueError("Unexpected k8s pre-release name: %s", prerelease)

if _branch_exists(
f"{remote}/{branch}", remote=True, project_basedir=project_basedir
):
LOG.info("Cleaning up obsolete pre-release branch: %s", branch)
_exec(["git", "push", remote, "--delete", branch], cwd=project_basedir)
else:
LOG.info("Obsolete branch not found, skpping: %s", branch)
# Use a single branch for all pre-releases of a given risk level,
# e.g. v1.33.0-alpha.0 -> autoupdate/v1.33.0-alpha
branch = f"autoupdate/{prerelease}"
return re.sub(r"(-[a-zA-Z]+)\.[0-9]+", r"\1", branch)


def remove_obsolete_prereleases():
Expand All @@ -128,14 +120,12 @@ def remove_obsolete_prereleases():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="subparser", required=True)

cmd = subparsers.add_parser("clean_obsolete_git_branches")
cmd = subparsers.add_parser("get_prerelease_git_branch")
cmd.add_argument(
"--project-basedir",
dest="project_basedir",
help="The k8s-snap project base directory.",
default=os.getcwd(),
"--prerelease",
dest="prerelease",
help="The upstream k8s pre-release.",
)
cmd.add_argument("--remote", dest="remote", help="Git remote.", default="origin")

subparsers.add_parser("get_outstanding_prerelease")
subparsers.add_parser("remove_obsolete_prereleases")
Expand Down
2 changes: 1 addition & 1 deletion scripts/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SNAP_NAME: str = "k8s"
SNAP_REPO: str = "https://github.com/canonical/k8s-snap.git/"
TIP_BRANCH = re.compile(
r"^(?:main)|^(?:release-\d+\.\d+)$|^(?:autoupdate\/v\d+\.\d+\.\d+-(?:alpha|beta|rc).\d+)$"
r"^(?:main)|^(?:release-\d+\.\d+)$|^(?:autoupdate\/v\d+\.\d+\.\d+-(?:alpha|beta|rc))$"
)


Expand Down

0 comments on commit f4ef36f

Please sign in to comment.