Updated node version in workflows. #62
This file contains hidden or 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
| permissions: | |
| contents: write | |
| name: Build and Deploy PowerMonitor API | |
| on: | |
| push: | |
| branches: | |
| - master | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| src_changed: ${{ steps.src-check.outputs.src_changed }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check if src folder changed | |
| id: src-check | |
| run: | | |
| BEFORE=${{ github.event.before }} | |
| if [ -z "$BEFORE" ] || [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then | |
| BEFORE=$(git rev-parse HEAD^) | |
| fi | |
| if git diff --name-only $BEFORE ${{ github.sha }} | grep '^src/'; then | |
| echo "src_changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "src_changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "22.14.0" | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Lint | |
| run: npm run lint | |
| - name: Run tests | |
| run: npm run test | |
| - name: Check version bump eligibility | |
| id: version-check | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| # Fetch tags to check existing releases | |
| git fetch --tags || true | |
| # Detect trailing .0 or .0.0 | |
| if echo "$VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.0$|^[0-9]+\.0\.0$'; then | |
| # If a tag for this version already exists, we bump; otherwise skip | |
| if git tag --list "v$VERSION" | grep -q "v$VERSION"; then | |
| echo "bump=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "bump=false" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| # Non zero-ending versions always bump | |
| echo "bump=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Bump version | |
| if: steps.version-check.outputs.bump == 'true' | |
| run: npm version patch --no-git-tag-version | |
| - name: Replace build | |
| run: node ./replace.build.js | |
| - name: Check versions | |
| run: npm run test tests/shared/version-consistency.spec.ts | |
| - name: Build | |
| run: tsc -p tsconfig.build.prod.json | |
| - name: Configure git | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| - name: Commit and push version bump, lockfile, and environments | |
| if: steps.src-check.outputs.src_changed == 'true' | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| git fetch --prune --unshallow || true | |
| git checkout master | |
| git add package.json package-lock.json src/config/environments.ts | |
| git commit -m "[skip ci] $VERSION" || echo "No changes to commit" | |
| git tag v$VERSION | |
| git push origin master | |
| git push origin v$VERSION | |
| - name: Archive dist and package files | |
| run: | | |
| zip -r powermonitor-api.zip dist package.json package-lock.json | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: powermonitor-api-artifact | |
| path: powermonitor-api.zip | |
| deploy: | |
| runs-on: [self-hosted, linux, arm] | |
| needs: build | |
| if: needs.build.outputs.src_changed == 'true' | |
| steps: | |
| - name: Download artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: powermonitor-api-artifact | |
| - name: Stop PM2 app | |
| run: | | |
| sudo -u pi pm2 stop PowerMonitor || true | |
| - name: Remove old dist folder | |
| run: | | |
| sudo -u pi rm -rf /home/pi/PowerMonitor.API/dist | |
| - name: Unpack archive to target | |
| run: | | |
| sudo -u pi unzip -o powermonitor-api.zip -d /home/pi/PowerMonitor.API | |
| - name: Install dependencies on target | |
| run: | | |
| cd /home/pi/PowerMonitor.API && sudo -u pi npm ci --omit=dev | |
| - name: Start PM2 app | |
| run: | | |
| sudo -u pi pm2 start /home/pi/PowerMonitor.API/ecosystem.config.js --only PowerMonitor |