|
| 1 | +name: NPM Publish Package |
| 2 | +description: Publish a package to npm with retry and verification logic |
| 3 | + |
| 4 | +inputs: |
| 5 | + registry-url: |
| 6 | + description: npm registry URL |
| 7 | + required: false |
| 8 | + default: "https://registry.npmjs.org/" |
| 9 | + package-dir: |
| 10 | + description: Directory containing package.json to publish |
| 11 | + required: true |
| 12 | + verify-attempts: |
| 13 | + description: Number of verification attempts |
| 14 | + required: false |
| 15 | + default: "90" |
| 16 | + verify-delay: |
| 17 | + description: Delay between verification attempts in seconds |
| 18 | + required: false |
| 19 | + default: "10" |
| 20 | + |
| 21 | +runs: |
| 22 | + using: composite |
| 23 | + steps: |
| 24 | + - name: Publish to npm |
| 25 | + shell: bash |
| 26 | + env: |
| 27 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 28 | + REGISTRY_URL: ${{ inputs.registry-url }} |
| 29 | + PACKAGE_DIR: ${{ inputs.package-dir }} |
| 30 | + VERIFY_ATTEMPTS: ${{ inputs.verify-attempts }} |
| 31 | + VERIFY_DELAY: ${{ inputs.verify-delay }} |
| 32 | + run: | |
| 33 | + set -euo pipefail |
| 34 | +
|
| 35 | + package_name=$(jq -r '.name' "${PACKAGE_DIR}/package.json") |
| 36 | + package_version=$(jq -r '.version' "${PACKAGE_DIR}/package.json") |
| 37 | +
|
| 38 | + registry_version_exists() { |
| 39 | + local encoded_package_name |
| 40 | + local version_json |
| 41 | + local published_version |
| 42 | +
|
| 43 | + encoded_package_name=$(node -e 'process.stdout.write(encodeURIComponent(process.argv[1]))' "$package_name") |
| 44 | + version_json=$(curl --silent --show-error --fail "${REGISTRY_URL%/}/${encoded_package_name}/${package_version}" 2>/dev/null || true) |
| 45 | +
|
| 46 | + if [[ -z "$version_json" ]]; then |
| 47 | + return 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + published_version=$(jq -r '.version // empty' <<<"$version_json") |
| 51 | + [[ "$published_version" == "$package_version" ]] |
| 52 | + } |
| 53 | +
|
| 54 | + version_exists() { |
| 55 | + local published_version |
| 56 | + published_version=$(npm view "${package_name}@${package_version}" version --registry "$REGISTRY_URL" 2>/dev/null || true) |
| 57 | +
|
| 58 | + if [[ "$published_version" == "$package_version" ]]; then |
| 59 | + return 0 |
| 60 | + fi |
| 61 | +
|
| 62 | + registry_version_exists |
| 63 | + } |
| 64 | +
|
| 65 | + verify_version_exists() { |
| 66 | + local attempts="$VERIFY_ATTEMPTS" |
| 67 | + local delay_seconds="$VERIFY_DELAY" |
| 68 | +
|
| 69 | + for attempt in $(seq 1 "$attempts"); do |
| 70 | + if version_exists; then |
| 71 | + echo "Verified ${package_name}@${package_version} on npm" |
| 72 | + return 0 |
| 73 | + fi |
| 74 | +
|
| 75 | + if [[ "$attempt" -eq "$attempts" ]]; then |
| 76 | + break |
| 77 | + fi |
| 78 | +
|
| 79 | + echo "Waiting for ${package_name}@${package_version} to appear on npm (${attempt}/${attempts})..." |
| 80 | + sleep "$delay_seconds" |
| 81 | + done |
| 82 | +
|
| 83 | + echo "::error::${package_name}@${package_version} is still missing from npm after publish." |
| 84 | + return 1 |
| 85 | + } |
| 86 | +
|
| 87 | + if version_exists; then |
| 88 | + echo "${package_name}@${package_version} already exists on npm, skipping" |
| 89 | + exit 0 |
| 90 | + fi |
| 91 | +
|
| 92 | + publish_log=$(mktemp) |
| 93 | +
|
| 94 | + if (cd "$PACKAGE_DIR" && pnpm publish --access public --no-git-checks) 2>&1 | tee "$publish_log"; then |
| 95 | + verify_version_exists |
| 96 | + rm -f "$publish_log" |
| 97 | + exit 0 |
| 98 | + fi |
| 99 | +
|
| 100 | + if grep -Eiq 'cannot publish over the previously published versions|previously published versions' "$publish_log"; then |
| 101 | + echo "${package_name}@${package_version} was already published according to npm, skipping" |
| 102 | + rm -f "$publish_log" |
| 103 | + exit 0 |
| 104 | + fi |
| 105 | +
|
| 106 | + if version_exists; then |
| 107 | + echo "${package_name}@${package_version} already exists on npm after publish attempt, skipping" |
| 108 | + rm -f "$publish_log" |
| 109 | + exit 0 |
| 110 | + fi |
| 111 | +
|
| 112 | + echo "::error::Failed to publish ${package_name}@${package_version}. Exact version is still missing from npm." |
| 113 | + rm -f "$publish_log" |
| 114 | + exit 1 |
0 commit comments