diff --git a/.github/workflows/gpr-publish.yaml b/.github/workflows/gpr-publish.yaml deleted file mode 100644 index 0459893..0000000 --- a/.github/workflows/gpr-publish.yaml +++ /dev/null @@ -1,46 +0,0 @@ -name: Publish to GitHub Packages Registry - -# Version: 1.0.0 -# Modified: No -# Requirements: -# - The `name` in `package.json` should start with `@athombv/`. -# -# Ensure you've run `npm version major|minor|patch` on the `master` branch before merging to `production`. -# -# This GitHub Workflow: -# 1. [Optional] If `npm run build` exists. If so, it runs `npm ci` and `npm run build`. -# 2. Publishes the package to the GitHub Packages Registry. - -on: - push: - branches: - - production - -jobs: - publish: - name: Publish - runs-on: ubuntu-latest - steps: - - - name: Checkout git repository - uses: actions/checkout@v3 - - - name: Set up node 16 environment - uses: actions/setup-node@v3 - with: - node-version: 16 - registry-url: 'https://npm.pkg.github.com' - - - name: Build - run: | - if cat package.json | jq -e .scripts.build; then - echo "`npm run build` does exist. Building..." - npm ci - npm run build - else - echo "`npm run build` does not exist. Skipping build..." - fi - - name: Publish - run: npm publish - env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/publish-package-npm.yaml b/.github/workflows/publish-package-npm.yaml new file mode 100644 index 0000000..507f92a --- /dev/null +++ b/.github/workflows/publish-package-npm.yaml @@ -0,0 +1,67 @@ +name: Publish to NPM + +# Publishes the package to npmjs.org using OIDC (Trusted Publisher). +# +# Requirements: +# - The `@athombv/jsdoc-template` package must have a Trusted Publisher +# configured on npmjs.org pointing at this repository and workflow file. +# - Bump the version on `master` via the Version workflow before merging to +# `production`. +# +# Steps: +# 1. Verifies a `files` array is present in package.json. +# 2. Installs dependencies and runs `npm run build` if it exists. +# 3. On `workflow_dispatch`: runs `npm publish --dry-run`. +# 4. On push to `production`: publishes to npmjs.org. + +on: + workflow_dispatch: + push: + branches: + - production + +permissions: + id-token: write + contents: read + +jobs: + publish: + name: Publish + runs-on: ubuntu-latest + steps: + - name: Checkout git repository + uses: actions/checkout@v4 + + - name: Set up node + uses: actions/setup-node@v4 + with: + # Node 24 bundles npm >= 11.5.1, required for OIDC `npm publish`. + node-version: '24' + registry-url: 'https://registry.npmjs.org/' + + - name: Verify files field in package.json + run: | + if ! jq -e .files package.json > /dev/null; then + echo "Missing 'files' array in package.json." + exit 1 + fi + + - name: Install dependencies + run: npm ci --ignore-scripts --audit=false + + - name: Build + run: | + if jq --exit-status '.scripts | has("build")' package.json > /dev/null; then + echo "Running npm run build..." + npm run build + else + echo "No build script; skipping." + fi + + - name: Publish dry run + if: github.event_name == 'workflow_dispatch' + run: npm publish --dry-run --provenance + + - name: Publish + if: github.event_name == 'push' && github.ref == 'refs/heads/production' + run: npm publish --provenance diff --git a/.github/workflows/update-dependents.yaml b/.github/workflows/update-dependents.yaml index e1bc074..c618435 100644 --- a/.github/workflows/update-dependents.yaml +++ b/.github/workflows/update-dependents.yaml @@ -2,7 +2,7 @@ name: Update Dependent Repos on: workflow_run: - workflows: ["Publish to GitHub Packages Registry"] + workflows: ["Publish to NPM"] types: - completed @@ -161,9 +161,15 @@ jobs: else . end ' package.json > package.json.tmp && mv package.json.tmp package.json - # Configure npm to use GitHub Packages for @athombv scope - echo "@athombv:registry=https://npm.pkg.github.com" >> .npmrc - echo "//npm.pkg.github.com/:_authToken=${GH_TOKEN}" >> .npmrc + # Strip any leftover @athombv -> GitHub Packages mapping from .npmrc + # so the new version resolves from npmjs.org. Remove the file when + # nothing meaningful remains. + if [ -f .npmrc ]; then + sed -i '/@athombv:registry/d; /npm.pkg.github.com/d' .npmrc + if [ ! -s .npmrc ] || ! grep -qv '^\s*$' .npmrc; then + rm -f .npmrc + fi + fi # Update lockfile npm install --package-lock-only --ignore-scripts @@ -171,7 +177,12 @@ jobs: # Commit and push git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - git add package.json package-lock.json + # Stage the version bump, plus any add/modify/delete to .npmrc + # if it currently exists or is tracked in the repo. + git add -A -- package.json package-lock.json + if [ -e .npmrc ] || git ls-files --error-unmatch .npmrc > /dev/null 2>&1; then + git add -A -- .npmrc + fi git commit -m "chore: update @athombv/jsdoc-template to ${NEW_SPEC}" if ! git push --force origin "$BRANCH_NAME" 2>&1; then diff --git a/README.md b/README.md index 9a3d6e8..c4f1667 100644 --- a/README.md +++ b/README.md @@ -2,21 +2,13 @@ ## How to use this template -This package is publicly distributed via [GitHub Packages](https://github.com/athombv/homey-jsdoc-template/pkgs/npm/jsdoc-template). No GitHub token is required to install it. - -1. Add a `.npmrc` in the root of your project so npm resolves the `@athombv` scope to GitHub Packages: - -``` -@athombv:registry=https://npm.pkg.github.com -``` - -2. Install as a dev dependency +1. Install as a dev dependency ```bash $ npm i --save-dev @athombv/jsdoc-template ``` -3. Create `./jsdoc.json`: +2. Create `./jsdoc.json`: ```json { @@ -78,7 +70,7 @@ npm link @athombv/jsdoc-template ## Publishing 1. Bump the version on `master` via the [Version workflow](https://github.com/athombv/jsdoc-template/actions/workflows/version.yaml). -2. Merge `master` to `production` — this will automatically publish the release to the GitHub Packages Registry. +2. Merge `master` to `production` - this will automatically publish the release to npmjs.org via OIDC (Trusted Publisher). 3. Create PRs in dependent repos via the [Update Dependents workflow](https://github.com/athombv/jsdoc-template/actions/workflows/update-dependents.yaml). This will automatically discover repos in the `athombv` org that use `@athombv/jsdoc-template` and open PRs to update them to the new version. diff --git a/package.json b/package.json index 918d17c..62f8402 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ "/tmpl", "/publish.js" ], + "publishConfig": { + "access": "public" + }, "author": "Athom B.V.", "license": "GPL-3.0", "dependencies": {