|
5 | 5 | branches: [master] |
6 | 6 | pull_request: |
7 | 7 | branches: [master] |
| 8 | + release: |
| 9 | + types: [published, prereleased] |
8 | 10 |
|
9 | 11 | jobs: |
10 | 12 | lint-and-build: |
|
71 | 73 | test -f result/index.d.ts |
72 | 74 | test -f result/sindri/index.js |
73 | 75 | test -f result/sindri/index.d.ts |
| 76 | +
|
| 77 | + deploy: |
| 78 | + name: publish to npm on release |
| 79 | + if: github.event_name == 'release' |
| 80 | + needs: lint-and-build |
| 81 | + runs-on: ubuntu-latest |
| 82 | + permissions: |
| 83 | + contents: write # Needed to upload the .tgz to the GitHub Release. |
| 84 | + steps: |
| 85 | + - name: Checkout repository |
| 86 | + uses: actions/checkout@v4 |
| 87 | + |
| 88 | + - name: Setup Node.js |
| 89 | + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.0.3 |
| 90 | + with: |
| 91 | + node-version: '22' |
| 92 | + cache: 'yarn' |
| 93 | + registry-url: 'https://registry.npmjs.org' |
| 94 | + always-auth: true |
| 95 | + |
| 96 | + - name: Install Nix |
| 97 | + uses: cachix/install-nix-action@c134e4c9e34bac6cab09cf239815f9339aaaf84e # v31 |
| 98 | + with: |
| 99 | + github_access_token: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + |
| 101 | + - name: Install dependencies |
| 102 | + run: | |
| 103 | + yarn install |
| 104 | + cd examples |
| 105 | + yarn install |
| 106 | +
|
| 107 | + - name: Build the package |
| 108 | + run: nix build |
| 109 | + |
| 110 | + - name: Prepare dist and set version from release tag |
| 111 | + env: |
| 112 | + TAG_NAME: ${{ github.event.release.tag_name }} |
| 113 | + run: | |
| 114 | + set -euo pipefail |
| 115 | + rm -rf dist |
| 116 | + mkdir -p dist |
| 117 | + # Copy without preserving ownership so files are writable. |
| 118 | + cp -R result/. dist/ |
| 119 | + chmod -R u+rwX dist |
| 120 | +
|
| 121 | + # Validate and extract semver from tag: vMAJOR.MINOR.PATCH(-prerelease)? |
| 122 | + if [[ "${TAG_NAME}" =~ ^v([0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z0-9\.-]+)?)$ ]]; then |
| 123 | + VERSION="${BASH_REMATCH[1]}" |
| 124 | + else |
| 125 | + echo "Release tag must match ^vMAJOR.MINOR.PATCH(-prerelease)? ; got: ${TAG_NAME}" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | +
|
| 129 | + # Sanity-check package name. |
| 130 | + NAME=$(jq -r '.name' dist/package.json) |
| 131 | + if [[ "${NAME}" != "@sindrilabs/openai" ]]; then |
| 132 | + echo "Refusing to publish unexpected package name: ${NAME}" |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | +
|
| 136 | + # Patch version. |
| 137 | + tmp=$(mktemp) |
| 138 | + jq --arg v "${VERSION}" '.version = $v' dist/package.json > "${tmp}" |
| 139 | + mv "${tmp}" dist/package.json |
| 140 | + echo "Ready to publish ${NAME}@${VERSION}" |
| 141 | +
|
| 142 | + - name: Pack tarball |
| 143 | + env: |
| 144 | + TAG_NAME: ${{ github.event.release.tag_name }} |
| 145 | + run: | |
| 146 | + set -euo pipefail |
| 147 | + pushd dist >/dev/null |
| 148 | + PKG_TGZ=$(npm pack) |
| 149 | + popd >/dev/null |
| 150 | + mv "dist/${PKG_TGZ}" "sindrilabs-openai.${TAG_NAME}.tgz" |
| 151 | + ls -l "sindrilabs-openai.${TAG_NAME}.tgz" |
| 152 | +
|
| 153 | + - name: Publish to npm |
| 154 | + env: |
| 155 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # Used by setup-node's npm rc. |
| 156 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 157 | + run: | |
| 158 | + set -euo pipefail |
| 159 | + # Publish scoped package publicly; always tag as latest (even for pre-releases). |
| 160 | + (cd dist && npm publish --access public --tag latest) |
| 161 | +
|
| 162 | + - name: Attach tarball to GitHub Release |
| 163 | + uses: softprops/action-gh-release@v2 |
| 164 | + with: |
| 165 | + files: sindrilabs-openai.${{ github.event.release.tag_name }}.tgz |
0 commit comments