From 661c6cc280ce84b8d5293112105e76fa09bf927d Mon Sep 17 00:00:00 2001 From: Ruben Talstra Date: Mon, 10 Feb 2025 19:34:30 +0100 Subject: [PATCH] chore(ci): Improve version bump logic and remove redundant steps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Now correctly determines the base version for version bumping: - Uses the version from an existing version bump PR if available. - Falls back to `package.json` version if no existing PR is found. - Ensures that the version is bumped based on the labels of the newly merged PR. - Closes an existing version bump PR before creating a new one to prevent duplicates. - Removed the redundant branch creation step as `create-pull-request@v7` fully manages it. - Ensures that every new version bump PR correctly increments beyond the last one. This improves automation reliability and prevents version conflicts. 🚀 --- .github/workflows/version-bump.yml | 31 +++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/.github/workflows/version-bump.yml b/.github/workflows/version-bump.yml index cb825eb5d2..5dc3bd88f0 100644 --- a/.github/workflows/version-bump.yml +++ b/.github/workflows/version-bump.yml @@ -28,12 +28,33 @@ jobs: echo "Last version: $LAST_VERSION" echo "LAST_VERSION=$LAST_VERSION" >> $GITHUB_ENV + - name: Check if an existing version bump PR exists + id: check_existing_pr + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + EXISTING_PR=$(gh pr list --state open --json number,title --jq '.[] | select(.title | contains("chore(version)")) | {number: .number, title: .title}') + EXISTING_PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number') + EXISTING_PR_VERSION=$(echo "$EXISTING_PR" | grep -oP '\d+\.\d+\.\d+' | head -n 1) + + if [[ -n "$EXISTING_PR_NUMBER" ]]; then + echo "Closing existing PR #$EXISTING_PR_NUMBER" + gh pr close "$EXISTING_PR_NUMBER" --comment "🚀 Auto-closing this PR in favor of a new version bump." + echo "EXISTING_PR_NUMBER=$EXISTING_PR_NUMBER" >> $GITHUB_ENV + fi + + if [[ -n "$EXISTING_PR_VERSION" ]]; then + echo "EXISTING_PR_VERSION=$EXISTING_PR_VERSION" >> $GITHUB_ENV + fi + - name: Determine new version based on PR labels id: bump_version env: PR_LABELS: ${{ join(github.event.pull_request.labels.*.name, ' ') }} + BASE_VERSION: ${{ env.EXISTING_PR_VERSION || env.LAST_VERSION }} run: | echo "Labels on PR: $PR_LABELS" + echo "Base version: $BASE_VERSION" MAJOR=false MINOR=false @@ -49,14 +70,14 @@ jobs: PATCH=true fi - NEW_VERSION=$LAST_VERSION + NEW_VERSION=$BASE_VERSION if [ "$MAJOR" = true ]; then - NEW_VERSION=$(semver -i major $LAST_VERSION) + NEW_VERSION=$(semver -i major $BASE_VERSION) elif [ "$MINOR" = true ]; then - NEW_VERSION=$(semver -i minor $LAST_VERSION) + NEW_VERSION=$(semver -i minor $BASE_VERSION) elif [ "$PATCH" = true ]; then - NEW_VERSION=$(semver -i patch $LAST_VERSION) + NEW_VERSION=$(semver -i patch $BASE_VERSION) fi echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV @@ -71,7 +92,7 @@ jobs: npm install npm ci - - name: Create or Update Pull Request + - name: Create New Pull Request uses: peter-evans/create-pull-request@v7 with: token: ${{ secrets.GITHUB_TOKEN }}