Skip to content

Commit 6a049fd

Browse files
committed
Release automatically when the version in package.json changes
Add a release job to the Lint and Test workflow. On a push to the default branch, after lint and test pass, it reads the version from package.json and, when the tag v<version> does not exist yet, creates it together with the GitHub release. Tags created with the workflow token do not start other workflows, so the Release workflow is now also callable and is run from the same workflow run with the new tag; its tag trigger is kept so a tag pushed by hand still publishes. Document the procedure in the README. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 619ed8c commit 6a049fd

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎.github/workflows/default.yml‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,61 @@ jobs:
3737
run: npm ci
3838
- name: Test
3939
run: npm test
40+
41+
release:
42+
needs: [lint, test]
43+
if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
44+
runs-on: ubuntu-latest
45+
permissions:
46+
contents: write
47+
concurrency:
48+
group: release
49+
cancel-in-progress: false
50+
outputs:
51+
tag: ${{ steps.version.outputs.tag }}
52+
created: ${{ steps.release.outputs.created }}
53+
steps:
54+
- name: Checkout repository
55+
uses: actions/checkout@v4
56+
- name: Read version
57+
id: version
58+
run: |
59+
version=$(sed -nE 's/^ *"version": *"([^"]+)".*/\1/p' package.json | head -1)
60+
if [ -z "$version" ]; then
61+
echo "No version found in package.json" >&2
62+
exit 1
63+
fi
64+
echo "tag=v$version" >> "$GITHUB_OUTPUT"
65+
- name: Create tag and release
66+
id: release
67+
env:
68+
GH_TOKEN: ${{ github.token }}
69+
TAG: ${{ steps.version.outputs.tag }}
70+
run: |
71+
status=0
72+
git ls-remote --exit-code --tags origin "refs/tags/$TAG" > /dev/null || status=$?
73+
case "$status" in
74+
0)
75+
echo "Tag $TAG already exists; nothing to release."
76+
echo "created=false" >> "$GITHUB_OUTPUT"
77+
;;
78+
2)
79+
gh release create "$TAG" --target "$GITHUB_SHA" --title "$TAG" --generate-notes
80+
echo "created=true" >> "$GITHUB_OUTPUT"
81+
;;
82+
*)
83+
echo "Could not check whether tag $TAG exists (git ls-remote exited with $status)" >&2
84+
exit "$status"
85+
;;
86+
esac
87+
88+
publish:
89+
needs: release
90+
if: needs.release.outputs.created == 'true'
91+
permissions:
92+
contents: read
93+
id-token: write
94+
uses: ./.github/workflows/release.yml
95+
with:
96+
tag: ${{ needs.release.outputs.tag }}
97+
secrets: inherit

‎.github/workflows/release.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ on:
44
push:
55
tags:
66
- '**'
7+
workflow_call:
8+
inputs:
9+
tag:
10+
description: Tag to publish, for example v3.1.0
11+
required: true
12+
type: string
713

814
jobs:
915
lint:
1016
runs-on: ubuntu-latest
1117
steps:
1218
- name: Checkout repository
1319
uses: actions/checkout@v4
20+
with:
21+
ref: ${{ inputs.tag || github.ref }}
1422
- name: Install node
1523
uses: actions/setup-node@v4
1624
with:
@@ -25,6 +33,8 @@ jobs:
2533
steps:
2634
- name: Checkout repository
2735
uses: actions/checkout@v4
36+
with:
37+
ref: ${{ inputs.tag || github.ref }}
2838
- name: Install node
2939
uses: actions/setup-node@v4
3040
with:
@@ -43,6 +53,8 @@ jobs:
4353
steps:
4454
- name: Checkout repository
4555
uses: actions/checkout@v4
56+
with:
57+
ref: ${{ inputs.tag || github.ref }}
4658
- name: Install node
4759
uses: actions/setup-node@v4
4860
with:

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Requires **Node.js 20.18.1** or later. HTTP requests go through [undici](https:/
1818
- [Connection reuse and HTTP/2](#connection-reuse-and-http2)
1919
- [Response Fields](#response-fields)
2020
- [TypeScript](#typescript)
21+
- [Releasing](#releasing)
2122

2223
## Install
2324

@@ -236,3 +237,7 @@ The library is written in TypeScript and exports the following types:
236237
```ts
237238
import IPData, { EU_BASE_URL, CacheConfig, HttpConfig, LookupParams, LookupResponse } from 'ipdata';
238239
```
240+
241+
## Releasing
242+
243+
Releases are cut by CI. Bump `version` in `package.json` in a pull request (`npm version x.y.z --no-git-tag-version` also updates `package-lock.json`) and merge it. On the merge to `main`, CI tags the commit `vx.y.z`, creates the GitHub release with generated notes and publishes the package to [npm](https://www.npmjs.com/package/ipdata). A merge whose version is already tagged publishes nothing. Pushing a `vx.y.z` tag by hand still publishes that tag.

0 commit comments

Comments
 (0)