Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 74 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ name: CI
on:
push:
pull_request:
# Run from the Actions tab or with: gh workflow run ci.yml -f version=0.1.3 -f tag=latest
workflow_dispatch:
inputs:
version:
description: Published version to tag, such as 0.1.3
required: true
type: string
tag:
description: Dist-tag to point at that version
required: true
default: latest
type: string
permissions:
contents: read
jobs:
build:
name: Build and test
if: github.event_name != 'workflow_dispatch'
runs-on: ubuntu-24.04
timeout-minutes: 30
steps:
Expand Down Expand Up @@ -38,51 +51,88 @@ jobs:
publish:
name: Publish to npm
needs: build
if: startsWith(github.ref, 'refs/tags/v')
# A merge to main publishes when package.json has a version that isn't on npm yet.
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
environment: npm
permissions:
contents: read
# Creates the v<version> tag and GitHub release.
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 26
registry-url: https://registry.npmjs.org
# Publish with the latest npm; trusted publishing (OIDC) needs npm 11.5.1 or newer.
- run: npm install --global npm@12.2.3
- uses: actions/download-artifact@v4
with:
name: puppet-neo-npm
path: artifacts
- name: Check the tag and choose the dist-tag
- name: Check the version and choose the dist-tag
id: release
env:
TAG: ${{ github.ref_name }}
run: |
version=$(jq -r .version package.json)
if [ "$TAG" != "v$version" ]; then
echo "::error::Tag $TAG does not match package version $version"
exit 1
fi
if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then
echo "::error::$TAG is not on main"
exit 1
if [ "$(npm view "wechaty-puppet-neo@$version" version 2>/dev/null)" = "$version" ]; then
echo "wechaty-puppet-neo@$version is already on npm, so there is nothing to publish."
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
# A leading ./ keeps npm from reading the path as a GitHub owner/repo shorthand.
tarball="./artifacts/wechaty-puppet-neo-$version.tgz"
test -f "$tarball"
# Wechaty versioning: an odd minor version or a prerelease is a development release.
minor=$(echo "$version" | cut -d. -f2)
if [ $((minor % 2)) -eq 1 ] || [[ "$version" == *-* ]]; then dist_tag=next; else dist_tag=latest; fi
echo "tarball=$tarball" >> "$GITHUB_OUTPUT"
echo "publish=true" >> "$GITHUB_OUTPUT"
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "dist-tag=$dist_tag" >> "$GITHUB_OUTPUT"
- uses: actions/download-artifact@v4
if: steps.release.outputs.publish == 'true'
with:
name: puppet-neo-npm
path: artifacts
# npm trusted publishing (OIDC): npmjs.com trusts this workflow (ci.yml) in the npm environment, so no token is needed.
# A leading ./ keeps npm from reading the path as a GitHub owner/repo shorthand.
- name: Publish
if: steps.release.outputs.publish == 'true'
env:
TARBALL: ${{ steps.release.outputs.tarball }}
VERSION: ${{ steps.release.outputs.version }}
DIST_TAG: ${{ steps.release.outputs.dist-tag }}
run: npm publish "$TARBALL" --access public --provenance --tag "$DIST_TAG"
run: npm publish "./artifacts/wechaty-puppet-neo-$VERSION.tgz" --access public --provenance --tag "$DIST_TAG"
- name: Tag the release on GitHub
if: steps.release.outputs.publish == 'true'
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ steps.release.outputs.version }}
DIST_TAG: ${{ steps.release.outputs.dist-tag }}
run: |
prerelease=
if [ "$DIST_TAG" = next ]; then prerelease=--prerelease; fi
gh release create "v$VERSION" --target "$GITHUB_SHA" --title "v$VERSION" --generate-notes $prerelease

dist-tag:
name: Move a dist-tag
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-24.04
environment: npm
permissions:
contents: read
id-token: write
steps:
- uses: actions/setup-node@v4
with:
node-version: 26
registry-url: https://registry.npmjs.org
# Dist-tags over trusted publishing (OIDC) need npm 12.2.0 or newer, and Allow npm dist-tag on npmjs.com.
- run: npm install --global npm@12.2.3
- name: Point the dist-tag at the version
env:
VERSION: ${{ inputs.version }}
TAG: ${{ inputs.tag }}
run: |
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ && "$TAG" =~ ^[a-z][a-z0-9-]*$ ]]; then
echo "::error::Give a version such as 0.1.3 and a lowercase dist-tag such as latest"
exit 1
fi
if [ "$(npm view "wechaty-puppet-neo@$VERSION" version 2>/dev/null)" != "$VERSION" ]; then
echo "::error::wechaty-puppet-neo@$VERSION is not on npm"
exit 1
fi
npm dist-tag add "wechaty-puppet-neo@$VERSION" "$TAG"
npm dist-tag ls wechaty-puppet-neo
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ Run `npm test` for type checking and the offline tests, and `npm run test:packag

### Releasing

CI publishes to npm when a `v<version>` tag on `main` is pushed and every platform build and package test passes. The tag must match the `package.json` version. An odd minor version, such as 0.1.x, publishes on the `next` dist-tag; an even minor version publishes on `latest`, following the [Wechaty versioning conventions](https://github.com/wechaty/wechaty/issues/905). The tarball carries npm provenance from this workflow, which publishes through npm trusted publishing (OIDC) with no npm token.
To release, merge a pull request that changes the version in `package.json`. When that commit lands on `main` and its build and package tests pass, CI publishes the version to npm, then creates the `v<version>` tag and a GitHub release with notes from the merged pull requests. A commit whose version is already on npm publishes nothing. An odd minor version, such as 0.1.x, publishes on the `next` dist-tag; an even minor version publishes on `latest`, following the [Wechaty versioning conventions](https://github.com/wechaty/wechaty/issues/905). The tarball carries npm provenance from this workflow, which publishes through npm trusted publishing (OIDC) with no npm token.

To point a dist-tag at a published version, run the workflow by hand:

```sh
git tag v0.1.3
git push origin v0.1.3
gh workflow run ci.yml -f version=0.1.3 -f tag=latest
```

### Development container
Expand Down
Loading