From f7279e2d8e0fc9c605b38d894eec8420b967320d Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Tue, 9 Dec 2025 03:47:06 +0000 Subject: [PATCH] feat: add automatic package.json version bump based on GitHub releases. - Added automatic package.json version bump to the publish extension workflow. It gets the release tag, validates that it's the correct format, removes any prerelease tags it may have (because vscode doesn't like prerelease tags in the extension versions), bump the version in package.json and package.lock with the version from the release tag, commits the changes, force pushes the tag reference to point to the new commit, and then publish the extension to the marketplaces. --- .github/workflows/publish-extension.yml | 126 +++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish-extension.yml b/.github/workflows/publish-extension.yml index 66b6101..a5293d3 100644 --- a/.github/workflows/publish-extension.yml +++ b/.github/workflows/publish-extension.yml @@ -1,17 +1,141 @@ +# The auto package.json version update code is adapted from: https://github.com/valeryan/vscode-phpsab/blob/v0.0.21/.github/workflows/publish.yml + on: release: types: [prereleased, released] +env: + TAG_NAME: ${{ github.ref_name }} + TARGET_BRANCH: ${{ github.event.release.target_commitish }} + +permissions: + contents: write + actions: read + name: Publish Extension to VS Code Marketplace and Open VSX Registry jobs: + validate-release-version: + name: Validate Release Version + runs-on: ubuntu-latest + if: github.event_name == 'release' + outputs: + version: ${{ steps.parse-version.outputs.version }} + is-valid: ${{ steps.parse-version.outputs.is-valid }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Parse and Validate Version + id: parse-version + run: | + TAG_NAME="${{ env.TAG_NAME }}" + + # Remove 'v' prefix if present and validate semver format + if [[ $TAG_NAME =~ ^v?([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9\.-]+)?(\+[a-zA-Z0-9\.-]+)?)$ ]]; then + VERSION=${BASH_REMATCH[1]} + echo "version=$VERSION" >> $GITHUB_OUTPUT + echo "is-valid=true" >> $GITHUB_OUTPUT + echo "✅ Valid version format: $VERSION" + else + echo "is-valid=false" >> $GITHUB_OUTPUT + echo "❌ Invalid version format: $TAG_NAME" + echo "Version must follow semver format (e.g., v1.0.0, 1.0.0, v1.0.0-beta.1)" + exit 1 + fi + + update-version: + name: Update Version + needs: validate-release-version + runs-on: ubuntu-latest + if: needs.validate-release-version.outputs.is-valid == 'true' + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + + - name: Setup Git + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20.x + cache: "npm" + + - name: Install Dependencies + run: npm ci + + - name: Update Package.json Version + run: | + VERSION="${{ needs.validate-release-version.outputs.version }}" + + # If this is a prerelease, remove any prerelease tags to get base version + if [ "${{ github.event.action }}" = "prereleased" ]; then + BASE_VERSION=$(echo "$VERSION" | sed 's/-.*$//') + echo "🔄 Prerelease detected: using base version $BASE_VERSION instead of $VERSION" + VERSION="$BASE_VERSION" + fi + + CURRENT_VERSION=$(node -p "require('./package.json').version") + + if [ "$VERSION" = "$CURRENT_VERSION" ]; then + echo "â„šī¸ Package.json already has version $VERSION, skipping update" + else + echo "đŸ“Ļ Updating package.json from $CURRENT_VERSION to $VERSION" + npm version $VERSION --no-git-tag-version + echo "✅ Successfully updated package version to $VERSION" + fi + + - name: Check for Changes + id: git-check + run: | + # Check for any modified or untracked files + if [ -n "$(git status --porcelain)" ]; then + echo "changes=true" >> $GITHUB_OUTPUT + echo "📝 Changes detected:" + git status --porcelain + else + echo "changes=false" >> $GITHUB_OUTPUT + echo "â„šī¸ No changes to commit" + fi + + - name: Commit and Push All Changes + if: steps.git-check.outputs.changes == 'true' + run: | + git add package.json package-lock.json + git commit -m "chore: version bump ${{ env.TAG_NAME }} + + - Update version to ${{ needs.validate-release-version.outputs.version }}" + git push origin HEAD:${{ env.TARGET_BRANCH }} + + - name: Update Tag Reference + if: steps.git-check.outputs.changes == 'true' + run: | + # Move the existing tag to point to the new commit (preserves GitHub release relationship) + git tag ${{ env.TAG_NAME }} -f + git push origin ${{ env.TAG_NAME }} -f + echo "✅ Updated tag ${{ env.TAG_NAME }} to point to release commit" + deploy: + name: Publish Extension + needs: [validate-release-version, update-version] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + ref: ${{ env.TARGET_BRANCH }} + token: ${{ secrets.GITHUB_TOKEN }} - uses: actions/setup-node@v4 with: node-version: 20 - - run: npm install + - run: npm ci - name: Publish to VS Code Marketplace uses: HaaLeo/publish-vscode-extension@v2