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