Skip to content

Commit

Permalink
ci(helm): update helm chart version fix
Browse files Browse the repository at this point in the history
When pushing tags (annotated?), github's event.base_ref may not be set.
When this happens, we can figure out the released branch ourselves, by
inspecting all branches containing the tag, and ensuring it's the tip
and it's the current HEAD.

Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Jan 14, 2025
1 parent 3fe353c commit afe85fb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/release-chart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ jobs:
run: |
tag="${{ github.ref_name }}"
BASE_REF="${{ github.event.base_ref }}"
BRANCH="${BASE_REF#refs/heads/}"
if [ -n "$BASE_REF" ]; then
BRANCH="${BASE_REF#refs/heads/}"
else
BRANCH="$(nix-shell --pure --run "./scripts/helm/find-released-branch.sh "$tag"" ./scripts/helm/shell.nix)"
fi
echo "BASE_BRANCH=$BRANCH" >> $GITHUB_ENV
nix-shell --pure --run "./scripts/helm/publish-chart-yaml.sh --released "$tag" --released-branch "$BRANCH"" ./scripts/helm/shell.nix
nix-shell --pure --run "SKIP_GIT=1 ./scripts/helm/generate-readme.sh" ./scripts/helm/shell.nix
- name: Create Pull Request
Expand All @@ -73,7 +79,7 @@ jobs:
signoff: true
delete-branch: true
branch-suffix: "random"
base: ${{ github.event.base_ref }}
base: ${{ env.BASE_BRANCH }}
token: ${{ secrets.ORG_CI_GITHUB }}
- name: Approve Pull Request by CI Bot
if: ${{ steps.cpr.outputs.pull-request-number }}
Expand Down
55 changes: 55 additions & 0 deletions scripts/helm/find-released-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

SCRIPTDIR="$(dirname "$(realpath "${BASH_SOURCE[0]:-"$0"}")")"
ROOTDIR="$SCRIPTDIR/../.."

source "$ROOTDIR/scripts/utils/log.sh"

set -euo pipefail

# Check if the given branch matches the tag
# Example:
# release/2.7 matches tag v2.7.2, v2.7.2-rc.4, etc..
# release/2.7 does not match tag v2.6.0, etc...
tag_matches_branch() {
local tag="${1#v}"
local release_branch="$2"

branch_version="${release_branch#release/}"
if ! [[ "$branch_version" =~ ^[0-9]+.[0-9]+$ ]]; then
return 1
fi

if ! [[ "$tag" = "$branch_version"* ]]; then
return 1
fi
}

# For the given tag, find the branch which is compatible
# See tag_matches_branch for more information.
find_released_branch() {
local tag="$1"
local branches=$(git branch -r --contains "$TAG" --points-at "$TAG" --format "%(refname:short)" 2>/dev/null)
local branch=""

for release_branch in $branches; do
release_branch=${release_branch#origin/}
if tag_matches_branch "$TAG" "$release_branch"; then
if [ -n "$branch" ]; then
log_fatal "Multiple branches matched!"
fi
branch="$release_branch"
fi
done

echo "$branch"
}

TAG="$1"
BRANCH="$(find_released_branch "$TAG")"

if [ -z "$BRANCH" ]; then
log_fatal "Failed to find matching released branch for tag '$TAG'"
fi

echo "$BRANCH"

0 comments on commit afe85fb

Please sign in to comment.