Deploy to npm #14
Workflow file for this run
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: Deploy to npm | |
on: | |
workflow_dispatch: | |
concurrency: | |
group: "${{ github.workflow }}-${{ github.ref }}" | |
cancel-in-progress: true | |
permissions: | |
contents: write | |
id-token: write | |
jobs: | |
open-release: | |
runs-on: ubuntu-latest | |
outputs: | |
INITIAL_MASTER_POSITION: ${{ steps.create-tag.outputs.INITIAL_MASTER_POSITION }} | |
INITIAL_GH_PAGES_POSITION: ${{ steps.create-tag.outputs.INITIAL_GH_PAGES_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_MASTER_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: 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 . | |
git status | |
pnpm publish --provenance --access=public --no-git-checks | |
mkdir --parents "${{ needs.open-release.outputs.VERSION }}/docs" | |
mv target/apidocs "${{ needs.open-release.outputs.VERSION }}/docs/api" | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
- name: Commit changes | |
run: | | |
git checkout gh-pages -f | |
echo "INITIAL_GH_PAGES_POSITION=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" | |
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: | |
needs: [ open-release, deploy ] | |
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 master ref to its original position | |
if: needs.open-release.outputs.INITIAL_MASTER_POSITION != '' | |
run: | | |
CURRENT_REF_POSITION=$(git rev-parse HEAD) | |
if [ "${CURRENT_REF_POSITION}" != "${{ needs.open-release.outputs.INITIAL_MASTER_POSITION }}" ]; then | |
git reset --hard ${{ needs.open-release.outputs.INITIAL_MASTER_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 }} | |
- name: Restore the gh-pages ref to its original position | |
if: needs.open-release.outputs.INITIAL_GH_PAGES_POSITION != '' | |
run: | | |
CURRENT_REF_POSITION=$(git rev-parse HEAD) | |
if [ "${CURRENT_REF_POSITION}" != "${{ needs.open-release.outputs.INITIAL_GH_PAGES_POSITION }}" ]; then | |
git reset --hard ${{ needs.open-release.outputs.INITIAL_GH_PAGES_POSITION }} | |
if [ "${{ github.ref_type }}" == "tag" ]; then | |
git ${{ github.ref_type }} -f ${{ github.ref_name }} | |
fi | |
git push -f origin ${{ github.ref_name }} | |
fi |