feat: update GitHub Actions workflow to version and publish packages … #10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish to NPM | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
workflow_dispatch: | |
inputs: | |
release_type: | |
description: 'Release type (stable/alpha)' | |
required: true | |
default: 'stable' | |
type: choice | |
options: | |
- stable | |
- alpha | |
permissions: | |
contents: write | |
jobs: | |
version-and-publish: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: '20.x' | |
registry-url: 'https://registry.npmjs.org/' | |
- name: Install pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
- name: Set Release Type | |
id: release_type | |
run: | | |
if [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then | |
echo "type=alpha" >> $GITHUB_OUTPUT | |
elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then | |
echo "type=stable" >> $GITHUB_OUTPUT | |
else | |
echo "type=${{ inputs.release_type }}" >> $GITHUB_OUTPUT | |
fi | |
- name: Install Dependencies | |
run: | | |
if [[ "${{ steps.release_type.outputs.type }}" == "alpha" ]]; then | |
pnpm run preinstall:develop && pnpm install | |
else | |
pnpm run preinstall:stable && pnpm install | |
fi | |
- name: Build | |
run: | | |
if [[ "${{ steps.release_type.outputs.type }}" == "alpha" ]]; then | |
pnpm run build:develop | |
else | |
pnpm run build:stable | |
fi | |
- name: Get Next Version | |
id: semantic | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
pnpm add -D semantic-release | |
OUTPUT=$(unset GITHUB_ACTIONS && npx semantic-release --dry-run --no-ci) | |
VERSION=$(echo "$OUTPUT" | grep -o "The next release version is [0-9]*\.[0-9]*\.[0-9]*" | awk '{print $6}') | |
if [[ "${{ steps.release_type.outputs.type }}" == "alpha" ]]; then | |
VERSION="${VERSION}-alpha.$(date +%s)" | |
fi | |
echo "version=${VERSION}" >> $GITHUB_OUTPUT | |
- name: Update Version | |
run: | | |
npm version ${{ steps.semantic.outputs.version }} --no-git-tag-version | |
- name: Publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
run: | | |
if [[ "${{ steps.release_type.outputs.type }}" == "alpha" ]]; then | |
pnpm publish --access=public --tag alpha --no-git-checks | |
else | |
pnpm publish --access=public --no-git-checks | |
fi | |
- name: Create Git Tag | |
if: success() | |
run: | | |
git config user.name 'github-actions[bot]' | |
git config user.email 'github-actions[bot]@users.noreply.github.com' | |
git tag -a "v${{ steps.semantic.outputs.version }}" -m "Release v${{ steps.semantic.outputs.version }}" | |
git push origin "v${{ steps.semantic.outputs.version }}" |