Files
prettier-plugin-openapi/.github/workflows/release.yml

141 lines
4.9 KiB
YAML

name: Release
on:
workflow_run:
workflows: ["CI"] # Must match the CI workflow name exactly
types: [completed]
permissions:
contents: write # Needed to create tags/releases
packages: write # If you also publish GitHub Packages
id-token: write # Optional (for OIDC to cloud registries)
jobs:
publish-and-release:
# Only proceed if:
# 1) CI concluded successfully
# 2) The run was triggered by a push (not a PR)
# 3) The branch is main
# 4) The run belongs to this repository (not a fork)
if: >
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'main' &&
github.event.workflow_run.head_repository.full_name == github.repository
runs-on: ubuntu-latest
steps:
# Check out the exact commit that passed CI
- name: Checkout the successful commit
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Use Node 20
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
registry-url: https://registry.npmjs.org/
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install
run: bun install --frozen-lockfile
# Optional: Re-run build to ensure publish artifacts exist
- name: Build
run: bun run build
# Read current version from package.json
- name: Read current version
id: current-version
shell: bash
run: |
ver=$(node -p "require('./package.json').version")
echo "version=$ver" >> "$GITHUB_OUTPUT"
# Check if version was already bumped in this commit
- name: Check if version was bumped
id: version-check
shell: bash
run: |
# Get the previous commit's version
PREV_VERSION=$(git show HEAD~1:package.json 2>/dev/null | node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0")
CURRENT_VERSION="${{ steps.current-version.outputs.version }}"
echo "Previous version: $PREV_VERSION"
echo "Current version: $CURRENT_VERSION"
if [ "$PREV_VERSION" != "$CURRENT_VERSION" ]; then
echo "Version was already bumped from $PREV_VERSION to $CURRENT_VERSION"
echo "bumped=false" >> "$GITHUB_OUTPUT"
echo "final_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
else
echo "No version bump detected, will auto-patch bump"
echo "bumped=true" >> "$GITHUB_OUTPUT"
fi
# Auto-patch bump version if no version change was made
- name: Auto-patch bump version
if: steps.version-check.outputs.bumped == 'true'
id: bump-version
shell: bash
run: |
npm version patch --no-git-tag-version
NEW_VERSION=$(node -p "require('./package.json').version")
echo "version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
echo "Auto-bumped version to $NEW_VERSION"
# Set final version
- name: Set final version
id: final-version
shell: bash
run: |
if [ "${{ steps.version-check.outputs.bumped }}" = "true" ]; then
echo "version=${{ steps.bump-version.outputs.version }}" >> "$GITHUB_OUTPUT"
else
echo "version=${{ steps.current-version.outputs.version }}" >> "$GITHUB_OUTPUT"
fi
# Commit version bump if auto-bumped
- name: Commit auto-bumped version
if: steps.version-check.outputs.bumped == 'true'
shell: bash
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add package.json
git commit -m "chore: auto-bump version to ${{ steps.bump-version.outputs.version }}"
git push origin main
# Create a git tag like v1.2.3 if it doesn't already exist
- name: Create tag if missing
shell: bash
run: |
TAG="v${{ steps.final-version.outputs.version }}"
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag $TAG already exists."
else
git tag "$TAG" ${{ github.event.workflow_run.head_sha }}
git push origin "$TAG"
fi
# Publish to npm (requires NPM_TOKEN in repo secrets)
- name: Publish to npm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm publish --access public
# Create a GitHub Release for the tag
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.final-version.outputs.version }}
name: v${{ steps.final-version.outputs.version }}
generate_release_notes: true