|
| 1 | +name: Publish react-intl-universal |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - beta |
| 7 | + - master |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + id-token: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: npm-publish-${{ github.ref_name }} |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + publish: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + defaults: |
| 22 | + run: |
| 23 | + working-directory: packages/react-intl-universal |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Setup Node.js |
| 30 | + uses: actions/setup-node@v4 |
| 31 | + with: |
| 32 | + node-version: 24 |
| 33 | + registry-url: https://registry.npmjs.org/ |
| 34 | + cache: npm |
| 35 | + cache-dependency-path: packages/react-intl-universal/package-lock.json |
| 36 | + |
| 37 | + - name: Use npm version with trusted publishing support |
| 38 | + run: | |
| 39 | + npm install --global npm@latest |
| 40 | + node --version |
| 41 | + npm --version |
| 42 | +
|
| 43 | + - name: Install dependencies |
| 44 | + run: npm ci |
| 45 | + |
| 46 | + # Release rules: |
| 47 | + # - beta branch publishes <package.json version>-beta.N with the npm "beta" dist-tag. |
| 48 | + # - master branch publishes package.json version unchanged with the npm "latest" dist-tag. |
| 49 | + # Keep package.json version as the stable base version, for example 2.14.0. |
| 50 | + - name: Prepare release version |
| 51 | + id: release |
| 52 | + run: | |
| 53 | + set -euo pipefail |
| 54 | +
|
| 55 | + PACKAGE_NAME="$(node -p "require('./package.json').name")" |
| 56 | + SOURCE_VERSION="$(node -p "require('./package.json').version")" |
| 57 | + BRANCH_NAME="${GITHUB_REF_NAME}" |
| 58 | +
|
| 59 | + if [[ ! "${SOURCE_VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 60 | + echo "package.json should keep the stable base version, but it has ${SOURCE_VERSION}." |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +
|
| 64 | + if [[ "${BRANCH_NAME}" == "beta" ]]; then |
| 65 | + DIST_TAG="beta" |
| 66 | + BASE_VERSION="${SOURCE_VERSION}" |
| 67 | +
|
| 68 | + if npm view "${PACKAGE_NAME}@${BASE_VERSION}" version >/dev/null 2>&1; then |
| 69 | + echo "${PACKAGE_NAME}@${BASE_VERSION} is already published as a stable version. Bump package.json before publishing a new beta." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + PUBLISHED_VERSIONS_JSON="$(npm view "${PACKAGE_NAME}" versions --json)" |
| 74 | + NEXT_BETA_NUMBER="$( |
| 75 | + PUBLISHED_VERSIONS_JSON="${PUBLISHED_VERSIONS_JSON}" BASE_VERSION="${BASE_VERSION}" node <<'NODE' |
| 76 | + const versions = JSON.parse(process.env.PUBLISHED_VERSIONS_JSON || '[]'); |
| 77 | + const baseVersion = process.env.BASE_VERSION; |
| 78 | + const escapedBaseVersion = baseVersion.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 79 | + const betaPattern = new RegExp(`^${escapedBaseVersion}-beta\\.(\\d+)$`); |
| 80 | + let maxBetaNumber = -1; |
| 81 | +
|
| 82 | + for (const version of versions) { |
| 83 | + const match = betaPattern.exec(version); |
| 84 | + if (match) { |
| 85 | + maxBetaNumber = Math.max(maxBetaNumber, Number(match[1])); |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + process.stdout.write(String(maxBetaNumber + 1)); |
| 90 | + NODE |
| 91 | + )" |
| 92 | + PACKAGE_VERSION="${BASE_VERSION}-beta.${NEXT_BETA_NUMBER}" |
| 93 | + elif [[ "${BRANCH_NAME}" == "master" ]]; then |
| 94 | + DIST_TAG="latest" |
| 95 | + PACKAGE_VERSION="${SOURCE_VERSION}" |
| 96 | + else |
| 97 | + echo "Unsupported release branch: ${BRANCH_NAME}" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | +
|
| 101 | + if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then |
| 102 | + echo "${PACKAGE_NAME}@${PACKAGE_VERSION} already exists on npm." |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +
|
| 106 | + PACKAGE_VERSION="${PACKAGE_VERSION}" node <<'NODE' |
| 107 | + const fs = require('fs'); |
| 108 | + const packagePath = './package.json'; |
| 109 | + const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8')); |
| 110 | + pkg.version = process.env.PACKAGE_VERSION; |
| 111 | + fs.writeFileSync(packagePath, `${JSON.stringify(pkg, null, 2)}\n`); |
| 112 | + NODE |
| 113 | +
|
| 114 | + echo "Prepared ${PACKAGE_NAME}@${PACKAGE_VERSION} for ${DIST_TAG} release." |
| 115 | + echo "package-name=${PACKAGE_NAME}" >> "${GITHUB_OUTPUT}" |
| 116 | + echo "package-version=${PACKAGE_VERSION}" >> "${GITHUB_OUTPUT}" |
| 117 | + echo "dist-tag=${DIST_TAG}" >> "${GITHUB_OUTPUT}" |
| 118 | +
|
| 119 | + - name: Test |
| 120 | + run: npm test -- --runInBand |
| 121 | + |
| 122 | + - name: Build |
| 123 | + run: npm run build |
| 124 | + |
| 125 | + - name: Type test |
| 126 | + run: npm run test:types |
| 127 | + |
| 128 | + - name: Publish beta package to npm |
| 129 | + if: github.ref_name == 'beta' |
| 130 | + run: npm publish --tag "${{ steps.release.outputs.dist-tag }}" |
| 131 | + |
| 132 | + - name: Stage publish package to npm |
| 133 | + if: github.ref_name == 'master' |
| 134 | + run: npm stage publish |
0 commit comments