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