Skip to content

Commit 12fbc4e

Browse files
authored
Create/delete release branches automatically (#1488)
* Create/delete release branches automatically * Update gha: cleanup old releases * Fix path * Remove version from local workflow path * Adjust comments * Fix missing permission * Address comments * comments * Fix env
1 parent 8e6e3e4 commit 12fbc4e

5 files changed

Lines changed: 131 additions & 4 deletions

File tree

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- uses: actions/setup-go@v5
2929
with:
3030
go-version: '1.25'
31-
- uses: actions/checkout@v4
31+
- uses: actions/checkout@v5
3232
- uses: gardener/cc-utils/.github/actions/setup-git-identity@master
3333
with:
3434
remove-trusted-label: false
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Reusable - Create Branch
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
version:
7+
required: true
8+
type: string
9+
secrets:
10+
token:
11+
required: true
12+
13+
jobs:
14+
create-branch:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
steps:
19+
- name: Checkout repo
20+
uses: actions/checkout@v5
21+
22+
- name: Setup Git identity # Configure git with a user name and email
23+
uses: gardener/cc-utils/.github/actions/setup-git-identity@master
24+
25+
- name: Create new branch
26+
run: |
27+
# Validate input
28+
if [ -z "${{ inputs.version }}" ]; then
29+
echo "ERROR: version input is empty"
30+
exit 1
31+
fi
32+
33+
VERSION="${{ inputs.version }}"
34+
# Remove last .X (patch) from the version
35+
MINOR_VERSION="${VERSION%.*}"
36+
BRANCH_NAME="release-${MINOR_VERSION}"
37+
38+
# Abort if branch already exists
39+
if git ls-remote --exit-code --heads origin "${BRANCH_NAME}" >/dev/null 2>&1; then
40+
echo "Branch ${BRANCH_NAME} already exists on origin. Exiting."
41+
exit 0
42+
fi
43+
git checkout -b "${BRANCH_NAME}"
44+
45+
# Compute next development version
46+
NEXT_VERSION="${MINOR_VERSION}.1"
47+
echo "${NEXT_VERSION}-dev" > VERSION
48+
49+
# Generate artifacts
50+
make generate
51+
52+
# Commit and push changes
53+
git add --all
54+
git commit -m "[bot] Prepare next Development Cycle $NEXT_VERSION"
55+
git push origin $BRANCH_NAME
56+
env:
57+
GITHUB_TOKEN: ${{ secrets.token }}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Cleanup Old Releases
2+
3+
on:
4+
schedule:
5+
# Runs every Sunday at 12:00 AM UTC
6+
- cron: '0 0 * * 0'
7+
8+
workflow_dispatch: # Optional manual trigger
9+
10+
permissions:
11+
contents: write # This allows deleting branches
12+
13+
jobs:
14+
cleanup:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Install jq
18+
run: sudo apt-get update && sudo apt-get install -y jq
19+
20+
- name: Cleanup old branches
21+
env:
22+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
GH_REPO: ${{ github.repository }}
24+
run: |
25+
#!/bin/bash
26+
set -euo pipefail
27+
28+
# Fetch the latest release tag
29+
latest_tag=$(gh release list --exclude-pre-releases --limit 1 | awk '{print $1}')
30+
31+
if [[ $latest_tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
32+
echo "Latest release: $latest_tag"
33+
else
34+
echo "Latest release tag '$latest_tag' is NOT a valid semver (vX.Y.Z)"
35+
exit 0
36+
fi
37+
38+
# Parse major and minor
39+
IFS='.' read -r major minor patch <<<"${latest_tag#v}"
40+
41+
# Check if minor version is smaller than 3
42+
if [[ $minor -lt 3 ]]; then
43+
echo "Latest minor version ($minor) is smaller than 3. No branches or milestones to delete."
44+
exit 0
45+
fi
46+
47+
cutoff_minor=$((minor - 3))
48+
echo "Will delete all release branches <= v$major.$cutoff_minor"
49+
50+
# List all branches and filter ones that match release-vX.Y
51+
for branch in $(gh api repos/$GH_REPO/branches --paginate --jq '.[].name'); do
52+
if [[ $branch =~ ^release-v([0-9]+)\.([0-9]+) ]]; then
53+
b_major=${BASH_REMATCH[1]}
54+
b_minor=${BASH_REMATCH[2]}
55+
56+
if [[ $b_major -eq $major && $b_minor -le $cutoff_minor ]]; then
57+
echo "Deleting old branch: $branch"
58+
gh api -X DELETE "repos/$GH_REPO/git/refs/heads/$branch" || true
59+
fi
60+
fi
61+
done

.github/workflows/release-milestone.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Check Release Milestone
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v5
1717
with:
1818
path: './'
1919

.github/workflows/release.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,14 @@ jobs:
5959
with:
6060
name: ${{ needs.release-to-github-and-bump.outputs.component-descriptor.version }}
6161
strip_patch: true
62-
secrets:
63-
token: ${{ secrets.GITHUB_TOKEN }}
62+
secrets: inherit
63+
64+
create-release-branch:
65+
if: ${{ inputs.next-version == 'bump-minor' && inputs.release-version == 'set-prerelease' }}
66+
needs:
67+
- build
68+
- release-to-github-and-bump
69+
uses: ./.github/workflows/create-branch.yaml
70+
with:
71+
version: ${{ needs.release-to-github-and-bump.outputs.component-descriptor.version }}
72+
secrets: inherit

0 commit comments

Comments
 (0)