Skip to content

Commit

Permalink
ci: reject release if the tag fails any version validation
Browse files Browse the repository at this point in the history
 - must be consistent in the repo and the tag
 - must be more recent than latest on NPM

This solution requires jq, but that's OK because every runner image on
GitHub Actions has jq pre-installed.

Some of the tests with jq are equivalent to, e.g.,
`npm view dist/packages/web-component version`
but doing them with `jq` allows us to apply them earlier, before we even
make the builds.
  • Loading branch information
joanise committed May 2, 2024
1 parent 2604892 commit c67a8e6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
needs: test
steps:
- uses: actions/checkout@v4
- name: Stop early if the tag fails any validation
run: sh validate-version.sh ${{ github.ref_name }}
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
Expand All @@ -34,13 +36,6 @@ jobs:
- name: Check that the web-component bundle was up to date
run: |
if git status --porcelain | grep packages/studio-web/src/assets/; then echo ERROR: The web-component bundle is out of date. Please run \"npx nx bundle web-component\" and commit the results, then update the tag and try again.; false; fi
- name: Check that the versions are in sync
run: |
wcver=$(npm view dist/packages/web-component version)
nwcver=$(npm view dist/packages/ngx-web-component version)
pdep=$(npm view dist/packages/ngx-web-component peerDependencies.@readalongs/web-component)
if test "$wcver" != "$nwcver"; then echo ERROR: The ngx-web-component version $nwcver does not match the web-component version $wcver. Please update packages/web-component/package.json and packages/ngx-web-component/package.json, commit the results, update the tag and try again.; false; fi
if test "$pdep" != "$wcver"; then echo ERROR: ngx-web-component has a peerDependency on @readalongs/web-component version $pdep, which does not match the web-component version $wcver. Please update packages/ngx-web-component/package.json, commit the results, update the tag and try again.; false; fi
- name: Publish web-component to npmjs
run: |
cd dist/packages/web-component && npm publish --access=public
Expand Down
59 changes: 59 additions & 0 deletions validate-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/sh

INPUT_TAG="$1"

if [ -z "$INPUT_TAG" ]; then
echo "Usage: $0 <tag>"
exit 1
fi

VERSION=${INPUT_TAG#v}
BAD=

if [ "$(jq -r .version packages/web-component/package.json)" != "$VERSION" ]; then
echo "ERROR: version mismatch in packages/web-component/package.json"
BAD=1
fi

if [ "$(jq -r .version packages/ngx-web-component/package.json)" != "$VERSION" ]; then
echo "ERROR: version mismatch in packages/ngx-web-component/package.json"
BAD=1
fi

if [ "$(jq -r '.peerDependencies."@readalongs/web-component"' packages/ngx-web-component/package.json)" != "$VERSION" ]; then
echo "ERROR: peer dependency on @readalongs/web-component version mismatch in packages/ngx-web-component/package.json"
BAD=1
fi

if [ "$(jq -r .singleFileBundleVersion packages/studio-web/package.json)" != "$VERSION" ]; then
echo "ERROR: singleFileBundleVersion mismatch in packages/studio-web/package.json"
BAD=1
fi

version_less_than() {
{ echo "$1"; echo "$2"; } | sort -V -C && [ "$1" != "$2" ]
}

NPM_VERSION_WEBC=$(npm view @readalongs/web-component version)
echo "Published NPM @readalongs/web-component version: $NPM_VERSION_WEBC"
NPM_VERSION_NGXWEBC=$(npm view @readalongs/ngx-web-component version)
echo "Published NPM @readalongs/ngx-web-component version: $NPM_VERSION_NGXWEBC"
echo "New version: $VERSION"

if [ "$NPM_VERSION_WEBC" != "$NPM_VERSION_NGXWEBC" ]; then
echo "WARNING: published NPM @readalongs/web-component and @readalongs/ngx-web-component versions differ"
fi

if ! version_less_than "$NPM_VERSION_WEBC" "$VERSION"; then
echo "ERROR: new version $VERSION is not greater than published NPM @readalongs/web-component version $NPM_VERSION_WEBC"
BAD=1
fi

if ! version_less_than "$NPM_VERSION_NGXWEBC" "$VERSION"; then
echo "ERROR: new version $VERSION is not greater than published NPM @readalongs/ngx-web-component version $NPM_VERSION_NGXWEBC"
BAD=1
fi

if [ -n "$BAD" ]; then
exit 1
fi

0 comments on commit c67a8e6

Please sign in to comment.