|
| 1 | +# This workflow automatically creates a git tag when a version change is detected in package.json. |
| 2 | +# It runs on pushes to the main/master branch. |
| 3 | +name: Auto Tag Release |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - master |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: auto-tag-${{ github.ref }} |
| 13 | + cancel-in-progress: false |
| 14 | + |
| 15 | +jobs: |
| 16 | + tag-version: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + outputs: |
| 21 | + tag: ${{ steps.version.outputs.tag }} |
| 22 | + created: ${{ steps.tag.outputs.created }} |
| 23 | + env: |
| 24 | + EXTENSION_PATH: packages/vscode-tailwindcss |
| 25 | + PUBLISHER_NAME: TimsExperiments |
| 26 | + OPEN_VSX_TOKEN: ${{ secrets.OPEN_VSX_TOKEN }} |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + # Reads package.json, calculates the expected tag format (vX.Y.Z), and outputs it. |
| 33 | + # We need to know the current version in package.json to determine if a tag is missing. |
| 34 | + - name: Get Version and Tag |
| 35 | + id: version |
| 36 | + run: | |
| 37 | + cd ${{ env.EXTENSION_PATH }} || exit 1 |
| 38 | + VERSION=$(jq -r .version package.json) |
| 39 | +
|
| 40 | + if [ "${{ env.EXTENSION_PATH }}" == "." ] || [ "${{ env.EXTENSION_PATH }}" == "./" ]; then |
| 41 | + TAG="v$VERSION" |
| 42 | + else |
| 43 | + # Clean path for tag name (remove leading ./ and trailing /) |
| 44 | + CLEAN_PATH=$(echo "${{ env.EXTENSION_PATH }}" | sed 's/^\.\///' | sed 's/\/$//') |
| 45 | + TAG="$CLEAN_PATH/v$VERSION" |
| 46 | + fi |
| 47 | +
|
| 48 | + echo "Detected version: $VERSION" |
| 49 | + echo "Calculated tag: $TAG" |
| 50 | + echo "tag=$TAG" >> $GITHUB_OUTPUT |
| 51 | +
|
| 52 | + # Checks if the calculated tag exists. If not, creates and pushes it. |
| 53 | + # This ensures we only create tags that don't exist yet, avoiding errors and duplicate releases. |
| 54 | + - name: Check and Push Tag |
| 55 | + id: tag |
| 56 | + env: |
| 57 | + TAG: ${{ steps.version.outputs.tag }} |
| 58 | + run: | |
| 59 | + if git rev-parse "$TAG" >/dev/null 2>&1; then |
| 60 | + echo "Tag $TAG already exists. Skipping." |
| 61 | + echo "created=false" >> $GITHUB_OUTPUT |
| 62 | + else |
| 63 | + echo "Tag $TAG does not exist. Creating..." |
| 64 | + git config user.name "GitHub Action" |
| 65 | + git config user.email "[email protected]" |
| 66 | + git tag -a "$TAG" -m "Release $TAG" |
| 67 | + git push origin "$TAG" |
| 68 | + echo "created=true" >> $GITHUB_OUTPUT |
| 69 | + fi |
| 70 | +
|
| 71 | + release: |
| 72 | + needs: tag-version |
| 73 | + if: needs.tag-version.outputs.created == 'true' |
| 74 | + runs-on: ubuntu-latest |
| 75 | + env: |
| 76 | + EXTENSION_PATH: packages/vscode-tailwindcss |
| 77 | + PUBLISHER_NAME: TimsExperiments |
| 78 | + OPEN_VSX_TOKEN: ${{ secrets.OPEN_VSX_TOKEN }} |
| 79 | + steps: |
| 80 | + - uses: actions/checkout@v4 |
| 81 | + with: |
| 82 | + ref: ${{ needs.tag-version.outputs.tag }} |
| 83 | + |
| 84 | + - name: Detect pnpm version |
| 85 | + id: detect-pnpm |
| 86 | + run: | |
| 87 | + if [ -f package.json ] && grep -q '"packageManager":' package.json; then |
| 88 | + echo "packageManager found in package.json" |
| 89 | + echo "version=" >> $GITHUB_OUTPUT |
| 90 | + else |
| 91 | + echo "packageManager not found, using default" |
| 92 | + echo "version=10" >> $GITHUB_OUTPUT |
| 93 | + fi |
| 94 | +
|
| 95 | + - uses: pnpm/action-setup@v4 |
| 96 | + with: |
| 97 | + version: ${{ steps.detect-pnpm.outputs.version }} |
| 98 | + |
| 99 | + - name: Setup Node |
| 100 | + uses: actions/setup-node@v4 |
| 101 | + with: |
| 102 | + node-version: lts/* |
| 103 | + cache: "pnpm" |
| 104 | + |
| 105 | + - name: Install Dependencies |
| 106 | + run: pnpm install --frozen-lockfile |
| 107 | + |
| 108 | + - name: Build Everything |
| 109 | + run: pnpm -r run build |
| 110 | + |
| 111 | + # Updates the 'publisher' field in package.json to match the environment variable. |
| 112 | + # The upstream package.json has the original publisher. We need to publish under YOUR publisher ID. |
| 113 | + - name: Patch to ${{ env.PUBLISHER_NAME }} |
| 114 | + run: | |
| 115 | + cd ${{ env.EXTENSION_PATH }} |
| 116 | +
|
| 117 | + jq '.publisher = "${{ env.PUBLISHER_NAME }}"' package.json > package.json.tmp && mv package.json.tmp package.json |
| 118 | +
|
| 119 | + echo "Publisher verified as:" |
| 120 | + grep '"publisher":' package.json |
| 121 | +
|
| 122 | + # Runs 'vsce package' to create the file and 'ovsx publish' to upload it. |
| 123 | + # This creates the .vsix artifact and uploads it to the OpenVSX registry. |
| 124 | + - name: Build & Publish |
| 125 | + env: |
| 126 | + OVSX_PAT: ${{ env.OPEN_VSX_TOKEN }} |
| 127 | + run: | |
| 128 | + cd ${{ env.EXTENSION_PATH }} |
| 129 | +
|
| 130 | + pnpm dlx vsce package |
| 131 | +
|
| 132 | + pnpm dlx ovsx publish -p $OVSX_PAT |
0 commit comments