Skip to content

Deploy to npm

Deploy to npm #22

Workflow file for this run

name: Deploy to npm
on:
workflow_dispatch:
concurrency:
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true
permissions:
contents: write
pages: write
id-token: write
jobs:
open-release:
name: Open release
runs-on: ubuntu-latest
outputs:
INITIAL_MAIN_POSITION: ${{ steps.create-tag.outputs.INITIAL_MAIN_POSITION }}
TAG: ${{ steps.create-tag.outputs.TAG }}
VERSION: ${{ steps.create-tag.outputs.VERSION }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Configure Git User
run: |
git config user.email "[email protected]"
git config user.name "Gili Tzabari"
- name: Parse the release version from package.json
uses: martinbeentjes/[email protected]
id: parse-version
# Setting a GitHub Action output parameter:
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
# Extracting the release version number: https://stackoverflow.com/a/16623897/14731
- name: Create tag
id: create-tag
run: |
echo "INITIAL_MAIN_POSITION=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
VERSION=${{ steps.parse-version.outputs.current-version }}
TAG=release-${VERSION}
echo "TAG=${TAG}" >> "$GITHUB_OUTPUT"
echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT"
git tag ${TAG}
git push origin tag ${TAG}
deploy:
name: Deploy
needs: open-release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.open-release.outputs.TAG }}
fetch-depth: 0
- name: Configure Git User
run: |
git config user.email "[email protected]"
git config user.name "Gili Tzabari"
- name: Install node
uses: actions/setup-node@v4
with:
node-version: latest
registry-url: "https://registry.npmjs.org"
- name: Install pnpm
run: |
corepack enable
corepack prepare pnpm@latest --activate
# pnpm dependencies cannot be cached until pnpm is installed
# WORKAROUND: https://github.com/actions/setup-node/issues/531
- name: Extract cached dependencies
uses: actions/setup-node@v4
with:
cache: pnpm
- name: Test build
run: |
pnpm install
pnpm run build
- name: Deploy to npm
run: |
cd target/publish
cp ../../pnpm-lock.yaml .
pnpm publish --provenance --access=public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Generate documentation
run: |
VERSION=${{ needs.open-release.outputs.VERSION }}
rm -rf "docs/api/${VERSION}"
mkdir --parents "docs/api/${VERSION}"
mv target/apidocs/* "docs/api/${VERSION}"
- name: Commit documentation
run: |
git checkout ${{ github.ref_name }} -f
git add "docs/api/${{ needs.open-release.outputs.VERSION }}"
git commit -m "Committing documentation for version ${{ needs.open-release.outputs.VERSION }}"
git push
document:
name: Document
needs: deploy
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: "docs/api"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Publish documentation
run: |
git checkout ${{ github.ref_name }} -f
echo "INITIAL_GH_PAGES_POSITION=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
mkdir --parents "${{ needs.open-release.outputs.VERSION }}/docs"
mv target/apidocs "${{ needs.open-release.outputs.VERSION }}/docs/api"
git add "${{ needs.open-release.outputs.VERSION }}/docs/api"
git commit -m "Released version ${{ needs.open-release.outputs.VERSION }}"
git push
# Cleanup on failure: https://stackoverflow.com/a/74562058/14731
on-failure:
name: On failure
needs: [ open-release, deploy, document ]
runs-on: ubuntu-latest
if: ${{ failure() || cancelled() }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
fetch-depth: 0
- name: Install node
uses: actions/setup-node@v4
with:
node-version: latest
registry-url: "https://registry.npmjs.org"
- name: Install pnpm
run: |
corepack enable
corepack prepare pnpm@latest --activate
# pnpm dependencies cannot be cached until pnpm is installed
# WORKAROUND: https://github.com/actions/setup-node/issues/531
- name: Extract cached dependencies
uses: actions/setup-node@v4
with:
cache: pnpm
- name: Restore the main ref to its original position
if: needs.open-release.outputs.INITIAL_MAIN_POSITION != ''
run: |
CURRENT_REF_POSITION=$(git rev-parse HEAD)
if [ "${CURRENT_REF_POSITION}" != "${{ needs.open-release.outputs.INITIAL_MAIN_POSITION }}" ]; then
git reset --hard ${{ needs.open-release.outputs.INITIAL_MAIN_POSITION }}
if [ "${{ github.ref_type }}" == "tag" ]; then
git ${{ github.ref_type }} -f ${{ github.ref_name }}
fi
git push -f origin ${{ github.ref_name }}
fi
- name: Delete tag
if: needs.open-release.outputs.TAG != ''
run: |
git push --delete origin ${{ needs.open-release.outputs.TAG }}