Merge pull request #53 from absurdprofit/development #43
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: Publish to npm | |
on: | |
push: | |
branches: | |
- main | |
- next | |
- beta | |
tags: | |
- "v*" | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "20.x" | |
registry-url: "https://registry.npmjs.org" | |
- name: Install dependencies and build | |
run: npm ci && lerna run build --ignore @react-motion-router/example | |
- name: Get tag name and commit hash | |
id: git-info | |
run: | | |
if [[ "${GITHUB_REF##*/}" == "main" ]]; then | |
echo "TAG_NAME=latest" >> $GITHUB_ENV | |
else | |
echo "TAG_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV | |
fi | |
COMMIT_HASH=$(git rev-parse --short HEAD) | |
LAST_COMMIT_HASH=$(git rev-parse --short HEAD~1) | |
echo "COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV | |
echo "LAST_COMMIT_HASH=${LAST_COMMIT_HASH}" >> $GITHUB_ENV | |
- name: Conditional Version Update | |
id: version | |
if: startsWith(env.TAG_NAME, 'next') || startsWith(env.TAG_NAME, 'beta') | |
run: | | |
TAG_NAME=${{ env.TAG_NAME }} | |
COMMIT_HASH=${{ env.COMMIT_HASH }} | |
LAST_COMMIT_HASH=${{ env.LAST_COMMIT_HASH }} | |
ALL_PACKAGES_JSON=$(npx lerna list --json) | |
CHANGED_PACKAGES_JSON=$(npx lerna list --json --since ${LAST_COMMIT_HASH}) | |
CHANGED_PACKAGES_COUNT=$(echo $CHANGED_PACKAGES_JSON | jq '. | length') | |
if [[ $CHANGED_PACKAGES_COUNT -eq 0 ]]; then | |
echo "No packages changed. Exiting..." | |
echo "versioned=false" >> "$GITHUB_OUTPUT" | |
exit 0 | |
fi | |
echo $CHANGED_PACKAGES_JSON | jq -r '.[].location' | while read -r package; do | |
CURRENT_VERSION=$(jq -r '.version' $package/package.json) | |
NEW_VERSION="${CURRENT_VERSION}-${TAG_NAME}.sha-${COMMIT_HASH}" | |
jq ".version = \"${NEW_VERSION}\"" $package/package.json > $package/package.json.tmp && mv $package/package.json.tmp $package/package.json | |
done | |
echo $ALL_PACKAGES_JSON | jq -r '.[].location' | while read -r package; do | |
if ! echo $CHANGED_PACKAGES_JSON | jq -r '.[].location' | grep -q "$package"; then | |
jq ".private = true" $package/package.json > $package/package.json.tmp && mv $package/package.json.tmp $package/package.json | |
fi | |
done | |
echo "versioned=true" >> "$GITHUB_OUTPUT" | |
- name: Assume unchanged for all package.json files | |
run: | | |
git ls-files -m | grep 'package.json' | xargs -I {} git update-index --assume-unchanged {} | |
- name: Publish packages | |
if: ${{ steps.version.outputs.versioned == 'true' }} | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
run: | | |
TAG_NAME=${{ env.TAG_NAME }} | |
npx lerna publish from-package --yes --dist-tag $TAG_NAME |