|
1 | 1 | name: Publish package |
2 | 2 |
|
3 | 3 | on: |
4 | | - release: |
5 | | - types: [published] |
6 | 4 | workflow_dispatch: |
7 | 5 |
|
8 | 6 | permissions: {} |
9 | 7 |
|
10 | 8 | jobs: |
11 | 9 | publish-npm: |
12 | | - name: Publish to npm (OIDC) |
| 10 | + name: Publish release to npm (OIDC) |
13 | 11 | runs-on: ubuntu-latest |
14 | 12 | permissions: |
15 | | - contents: read |
| 13 | + # Needed to create the GitHub release and its package-version tag. |
| 14 | + contents: write |
| 15 | + # Needed by npm trusted publishing to mint an OIDC token. |
16 | 16 | id-token: write |
17 | 17 | steps: |
18 | 18 | - name: Checkout repo |
19 | 19 | uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
20 | 22 |
|
21 | 23 | - name: Setup environment (node + install) |
22 | 24 | uses: ./.github/actions/node-setup |
23 | 25 | with: |
24 | 26 | node-version: 24 |
25 | 27 |
|
| 28 | + - name: Read package metadata |
| 29 | + id: package |
| 30 | + shell: bash |
| 31 | + run: | |
| 32 | + set -euo pipefail |
| 33 | + name="$(node -p "JSON.parse(require('node:fs').readFileSync('package.json', 'utf8')).name")" |
| 34 | + version="$(node -p "JSON.parse(require('node:fs').readFileSync('package.json', 'utf8')).version")" |
| 35 | +
|
| 36 | + if [[ -z "${name}" || -z "${version}" ]]; then |
| 37 | + echo "package.json must define both name and version." >&2 |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | +
|
| 41 | + echo "name=${name}" >> "${GITHUB_OUTPUT}" |
| 42 | + echo "version=${version}" >> "${GITHUB_OUTPUT}" |
| 43 | + echo "tag_name=${version}" >> "${GITHUB_OUTPUT}" |
| 44 | +
|
| 45 | + - name: Ensure workflow is running on the default branch |
| 46 | + env: |
| 47 | + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} |
| 48 | + shell: bash |
| 49 | + run: | |
| 50 | + set -euo pipefail |
| 51 | +
|
| 52 | + if [[ "${GITHUB_REF_TYPE}" != "branch" || "${GITHUB_REF_NAME}" != "${DEFAULT_BRANCH}" ]]; then |
| 53 | + echo "Releases must be published from ${DEFAULT_BRANCH}; this run is using ${GITHUB_REF_NAME}." >&2 |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +
|
| 57 | + - name: Ensure package version is releasable |
| 58 | + env: |
| 59 | + GH_TOKEN: ${{ github.token }} |
| 60 | + PACKAGE_NAME: ${{ steps.package.outputs.name }} |
| 61 | + VERSION: ${{ steps.package.outputs.version }} |
| 62 | + TAG_NAME: ${{ steps.package.outputs.tag_name }} |
| 63 | + shell: bash |
| 64 | + run: | |
| 65 | + set -euo pipefail |
| 66 | +
|
| 67 | + if git ls-remote --exit-code --tags origin "refs/tags/${TAG_NAME}" >/dev/null 2>&1; then |
| 68 | + echo "Tag ${TAG_NAME} already exists." >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + if gh release view "${TAG_NAME}" >/dev/null 2>&1; then |
| 73 | + echo "Release ${TAG_NAME} already exists." >&2 |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +
|
| 77 | + if npm view "${PACKAGE_NAME}@${VERSION}" version >/dev/null 2>&1; then |
| 78 | + echo "Package ${PACKAGE_NAME}@${VERSION} is already published to npm." >&2 |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | +
|
26 | 82 | - name: Build and validate package |
27 | 83 | run: npm run prepublishOnly |
28 | 84 |
|
|
34 | 90 | registry-url: https://registry.npmjs.org |
35 | 91 |
|
36 | 92 | - name: Publish to npm |
37 | | - run: npm publish --provenance --access public |
| 93 | + run: npm publish --provenance --access public --tag latest |
| 94 | + |
| 95 | + - name: Create GitHub release |
| 96 | + env: |
| 97 | + GH_TOKEN: ${{ github.token }} |
| 98 | + PACKAGE_NAME: ${{ steps.package.outputs.name }} |
| 99 | + VERSION: ${{ steps.package.outputs.version }} |
| 100 | + TAG_NAME: ${{ steps.package.outputs.tag_name }} |
| 101 | + shell: bash |
| 102 | + run: | |
| 103 | + set -euo pipefail |
| 104 | + gh release create "${TAG_NAME}" \ |
| 105 | + --target "${GITHUB_SHA}" \ |
| 106 | + --title "${TAG_NAME}" \ |
| 107 | + --notes "Published ${PACKAGE_NAME}@${VERSION} to npm." |
0 commit comments