build: add lerna projects support #1
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: Frontend CI Workflow | ||
run-name: ${{ github.event_name == 'delete' && format('cleanup NPM dist tags') || format('') }} | ||
on: | ||
workflow_call: | ||
jobs: | ||
on-push: | ||
if: github.event_name == 'push' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
registry-url: 'https://npm.pkg.github.com/' | ||
- name: Install dependencies | ||
run: npm ci | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Check is Lerna project | ||
run: | | ||
if [ -f lerna.json ]; then | ||
echo "Project is a Lerna project." | ||
echo "IS_LERNA=true" >> $GITHUB_ENV | ||
else | ||
echo "Project is NOT a Lerna project." | ||
echo "IS_LERNA=false" >> $GITHUB_ENV | ||
fi | ||
- name: Bump version and commit (prerelease only) | ||
id: bump_version | ||
if: startsWith(github.ref, 'refs/heads/develop') || startsWith(github.ref, 'refs/heads/release') || startsWith(github.ref, 'refs/heads/feature/') | ||
run: | | ||
git config --global user.name "github-actions" | ||
git config --global user.email "[email protected]" | ||
CURRENT_BRANCH=$(echo $GITHUB_REF | sed 's/refs\/heads\///') | ||
if [[ "$CURRENT_BRANCH" == "develop" || "$CURRENT_BRANCH" == "release" || "$CURRENT_BRANCH" == feature/* ]]; then | ||
if [ "$IS_LERNA" == "true" ]; then | ||
lerna version prerelease -m "chore: bump prerelease version to %s" --no-private --no-push --no-git-tag-version --yes | ||
else | ||
npm version prerelease -m "chore: bump prerelease version to %s" | ||
fi | ||
echo "bump_version=true" >> $GITHUB_ENV | ||
else | ||
echo "bump_version=false" >> $GITHUB_ENV | ||
fi | ||
# This step is to update internal dependencies specified via tag version to their latest available versions | ||
- name: Update dependencies | ||
run: npm ls --json | jq -r '.dependencies | keys[]' | grep "@netcracker" | xargs --no-run-if-empty npm update | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Build the package | ||
run: npm run build --if-present | ||
- name: Run tests | ||
run: npm test --if-present | ||
- name: Publish to GitHub NPM Registry | ||
run: | | ||
CURRENT_BRANCH=$(echo $GITHUB_REF | sed 's/refs\/heads\///') | ||
if [ "$CURRENT_BRANCH" == "main" ]; then | ||
TAG_NAME="latest" | ||
elif [ "$CURRENT_BRANCH" == "release" ]; then | ||
TAG_NAME="next" | ||
elif [ "$CURRENT_BRANCH" == "develop" ]; then | ||
TAG_NAME="dev" | ||
elif [[ "$CURRENT_BRANCH" == feature/* ]]; then | ||
TAG_NAME="${CURRENT_BRANCH//\//-}" | ||
fi | ||
if [ -n "$TAG_NAME" ]; then | ||
if [ "$IS_LERNA" == "true" ]; then | ||
lerna publish from-package --yes --no-push --no-git-reset --no-git-tag-version --dist-tag "$TAG_NAME" | ||
else | ||
npm publish --tag "$TAG_NAME" | ||
fi | ||
fi | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Push changes back | ||
if: env.bump_version == 'true' | ||
run: | | ||
git push origin HEAD | ||
on-delete: | ||
if: github.event_name == 'delete' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
registry-url: 'https://npm.pkg.github.com/' | ||
- name: Get package name from package.json | ||
run: | | ||
if [ -f package.json ]; then | ||
PACKAGE_NAME=$(jq -r '.name' package.json) | ||
echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_ENV | ||
else | ||
echo "No package.json found." && exit 1 | ||
fi | ||
- name: Delete associated NPM dist tag | ||
run: | | ||
DELETED_BRANCH=${{ github.event.ref }} | ||
if [ "$DELETED_BRANCH" == "release" ]; then | ||
TAG_NAME="next" | ||
elif [ "$DELETED_BRANCH" == feature/* ]; then | ||
TAG_NAME="${CURRENT_BRANCH//\//-}" | ||
fi | ||
if [ -n "$TAG_NAME" ]; then | ||
npm dist-tag rm "$PACKAGE_NAME" "$TAG_NAME" | ||
fi | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |