Przeglądaj źródła

chore(ci): Update NPM publish workflow to run nightly and skip if no new commits in 24 hours

David Höck 6 miesięcy temu
rodzic
commit
513fc4bbc5
1 zmienionych plików z 31 dodań i 12 usunięć
  1. 31 12
      .github/workflows/publish_master_to_npm.yml

+ 31 - 12
.github/workflows/publish_master_to_npm.yml

@@ -2,13 +2,9 @@ name: Publish Master Branch to NPM
 
 on:
     workflow_dispatch:
-    push:
-        branches:
-            - master
-        paths:
-            - 'packages/**'
-            - 'package.json'
-            - 'package-lock.json'
+    # Nightly run at 02:00 UTC
+    schedule:
+        - cron: '0 2 * * *'
 
 jobs:
     publish:
@@ -26,44 +22,67 @@ jobs:
               with:
                   fetch-depth: 0
 
+            # Check if branch received commits in the last 24 h; skip remainder if none
+            - name: Check for new commits
+              id: commit_check
+              run: |
+                  # Count commits since 24 h ago on this branch
+                  COMMITS=$(git rev-list --count --since="24 hours" HEAD)
+                  echo "Commits found: $COMMITS"
+                  if [ "$COMMITS" -eq 0 ]; then
+                      echo "should_publish=false" >> $GITHUB_OUTPUT
+                  else
+                      echo "should_publish=true" >> $GITHUB_OUTPUT
+                  fi
+
             - name: Setup Node.js
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               uses: actions/setup-node@v4
               with:
                   node-version: '22.x'
                   registry-url: 'https://registry.npmjs.org'
 
             - name: Install dependencies
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               run: npm install --no-save
 
             - name: Get current date
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               id: date
               run: echo "date=$(date +'%Y%m%d%H%M')" >> $GITHUB_OUTPUT
 
             - name: Configure Git
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               run: |
                   git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
                   git config --global user.name "github-actions[bot]"
 
             - name: Version and build packages
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               run: |
-                  # Get the current version from lerna.json
+                  # Get current version from lerna.json
                   CURRENT_VERSION=$(node -p "require('./lerna.json').version")
-                  # Split version into parts
                   IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
-                  # Increment patch version
+                  # Increment patch version for pre-release
                   PATCH=$((VERSION_PARTS[2] + 1))
-                  # Create new version with date
+                  # Compose new pre-release version incl. timestamp
                   NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${PATCH}-master-${{ steps.date.outputs.date }}"
-                  # Update version using lerna directly
                   npx lerna version $NEW_VERSION --no-git-tag-version --yes --no-push --force-publish
 
             - name: Commit changes
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               run: |
                   git add .
                   git commit -m "chore: Bump version for pre-release to $NEW_VERSION"
 
             - name: Publish to NPM
+              if: ${{ steps.commit_check.outputs.should_publish == 'true' }}
               run: npx lerna publish from-package --yes --dist-tag master --no-git-reset
               env:
                   NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
                   NPM_CONFIG_PROVENANCE: true
+
+            # Informative noop when skipping
+            - name: Skip publish
+              if: ${{ steps.commit_check.outputs.should_publish == 'false' }}
+              run: echo "No new commits in last 24 hours – skipping publish."