Skip to content

Publish npm

Publish npm #47

Workflow file for this run

name: Publish npm
on:
workflow_call:
inputs:
NODE_VERSION:
required: true
type: string
PNPM_VERSION:
required: true
type: string
PUBLISH_APPS:
required: true
type: boolean
PUBLISH_PACKAGES:
required: true
type: boolean
PUBLISH_PLUGINS:
required: true
type: boolean
workflow_dispatch:
inputs:
NODE_VERSION:
description: Nodejs version used
required: true
type: string
default: "20.11.0"
PNPM_VERSION:
description: Pnpm version used
required: true
type: string
default: "8"
PUBLISH_APPS:
description: Publish apps modules
required: true
type: boolean
default: false
PUBLISH_PACKAGES:
description: Publish packages modules
required: true
type: boolean
default: false
PUBLISH_PLUGINS:
description: Publish plugins modules
required: true
type: boolean
default: true
jobs:
matrix:
name: Generate matrix
if: ${{ inputs.PUBLISH_APPS || inputs.PUBLISH_PACKAGES || inputs.PUBLISH_PLUGINS }}
runs-on: ubuntu-latest
outputs:
publish-matrix: ${{ steps.packages-matrix.outputs.PUBLISH_MATRIX }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
- name: Generate matrix
id: packages-matrix
run: |
PUBLISH_MATRIX="[]"
APPS_MATRIX="$(jq -c '.apps' < ./ci/matrix-npm.json)"
PACKAGES_MATRIX="$(jq -c '.packages' < ./ci/matrix-npm.json)"
PLUGINS_MATRIX="$(jq -c '.plugins' < ./ci/matrix-npm.json)"
if [ "${{ inputs.PUBLISH_APPS }}" = "true" ]; then
PUBLISH_MATRIX="$(jq -n -c --argjson acc "$PUBLISH_MATRIX" --argjson cur "$APPS_MATRIX" '$acc + $cur')"
fi
if [ "${{ inputs.PUBLISH_PACKAGES }}" = "true" ]; then
PUBLISH_MATRIX="$(jq -n -c --argjson acc "$PUBLISH_MATRIX" --argjson cur "$PACKAGES_MATRIX" '$acc + $cur')"
fi
if [ "${{ inputs.PUBLISH_PLUGINS }}" = "true" ]; then
PUBLISH_MATRIX="$(jq -n -c --argjson acc "$PUBLISH_MATRIX" --argjson cur "$PLUGINS_MATRIX" '$acc + $cur')"
fi
echo "PUBLISH_MATRIX=$PUBLISH_MATRIX" >> $GITHUB_OUTPUT
publish-npm:
name: Pubish on npm
if: ${{ inputs.PUBLISH_APPS || inputs.PUBLISH_PACKAGES || inputs.PUBLISH_PLUGINS }}
runs-on: ubuntu-latest
needs:
- matrix
strategy:
matrix:
modules: ${{ fromJSON(needs.matrix.outputs.publish-matrix) }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.ref }}
- name: Install pnpm
uses: pnpm/action-setup@v3
id: pnpm-install
with:
version: "${{ inputs.PNPM_VERSION }}"
run_install: false
- name: Check for new package version
id: check-version
run: |
PACKAGE_VERSION=$(jq -cr '.version' < ${{ matrix.modules.path }}/package.json)
if [ "$(curl -s -o /dev/null -I -w %{http_code} https://registry.npmjs.org/${{ matrix.modules.name }})" = 404 ]; then
REMOTE=false
else
REMOTE=$(pnpm view ${{ matrix.modules.name }} --json | jq --arg v "$PACKAGE_VERSION" 'any(.versions[] == $v; .)')
fi
if [ "$REMOTE" = "true" ]; then
echo "Package ${{ matrix.modules.name }} - version $PACKAGE_VERSION already exists"
else
echo "Package ${{ matrix.modules.name }} - version $PACKAGE_VERSION does not exist"
fi
echo "REMOTE=$REMOTE" >> $GITHUB_OUTPUT
- name: Setup Node.js
uses: actions/setup-node@v4
if: ${{ steps.check-version.outputs.REMOTE == 'false' }}
with:
node-version: "${{ inputs.NODE_VERSION }}"
registry-url: 'https://registry.npmjs.org'
always-auth: true
- name: Get pnpm store directory
if: ${{ steps.check-version.outputs.REMOTE == 'false' }}
id: pnpm-store
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- name: Cache node files
if: ${{ steps.check-version.outputs.REMOTE == 'false' }}
uses: actions/cache@v4
with:
path: |
${{ steps.pnpm-store.outputs.STORE_PATH }}
key: node-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
node-${{ runner.os }}-${{ runner.arch }}-
- name: Install dependencies
if: ${{ steps.check-version.outputs.REMOTE == 'false' }}
run: pnpm install --frozen-lockfile
- name: Publish packages
if: ${{ steps.check-version.outputs.REMOTE == 'false' }}
run: |
# This line use turbo to handle build dependency graph
pnpm run build --filter ${{ matrix.modules.name }}
# This line use pnpm to publish without turbo
pnpm --filter ${{ matrix.modules.name }} publish --no-git-checks --report-summary
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}