|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Publishes every @polycentric package to the GitLab package registry and, |
| 4 | +# when NPM_TOKEN is set, to public npm. Packages are published one at a time |
| 5 | +# so a version that already exists is skipped instead of aborting the whole |
| 6 | +# release (and blocking the packages ordered after it), keeping re-runs |
| 7 | +# idempotent. |
| 8 | +# |
| 9 | +# Env: |
| 10 | +# CI_COMMIT_TAG release tag, e.g. v2.0.2 (the leading "v" is stripped) |
| 11 | +# CI_SERVER_HOST GitLab host for the project package registry |
| 12 | +# CI_PROJECT_ID GitLab project id for the project package registry |
| 13 | +# NPM_TOKEN public npm auth token; public publish is skipped if unset |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +cd "$(dirname "$0")/../../.." |
| 17 | + |
| 18 | +VERSION="${CI_COMMIT_TAG#v}" |
| 19 | +echo "Publishing @polycentric packages at version ${VERSION}" |
| 20 | + |
| 21 | +# Keep prereleases (e.g. 2.0.0-alpha.1) off the `latest` dist-tag. |
| 22 | +DIST_TAG=latest |
| 23 | +if echo "$VERSION" | grep -q '-'; then |
| 24 | + DIST_TAG=$(echo "$VERSION" | sed -E 's/.*-([a-zA-Z]+).*/\1/') |
| 25 | + [ "$DIST_TAG" = "$VERSION" ] && DIST_TAG=next |
| 26 | +fi |
| 27 | +echo "Using npm dist-tag: ${DIST_TAG}" |
| 28 | + |
| 29 | +# Bump every @polycentric package to the release version. |
| 30 | +pnpm -r --filter "@polycentric/*" exec npm version "${VERSION}" --no-git-tag-version --allow-same-version |
| 31 | + |
| 32 | +# Topological order; pnpm rewrites workspace:* -> ${VERSION} on publish. |
| 33 | +PACKAGES="@polycentric/rs-core-uniffi-web @polycentric/js-storage-sqlite @polycentric/js-core @polycentric/js-browser @polycentric/js-node @polycentric/react-native" |
| 34 | + |
| 35 | +publish_all() { |
| 36 | + registry_url="$1" |
| 37 | + registry_label="$2" |
| 38 | + echo "Publishing to ${registry_label} (${registry_url})" |
| 39 | + pnpm config set @polycentric:registry "${registry_url}" |
| 40 | + for pkg in $PACKAGES; do |
| 41 | + set +e |
| 42 | + output=$(pnpm -r --filter "$pkg" publish --no-git-checks --access public --tag "$DIST_TAG" 2>&1) |
| 43 | + status=$? |
| 44 | + set -e |
| 45 | + echo "$output" |
| 46 | + if [ "$status" -ne 0 ]; then |
| 47 | + if echo "$output" | grep -qiE 'cannot publish over|previously published|already been taken|EPUBLISHCONFLICT|already exists'; then |
| 48 | + echo "==> ${pkg} is already published at ${VERSION} on ${registry_label}; skipping." |
| 49 | + else |
| 50 | + echo "==> ${pkg} failed to publish to ${registry_label}." >&2 |
| 51 | + exit "$status" |
| 52 | + fi |
| 53 | + fi |
| 54 | + done |
| 55 | +} |
| 56 | + |
| 57 | +publish_all "https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/" "the GitLab package registry" |
| 58 | + |
| 59 | +if [ -n "${NPM_TOKEN:-}" ]; then |
| 60 | + publish_all "https://registry.npmjs.org/" "public npm" |
| 61 | +else |
| 62 | + echo "NPM_TOKEN is not set; skipping public npm publish" >&2 |
| 63 | +fi |
0 commit comments