Skip to content

Commit 11fb139

Browse files
authored
feat: updated release workflow to automatically create a tag & release (#172)
* feat: updated release workflow to automatically create a tag & release * fix: added explanatory comments
1 parent 1c24db8 commit 11fb139

1 file changed

Lines changed: 75 additions & 5 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,84 @@
11
name: Publish package
22

33
on:
4-
release:
5-
types: [published]
64
workflow_dispatch:
75

86
permissions: {}
97

108
jobs:
119
publish-npm:
12-
name: Publish to npm (OIDC)
10+
name: Publish release to npm (OIDC)
1311
runs-on: ubuntu-latest
1412
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.
1616
id-token: write
1717
steps:
1818
- name: Checkout repo
1919
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
20+
with:
21+
fetch-depth: 0
2022

2123
- name: Setup environment (node + install)
2224
uses: ./.github/actions/node-setup
2325
with:
2426
node-version: 24
2527

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+
2682
- name: Build and validate package
2783
run: npm run prepublishOnly
2884

@@ -34,4 +90,18 @@ jobs:
3490
registry-url: https://registry.npmjs.org
3591

3692
- 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

Comments
 (0)