Skip to content

Publish

Publish #12

Workflow file for this run

name: Publish
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to publish from'
required: true
default: 'main'
type: string
version_type:
description: 'Version bump type'
required: true
type: choice
options:
- patch
- minor
- major
default: 'patch'
jobs:
test:
uses: ./.github/workflows/test.yml
publish:
needs: test
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.branch }}
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 'latest'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
- name: Install dependencies
run: npm i
- name: Configure Git
run: |
git config --local user.email "[email protected]"
git config --local user.name "Mikl Wolfe"
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
- name: Bump version
id: bump_version
run: |
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "Current version: $CURRENT_VERSION"
npm version ${{ github.event.inputs.version_type }} --no-git-tag-version
NEW_VERSION=$(node -p "require('./package.json').version")
echo "New version: $NEW_VERSION"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Build
run: npm run build
- name: Commit version changes
if: success()
run: |
git add package.json package-lock.json
git commit -m "v${{ steps.bump_version.outputs.new_version }}"
- name: Push Git Changes
if: success()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push origin HEAD:${{ github.event.inputs.branch }}
git push --tags
- name: Create GitHub Release
if: success()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create v${{ steps.bump_version.outputs.new_version }} \
--title "v${{ steps.bump_version.outputs.new_version }}" \
--generate-notes
- name: Publish to NPM
id: npm_publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm publish --access public
- name: Notify on success
if: success()
run: |
echo "🎉 Successfully published version ${{ steps.bump_version.outputs.new_version }}"
echo "- NPM package: https://www.npmjs.com/package/depsweep"
echo "- GitHub release: https://github.com/${{ github.repository }}/releases/tag/v${{ steps.bump_version.outputs.new_version }}"
- name: Rollback on failure
if: failure()
run: |
echo "::error::Deployment failed, attempting rollback..."
# Remove local tag
git tag -d v${{ steps.bump_version.outputs.new_version }} || true
# Remove remote tag if it was pushed
git push --delete origin v${{ steps.bump_version.outputs.new_version }} || true
# Reset local changes
git reset --hard HEAD^
git push origin HEAD:${{ github.event.inputs.branch }} || true
echo "::error::Rollback complete. Please check the repository state."