Skip to content

Commit

Permalink
Automatically manage pre-release branches
Browse files Browse the repository at this point in the history
We need to automatically create k8s-snap branches for upstream
pre-releases such as v1.33.0-alpha.1.

Whenever a new pre-release or stable release comes out, the old
pre-release branches become obsolete and are automatically
removed.

Note that these branches are used by the Launchpad recipes to
produce and publish k8s-snap builds.
  • Loading branch information
petrutlucian94 committed Dec 18, 2024
1 parent 3f30d5b commit ea5541f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Auto-update branches
name: Auto-update flavor branches

on:
push:
Expand Down Expand Up @@ -45,3 +45,4 @@ jobs:
- name: Push to ${{ steps.determine.outputs.branch }}
run: |
git push origin --force ${{ steps.determine.outputs.branch }}
63 changes: 63 additions & 0 deletions .github/workflows/update-pre-release-branches.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Auto-update pre-release branches

on:
push:
branches:
- main
schedule:
- cron: "0 0 * * *" # Runs every midnight

permissions:
contents: read

jobs:
update-branches:
permissions:
contents: write # for Git to git push
runs-on: ubuntu-latest
outputs:
preRelease: ${{ steps.determine.outputs.preRelease }}
branch: ${{ steps.determine.outputs.branch }}
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install Python dependencies
run: pip3 install packaging requests
- name: Checkout repository
uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.BOT_SSH_KEY }}
- name: Determine outstanding pre-release
id: determine
run: |
preRelease=`python3 ./build-scripts/k8s_releases.py get_outstanding_prerelease`
echo "preRelease=$preRelease" >> "$GITHUB_OUTPUT"
if [[ -n "$preRelease" ]]; then
branch="autoupdate/$preRelease"
fi
echo "branch=$branch" >> "$GITHUB_OUTPUT"
- name: Create pre-release branch ${{ steps.determine.outputs.branch }}
if: ${{ steps.determine.outputs.preRelease }} != ''
run: |
git checkout -b ${{ steps.determine.outputs.branch }}
git push origin --force ${{ steps.determine.outputs.branch }}
- name: Clean obsolete branches
run: |
git fetch origin
# Log the latest release for reference.
latestRelease=`python3 ./build-scripts/k8s_releases.py get_latest_release`
echo "Latest k8s release: $latestRelease"
for outstandingPreRelease in `python3 ./build-scripts/k8s_releases.py get_obsolete_prereleases`; do
branch="autoupdate/${outstandingPreRelease}"
if git branch -r | grep "origin/$branch"; then
echo "Cleaning up obsolete pre-release branch: $branch"
git push origin --delete $branch
else
echo "Obsolete branch not found, skpping: $branch"
fi
done
16 changes: 12 additions & 4 deletions build-scripts/k8s_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def get_k8s_tags() -> List[str]:


# k8s release naming:
# * alpha: v{major}.{minor}.{patch}r-alpha.{version}
# * beta: v{major}.{minor}.{patch}r-beta.{version}
# * rc: v{major}.{minor}.{patch}r-rc.{version}
# * alpha: v{major}.{minor}.{patch}-alpha.{version}
# * beta: v{major}.{minor}.{patch}-beta.{version}
# * rc: v{major}.{minor}.{patch}-rc.{version}
# * stable: v{major}.{minor}.{patch}
def is_stable_release(release: str):
return "-" not in release
Expand All @@ -50,6 +50,14 @@ def get_latest_release() -> str:
return k8s_tags[0]


def get_outstanding_prerelease() -> str:
latest_release = get_latest_release()
if not is_stable_release(latest_release):
return latest_release
# The latest release is a stable release, no outstanding pre-release.
return None


def get_obsolete_prereleases() -> List[str]:
"""Return obsolete K8s pre-releases.
Expand All @@ -75,4 +83,4 @@ def get_obsolete_prereleases() -> List[str]:
for item in out:
print(item)
else:
print(out)
print(out or "")

0 comments on commit ea5541f

Please sign in to comment.