|
| 1 | + |
| 2 | +name: Publish To NPM |
| 3 | + |
| 4 | +on: |
| 5 | + release: |
| 6 | + types: [published] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + id-token: write # required for npm provenance |
| 11 | + packages: write # required for GitHub Packages publish |
| 12 | + |
| 13 | +jobs: |
| 14 | + build_and_pack: |
| 15 | + name: Build and pack workspaces (once) |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + is_prerelease: ${{ steps.version.outputs.is_prerelease }} |
| 19 | + publish_tag: ${{ steps.version.outputs.publish_tag }} |
| 20 | + steps: |
| 21 | + - name: Checkout repo |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Configure Node |
| 25 | + uses: actions/setup-node@v4 |
| 26 | + with: |
| 27 | + node-version: 20 |
| 28 | + |
| 29 | + - name: Install dependencies |
| 30 | + run: npm ci |
| 31 | + |
| 32 | + - name: Lint |
| 33 | + run: npm run lint |
| 34 | + |
| 35 | + - name: Run tests |
| 36 | + run: npm run test |
| 37 | + |
| 38 | + - name: Build |
| 39 | + run: npm run build |
| 40 | + |
| 41 | + # Compute prerelease flag & desired dist-tag from top-level package.json version |
| 42 | + - name: Determine prerelease tag |
| 43 | + id: version |
| 44 | + run: | |
| 45 | + set -euo pipefail |
| 46 | + VERSION="$(node -p 'require("./package.json").version')" |
| 47 | + echo "Detected version: $VERSION" |
| 48 | +
|
| 49 | + # If version contains a hyphen, it's a prerelease (e.g., 1.2.3-alpha.1) |
| 50 | + if [[ "$VERSION" == *-* ]]; then |
| 51 | + echo "is_prerelease=true" >> "$GITHUB_OUTPUT" |
| 52 | + echo "publish_tag=prerelease" >> "$GITHUB_OUTPUT" |
| 53 | + echo "This is a prerelease. Will use tag 'prerelease'." |
| 54 | + else |
| 55 | + echo "is_prerelease=false" >> "$GITHUB_OUTPUT" |
| 56 | + echo "publish_tag=latest" >> "$GITHUB_OUTPUT" |
| 57 | + echo "This is a stable release. Will use tag 'latest'." |
| 58 | + fi |
| 59 | +
|
| 60 | + # Create tarballs for each workspace so we can publish the exact same artifacts twice |
| 61 | + - name: Pack workspaces |
| 62 | + run: | |
| 63 | + set -euo pipefail |
| 64 | + npm pack --workspaces |
| 65 | + echo "Packed tarballs:" |
| 66 | + ls -1 *.tgz |
| 67 | +
|
| 68 | + - name: Upload packed artifacts |
| 69 | + uses: actions/upload-artifact@v4 |
| 70 | + with: |
| 71 | + name: npm-tarballs |
| 72 | + path: | |
| 73 | + ./*.tgz |
| 74 | + if-no-files-found: error |
| 75 | + retention-days: 7 |
| 76 | + |
| 77 | + publish_npm: |
| 78 | + name: Publish to npmjs.org |
| 79 | + runs-on: ubuntu-latest |
| 80 | + needs: build_and_pack |
| 81 | + steps: |
| 82 | + - name: Download packed artifacts |
| 83 | + uses: actions/download-artifact@v4 |
| 84 | + with: |
| 85 | + name: npm-tarballs |
| 86 | + path: ./dist-tarballs |
| 87 | + |
| 88 | + - name: Configure Node for npmjs.org |
| 89 | + uses: actions/setup-node@v4 |
| 90 | + with: |
| 91 | + node-version: 20 |
| 92 | + registry-url: https://registry.npmjs.org |
| 93 | + always-auth: true |
| 94 | + |
| 95 | + - name: Publish tarballs to npmjs.org (with provenance and dist-tag) |
| 96 | + env: |
| 97 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # npm automation token |
| 98 | + PUBLISH_TAG: ${{ needs.build_and_pack.outputs.publish_tag }} |
| 99 | + run: | |
| 100 | + set -euo pipefail |
| 101 | + shopt -s nullglob |
| 102 | + for tgz in dist-tarballs/*.tgz; do |
| 103 | + echo "Publishing $tgz to npmjs.org with tag '${PUBLISH_TAG}' ..." |
| 104 | + # --access public needed for first publish of public packages |
| 105 | + npm publish "$tgz" --provenance --access public --tag "${PUBLISH_TAG}" |
| 106 | + done |
| 107 | +
|
| 108 | + publish_github: |
| 109 | + name: Publish to GitHub Packages |
| 110 | + runs-on: ubuntu-latest |
| 111 | + needs: build_and_pack |
| 112 | + steps: |
| 113 | + - name: Download packed artifacts |
| 114 | + uses: actions/download-artifact@v4 |
| 115 | + with: |
| 116 | + name: npm-tarballs |
| 117 | + path: ./dist-tarballs |
| 118 | + |
| 119 | + - name: Configure Node for GitHub Packages |
| 120 | + uses: actions/setup-node@v4 |
| 121 | + with: |
| 122 | + node-version: 20 |
| 123 | + registry-url: https://npm.pkg.github.com |
| 124 | + scope: '@finos' |
| 125 | + always-auth: true |
| 126 | + |
| 127 | + - name: Publish tarballs to GitHub Packages (with dist-tag) |
| 128 | + env: |
| 129 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # has packages: write via permissions |
| 130 | + PUBLISH_TAG: ${{ needs.build_and_pack.outputs.publish_tag }} |
| 131 | + run: | |
| 132 | + set -euo pipefail |
| 133 | + shopt -s nullglob |
| 134 | + for tgz in dist-tarballs/*.tgz; do |
| 135 | + echo "Publishing $tgz to GitHub Packages with tag '${PUBLISH_TAG}' ..." |
| 136 | + # GitHub Packages does not support npm provenance; omit --provenance and --access |
| 137 | + npm publish "$tgz" --tag "${PUBLISH_TAG}" |
0 commit comments