diff --git a/.github/workflows/update-branches.yaml b/.github/workflows/update-flavor-branches.yaml similarity index 97% rename from .github/workflows/update-branches.yaml rename to .github/workflows/update-flavor-branches.yaml index 3ac56592b..34e1e2935 100644 --- a/.github/workflows/update-branches.yaml +++ b/.github/workflows/update-flavor-branches.yaml @@ -1,4 +1,4 @@ -name: Auto-update branches +name: Auto-update flavor branches on: push: @@ -45,3 +45,4 @@ jobs: - name: Push to ${{ steps.determine.outputs.branch }} run: | git push origin --force ${{ steps.determine.outputs.branch }} + \ No newline at end of file diff --git a/.github/workflows/update-pre-release-branches.yaml b/.github/workflows/update-pre-release-branches.yaml new file mode 100755 index 000000000..8a86e28f2 --- /dev/null +++ b/.github/workflows/update-pre-release-branches.yaml @@ -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 diff --git a/build-scripts/k8s_releases.py b/build-scripts/k8s_releases.py index 7cb48a7f5..ddec88bbd 100755 --- a/build-scripts/k8s_releases.py +++ b/build-scripts/k8s_releases.py @@ -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 @@ -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 @@ -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. @@ -75,4 +84,4 @@ def get_obsolete_prereleases() -> List[str]: for item in out: print(item) else: - print(out) + print(out or "")