|
1 | | -name: Publish prerelease |
| 1 | +name: Publish Release |
2 | 2 |
|
3 | 3 | on: |
4 | | - # Allows you to run this workflow manually from the Actions tab |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v[0-9]+.[0-9]+.[0-9]+*' |
5 | 7 | workflow_dispatch: |
6 | 8 |
|
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +# This workflow will perform a publish whenever it is triggered |
| 17 | +# If you are using a fork, and want to push tags you can disable this workflow in the github ui |
7 | 18 | jobs: |
8 | | - prerelease: |
9 | | - name: Prerelease |
| 19 | + test: |
| 20 | + name: Test |
10 | 21 | runs-on: ubuntu-latest |
11 | 22 | timeout-minutes: 15 |
12 | 23 |
|
| 24 | + strategy: |
| 25 | + fail-fast: false |
| 26 | + matrix: |
| 27 | + node-version: [20.x, 22.x, 24.x] |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Git checkout |
| 31 | + uses: actions/checkout@v5 |
| 32 | + with: |
| 33 | + persist-credentials: false |
| 34 | + - name: Use Node.js ${{ matrix.node-version }} |
| 35 | + uses: actions/setup-node@v6 |
| 36 | + with: |
| 37 | + node-version: ${{ matrix.node-version }} |
| 38 | + - name: Prepare Environment |
| 39 | + run: | |
| 40 | + corepack enable |
| 41 | + yarn install |
| 42 | + env: |
| 43 | + CI: true |
| 44 | + - name: Run tests |
| 45 | + run: | |
| 46 | + yarn unit |
| 47 | + env: |
| 48 | + CI: true |
| 49 | + |
| 50 | + prepare: |
| 51 | + name: Prepare package |
| 52 | + runs-on: ubuntu-latest |
| 53 | + outputs: |
| 54 | + tag: ${{ steps.do-publish.outputs.tag }} |
| 55 | + prerelease: ${{ steps.do-publish.outputs.prerelease }} |
| 56 | + timeout-minutes: 15 |
| 57 | + |
| 58 | + permissions: |
| 59 | + contents: write |
| 60 | + |
13 | 61 | steps: |
14 | 62 | - uses: actions/checkout@v5 |
15 | 63 | with: |
16 | 64 | fetch-depth: 0 |
17 | 65 | persist-credentials: false |
18 | 66 | - name: Use Node.js 22.x |
19 | | - uses: actions/setup-node@v4 |
| 67 | + uses: actions/setup-node@v6 |
20 | 68 | with: |
21 | 69 | node-version: 22.x |
22 | | - - name: Check release is desired |
| 70 | + - name: Enable corepack |
| 71 | + run: corepack enable |
| 72 | + - name: Determine publish info |
23 | 73 | id: do-publish |
24 | 74 | run: | |
25 | | - if [ -z "${{ secrets.NPM_TOKEN }}" ]; then |
26 | | - echo "No Token" |
27 | | - elif [[ "${{ github.ref }}" == "refs/heads/master" ]]; then |
28 | | - echo "Publish nightly" |
29 | | - echo "publish=nightly" >> $GITHUB_OUTPUT |
| 75 | + # If this run was started manually, choose nightly for main and experimental otherwise. |
| 76 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 77 | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 78 | + echo "Publishing nightly" |
| 79 | + echo "tag=nightly" >> $GITHUB_OUTPUT |
| 80 | + else |
| 81 | + echo "Publishing experimental" |
| 82 | + echo "tag=experimental" >> $GITHUB_OUTPUT |
| 83 | + fi |
| 84 | + |
| 85 | + HASH=$(git rev-parse --short HEAD) |
| 86 | + TIMESTAMP=$(date +"%Y%m%d-%H%M%S") |
| 87 | + PRERELEASE_TAG=nightly-$(echo "${{ github.ref_name }}" | sed -r 's/[^a-z0-9]+/-/gi') |
| 88 | + echo "prerelease=${PRERELEASE_TAG}-${TIMESTAMP}-${HASH}" >> $GITHUB_OUTPUT |
| 89 | +
|
30 | 90 | else |
31 | | - echo "Publish experimental" |
32 | | - echo "publish=experimental" >> $GITHUB_OUTPUT |
| 91 | + # Otherwise (push by tag), keep the previous logic: compare published vs package.json |
| 92 | + PUBLISHED_VERSION=$(yarn npm info --json . | jq -c '.version' -r) |
| 93 | + THIS_VERSION=$(node -p "require('./package.json').version") |
| 94 | + # Simple bash helper to compare version numbers |
| 95 | + verlte() { |
| 96 | + [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ] |
| 97 | + } |
| 98 | + verlt() { |
| 99 | + [ "$1" = "$2" ] && return 1 || verlte $1 $2 |
| 100 | + } |
| 101 | + if verlt $PUBLISHED_VERSION $THIS_VERSION |
| 102 | + then |
| 103 | + echo "Publishing latest" |
| 104 | + echo "tag=latest" >> $GITHUB_OUTPUT |
| 105 | + else |
| 106 | + echo "Publishing hotfix" |
| 107 | + echo "tag=hotfix" >> $GITHUB_OUTPUT |
| 108 | + fi |
33 | 109 | fi |
34 | | - - name: Prepare Environment |
35 | | - if: ${{ steps.do-publish.outputs.publish }} |
| 110 | + - name: Prepare build |
36 | 111 | run: | |
37 | | - corepack enable |
38 | 112 | yarn install |
| 113 | +
|
| 114 | + # Bump to prerelease version if needed |
| 115 | + if [ "${{ steps.do-publish.outputs.prerelease }}" != "" ]; then |
| 116 | + OLD_VERSION=$(node -p "require('./package.json').version") |
| 117 | + yarn version ${OLD_VERSION}-${{ steps.do-publish.outputs.prerelease }} |
| 118 | + fi |
| 119 | +
|
| 120 | + yarn build |
39 | 121 | env: |
40 | 122 | CI: true |
41 | | - - name: Bump version and build |
42 | | - if: ${{ steps.do-publish.outputs.publish }} |
43 | | - run: | |
44 | | - PRERELEASE_TAG=nightly-$(echo "${{ github.ref_name }}" | sed -r 's/[^a-z0-9]+/-/gi') |
45 | | - yarn release --prerelease $PRERELEASE_TAG |
46 | | - env: |
47 | | - CI: true |
| 123 | + |
| 124 | + - name: Upload release artifact |
| 125 | + uses: actions/upload-artifact@v4 |
| 126 | + with: |
| 127 | + name: publish-dist |
| 128 | + path: | |
| 129 | + dist |
| 130 | + package.json |
| 131 | + retention-days: 1 |
| 132 | + if-no-files-found: error |
| 133 | + |
| 134 | + publish: |
| 135 | + name: Publish to NPM |
| 136 | + needs: |
| 137 | + - prepare |
| 138 | + - test |
| 139 | + runs-on: ubuntu-latest |
| 140 | + permissions: |
| 141 | + contents: read |
| 142 | + id-token: write # scoped for as short as possible, as this gives write access to npm |
| 143 | + |
| 144 | + steps: |
| 145 | + - uses: actions/checkout@v5 |
| 146 | + with: |
| 147 | + fetch-depth: 0 |
| 148 | + persist-credentials: false |
| 149 | + - name: Use Node.js 22.x |
| 150 | + uses: actions/setup-node@v6 |
| 151 | + with: |
| 152 | + node-version: 22.x |
| 153 | + |
| 154 | + - name: Download release artifact |
| 155 | + uses: actions/download-artifact@v5 |
| 156 | + with: |
| 157 | + name: publish-dist |
| 158 | + |
48 | 159 | - name: Publish to NPM |
49 | | - if: ${{ steps.do-publish.outputs.publish }} |
50 | 160 | run: | |
51 | | - yarn config set npmAuthToken $NPM_AUTH_TOKEN |
| 161 | + corepack enable |
| 162 | + yarn install |
52 | 163 |
|
53 | | - NEW_VERSION=$(node -p "require('./package.json').version") |
54 | | - yarn npm publish --access=public --tag "${{ steps.do-publish.outputs.publish }}" |
| 164 | + # Publish from the extracted release (build output present) |
| 165 | + yarn npm publish --access=public --provenance --tag ${{ needs.prepare.outputs.tag }} |
55 | 166 |
|
| 167 | + NEW_VERSION=$(node -p "require('./package.json').version") |
56 | 168 | echo "**Published:** $NEW_VERSION" >> $GITHUB_STEP_SUMMARY |
57 | 169 | env: |
58 | | - NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
59 | 170 | CI: true |
0 commit comments