|
| 1 | +name: Check NPM token |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - master |
| 10 | + workflow_dispatch: {} |
| 11 | + |
| 12 | +jobs: |
| 13 | + check-token: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Use Node.js |
| 19 | + uses: actions/setup-node@v4 |
| 20 | + with: |
| 21 | + node-version: '18' |
| 22 | + registry-url: 'https://registry.npmjs.org/' |
| 23 | + |
| 24 | + - name: Ensure NPM_TOKEN is set |
| 25 | + env: |
| 26 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 27 | + run: | |
| 28 | + if [ -z "${NPM_TOKEN}" ]; then |
| 29 | + echo "ERROR: secrets.NPM_TOKEN is not set. Add an npm automation token to Repository settings → Secrets → Actions as NPM_TOKEN." >&2 |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + echo "NPM_TOKEN present (hidden)." |
| 33 | +
|
| 34 | + - name: Configure npm auth |
| 35 | + env: |
| 36 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 37 | + run: | |
| 38 | + # write npm token to ~/.npmrc for authenticated registry access |
| 39 | + echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc |
| 40 | + echo "Wrote ~/.npmrc" |
| 41 | +
|
| 42 | + - name: Verify npm authentication |
| 43 | + run: | |
| 44 | + set -o pipefail |
| 45 | + if ! npm whoami 2>&1 | tee /tmp/npm-whoami.log; then |
| 46 | + echo "\nERROR: npm whoami failed. Possible causes:" >&2 |
| 47 | + echo " - NPM_TOKEN is invalid or revoked" >&2 |
| 48 | + echo " - Token user does not have publish access to @hawk.so scope" >&2 |
| 49 | + echo " - Organization requires 2FA or specific automation token" >&2 |
| 50 | + echo "\nTo debug locally, run:" >&2 |
| 51 | + echo " echo \"//registry.npmjs.org/:_authToken=YOUR_TOKEN\" > ~/.npmrc" >&2 |
| 52 | + echo " npm whoami" >&2 |
| 53 | + echo "\nSee https://docs.npmjs.com/ for token and organization settings." >&2 |
| 54 | + echo "\nLast npm whoami output:" >&2 |
| 55 | + sed -n '1,200p' /tmp/npm-whoami.log >&2 || true |
| 56 | + exit 1 |
| 57 | + else |
| 58 | + echo "npm whoami succeeded - token is valid." |
| 59 | + fi |
| 60 | +
|
| 61 | + - name: 'Optional: Check package visibility' |
| 62 | + run: | |
| 63 | + # check if package metadata is available (non-fatal) |
| 64 | + if npm view @hawk.so/types version >/dev/null 2>&1; then |
| 65 | + echo "Package @hawk.so/types exists on registry."; |
| 66 | + else |
| 67 | + echo "Note: package @hawk.so/types not found or private. This may be ok for first-time publish."; |
| 68 | + fi |
0 commit comments