name: Release GitHub Action on: release: types: [published] workflow_dispatch: inputs: tag: description: "Tag to release (e.g., v1.0.0)" required: true type: string concurrency: release-${{ github.event.release.tag_name || github.event.inputs.tag || github.ref_name }} permissions: contents: write jobs: release-action: name: Build and Release Action runs-on: ubuntu-latest steps: # Runs before checkout: a manual dispatch can name any tag-shaped ref, # and this workflow force-updates tags and overwrites release assets. # Require the tag to already exist as a published (non-draft) release. - name: Verify tag is a published release if: github.event_name == 'workflow_dispatch' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} INPUT_TAG: ${{ github.event.inputs.tag }} run: | if ! [[ "$INPUT_TAG" =~ ^v[0-9]+(\.[0-9]+)*$ ]]; then echo "Error: '$INPUT_TAG' is not a valid release tag." exit 1 fi if ! gh release view "$INPUT_TAG" \ --repo "$GITHUB_REPOSITORY" \ --json isDraft > /tmp/release.json 2>/dev/null; then echo "Error: '$INPUT_TAG' is not a published release of this repository." exit 1 fi if [ "$(jq -r '.isDraft' /tmp/release.json)" != "false" ]; then echo "Error: release '$INPUT_TAG' is a draft; refusing to publish action tags." exit 1 fi - name: Checkout uses: actions/checkout@v4 with: ref: ${{ github.event.inputs.tag || (github.event.release.tag_name || github.ref) }} fetch-depth: 0 - name: Setup Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 with: bun-version: "1.4.5" - name: Install dependencies run: bun install --frozen-lockfile # The action bundles benchmark, core and utils and loads compressors at # runtime, so build the whole workspace. The root script already orders # utils first and the action last. - name: Build action run: bun run build - name: Get version id: version env: INPUT_TAG: ${{ github.event.inputs.tag }} REF_NAME: ${{ github.ref_name }} run: | if [ -n "$INPUT_TAG" ]; then VERSION="$INPUT_TAG" else VERSION="$REF_NAME" fi # Validate that VERSION looks like a tag (starts with v followed by numbers) if ! [[ "$VERSION" =~ ^v[0-9]+(\.[0-9]+)*$ ]]; then echo "Error: Version '$VERSION' is not a valid release tag (must start with 'v' e.g. v1.0.0)" exit 1 fi echo "version=$VERSION" >> $GITHUB_OUTPUT # Extract major version using regex if [[ "$VERSION" =~ ^v([0-9]+) ]]; then MAJOR="v${BASH_REMATCH[1]}" echo "major=$MAJOR" >> $GITHUB_OUTPUT else echo "Error: Could not extract major version from '$VERSION'" exit 1 fi - name: Ensure we're on a branch run: | # If in detached HEAD, create a temporary branch if ! git symbolic-ref -q HEAD; then git checkout -b temp-release-branch fi - name: Commit built action dist id: commit run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" # Force add dist (ignored by .gitignore) git add -f packages/action/dist/ # Only commit if there are staged changes if git diff --cached --quiet; then echo "No changes to commit (dist files unchanged)" echo "skip_tag_update=true" >> $GITHUB_OUTPUT else git commit -m "chore: build action dist for ${{ steps.version.outputs.version }}" echo "skip_tag_update=false" >> $GITHUB_OUTPUT fi - name: Update release tag if: steps.commit.outputs.skip_tag_update != 'true' run: | # Update the release tag to point at HEAD (with dist files) and push VERSION="${{ steps.version.outputs.version }}" git tag -fa "$VERSION" -m "Release $VERSION with built dist" git push origin "refs/tags/$VERSION" --force - name: Update major version tag run: | # Force update the major version tag (e.g., v1) MAJOR="${{ steps.version.outputs.major }}" git tag -fa "$MAJOR" -m "Update $MAJOR tag to ${{ steps.version.outputs.version }}" if ! git push origin "refs/tags/$MAJOR" --force; then echo "Error: Failed to push major version tag $MAJOR" exit 1 fi - name: Create action release artifact run: | mkdir -p release-action/packages/action cp action.yml release-action/ cp -r packages/action/dist release-action/packages/action/ cp packages/action/README.md release-action/ cd release-action zip -r ../node-minify-action-${{ steps.version.outputs.version }}.zip . # Also runs for workflow_dispatch: publish.yml dispatches this workflow # because a GITHUB_TOKEN release never emits `release: published`. - name: Upload release artifact uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 # v2.6.2 with: tag_name: ${{ steps.version.outputs.version }} files: node-minify-action-${{ steps.version.outputs.version }}.zip # Re-runs and repeated dispatches target the same tag. This is the # action's default, set explicitly so the intent survives upgrades. overwrite_files: true