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 19, 2024
1 parent 3f30d5b commit f81a49d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 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 }}
77 changes: 77 additions & 0 deletions .github/workflows/update-pre-release-branches.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Auto-update pre-release branches

on:
push:
branches:
- main
pull_request:
paths:
- .github/workflows/update-pre-release-branches.yaml
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: Checkout repository
uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.BOT_SSH_KEY }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install Python dependencies
run: pip3 install packaging requests
- 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: Define git credentials
run: |
# Needed to create commits.
git config --global user.name "Github Actions"
git config --global user.email "worker@org.com"
- name: 'Update k8s component version: ${{ steps.determine.outputs.preRelease }}'
if: ${{ steps.determine.outputs.preRelease }} != ''
run: |
echo ${{ steps.determine.outputs.preRelease }} > ./build-scripts/components/kubernetes/version
git add ./build-scripts/components/kubernetes/version
git commit -m "Update k8s version to ${{ steps.determine.outputs.preRelease }}"
- 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
25 changes: 17 additions & 8 deletions build-scripts/k8s_releases.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/usr/bin/env python3

from packaging.version import Version
import json
import requests
import sys
from typing import List
from typing import List, Optional

import requests
from packaging.version import Version

K8S_TAGS_URL = "https://api.github.com/repos/kubernetes/kubernetes/tags"


def _url_get(url: str) -> str:
r = requests.get(url)
r = requests.get(url, timeout=5)
r.raise_for_status()
return r.text

Expand All @@ -28,9 +29,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 +51,14 @@ def get_latest_release() -> str:
return k8s_tags[0]


def get_outstanding_prerelease() -> Optional[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 +84,4 @@ def get_obsolete_prereleases() -> List[str]:
for item in out:
print(item)
else:
print(out)
print(out or "")

0 comments on commit f81a49d

Please sign in to comment.