diff --git a/.github/workflows/auto-release.yml b/.github/workflows/auto-release.yml index a9ced9a..b753bf7 100644 --- a/.github/workflows/auto-release.yml +++ b/.github/workflows/auto-release.yml @@ -11,9 +11,12 @@ jobs: release: name: Create release for new version runs-on: ubuntu-latest + outputs: + released: ${{ steps.check.outputs.released }} + tag: ${{ steps.check.outputs.tag }} steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 with: fetch-depth: 0 @@ -45,3 +48,77 @@ jobs: gh release create "$TAG" --title "$TAG" --notes "$NOTES" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + publish: + name: Publish package + runs-on: ubuntu-latest + needs: release + if: needs.release.outputs.released == 'true' + concurrency: + group: npm-publish-${{ needs.release.outputs.tag }} + cancel-in-progress: false + permissions: + contents: read + id-token: write + steps: + - name: Checkout + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 + with: + ref: ${{ needs.release.outputs.tag }} + persist-credentials: false + + - name: Setup Node.js + uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 + with: + node-version: 24 + registry-url: https://registry.npmjs.org + package-manager-cache: false + + - name: Install dependencies + shell: bash + run: | + if [ -f package-lock.json ]; then + npm ci + elif node -e "const p=require('./package.json'); process.exit(p.dependencies || p.devDependencies ? 0 : 1)"; then + npm install + else + echo "No dependencies to install." + fi + + - name: Validate package + shell: bash + run: | + if node -e "process.exit(require('./package.json').scripts?.check ? 0 : 1)"; then + npm run check + elif node -e "process.exit(require('./package.json').scripts?.test ? 0 : 1)"; then + npm test + elif node -e "process.exit(require('./package.json').scripts?.typecheck ? 0 : 1)"; then + npm run typecheck + else + echo "No validation script configured." + fi + + - name: Skip already published version + id: published + shell: bash + run: | + name=$(node -p "require('./package.json').name") + version=$(node -p "require('./package.json').version") + set +e + output=$(npm view "${name}@${version}" version 2>&1) + status=$? + set -e + if [ "$status" -eq 0 ]; then + echo "${name}@${version} is already published." + echo "skip=true" >> "$GITHUB_OUTPUT" + elif printf '%s' "$output" | grep -Eq 'E404|404 Not Found'; then + echo "Publishing ${name}@${version}." + echo "skip=false" >> "$GITHUB_OUTPUT" + else + printf '%s\n' "$output" >&2 + exit "$status" + fi + + - name: Publish to npm + if: steps.published.outputs.skip != 'true' + run: npm publish --access public