name: Publish alpha npm packages on: workflow_dispatch: inputs: release_tag: description: Published alpha GitHub Release tag (vX.Y.Z-alpha.N) required: false type: string source_commit: description: Repair an already published main SHA instead of publishing packages required: false type: string publish: description: Publish or repair nightly tags with npm OIDC default: false type: boolean workflow_call: inputs: source_commit: required: true type: string publish: required: true type: boolean permissions: contents: read concurrency: group: npm-publish cancel-in-progress: false jobs: verify: if: github.repository == 'arcships/dim' && github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 15 outputs: sha: ${{ steps.release.outputs.sha }} id: ${{ steps.release.outputs.id }} repair: ${{ steps.release.outputs.repair }} steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: persist-credentials: false fetch-depth: 0 - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version-file: .node-version package-manager-cache: false - name: Download and verify the original alpha packages id: release env: GH_TOKEN: ${{ github.token }} GH_REPO: ${{ github.repository }} RELEASE_TAG: ${{ inputs.release_tag }} SOURCE_COMMIT: ${{ inputs.source_commit }} shell: bash run: | set -euo pipefail if [[ -n "$SOURCE_COMMIT" ]]; then [[ -z "$RELEASE_TAG" && "$SOURCE_COMMIT" == "$GITHUB_SHA" ]] jq -n --arg sourceCommit "$SOURCE_COMMIT" '{sourceCommit: $sourceCommit}' > nightly-source.json node scripts/check-alpha-registry.mjs nightly-source.json source jq -e '.version != null' nightly-source.json >/dev/null { echo "sha=$SOURCE_COMMIT" echo 'repair=true' } >> "$GITHUB_OUTPUT" exit 0 fi [[ "$RELEASE_TAG" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)-alpha\.(0|[1-9][0-9]*)$ ]] mkdir -p staging gh api "repos/$GITHUB_REPOSITORY/releases/tags/$RELEASE_TAG" > staging/github-release.json jq -e --arg tag "$RELEASE_TAG" '.draft == false and .prerelease == true and .published_at != null and .tag_name == $tag' staging/github-release.json > /dev/null source_sha=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs%2Ftags%2F$RELEASE_TAG" --jq .sha) [[ "$source_sha" =~ ^[a-f0-9]{40}$ ]] git merge-base --is-ancestor "$source_sha" origin/main { echo "sha=$source_sha" echo "id=$(jq -er .id staging/github-release.json)" } >> "$GITHUB_OUTPUT" gh release download "$RELEASE_TAG" --dir staging/npm --pattern '*.tgz' --pattern release-manifest.json node scripts/publish-npm.mjs staging/npm "${RELEASE_TAG#v}" "$source_sha" --check-release staging/github-release.json - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 if: steps.release.outputs.repair == 'true' with: name: nightly-source path: nightly-source.json if-no-files-found: error retention-days: 1 - name: Report publication status env: PUBLISH_REQUESTED: ${{ inputs.publish }} PUBLISH_ENABLED: ${{ vars.NPM_ALPHA_PUBLISH_ENABLED }} shell: bash run: | if [[ "$PUBLISH_REQUESTED" != true || "$PUBLISH_ENABLED" != true ]]; then echo 'Validated only. npm publication requires publish=true and NPM_ALPHA_PUBLISH_ENABLED=true.' >> "$GITHUB_STEP_SUMMARY" fi if [[ "$PUBLISH_REQUESTED" == true && "$PUBLISH_ENABLED" != true ]]; then echo 'npm publication is disabled: configure the trusted publishers before enabling NPM_ALPHA_PUBLISH_ENABLED.' >&2 exit 1 fi publish: needs: verify if: inputs.publish && vars.NPM_ALPHA_PUBLISH_ENABLED == 'true' && github.repository == 'arcships/dim' && github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 40 environment: npm-release permissions: contents: read id-token: write steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: persist-credentials: false - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version-file: .node-version registry-url: https://registry.npmjs.org package-manager-cache: false - name: Install npm with trusted dist-tag support run: npm install --global npm@11.21.0 --ignore-scripts --registry https://registry.npmjs.org - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 if: needs.verify.outputs.repair == 'true' with: name: nightly-source digest-mismatch: error - name: Recheck the Release and publish dependencies before dimcode-alpha if: needs.verify.outputs.repair != 'true' env: GH_TOKEN: ${{ github.token }} GH_REPO: ${{ github.repository }} RELEASE_ID: ${{ needs.verify.outputs.id }} RELEASE_TAG: ${{ inputs.release_tag }} SOURCE_SHA: ${{ needs.verify.outputs.sha }} NPM_CONFIG_REGISTRY: https://registry.npmjs.org shell: bash run: | set -euo pipefail [[ "$RELEASE_TAG" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)-alpha\.(0|[1-9][0-9]*)$ ]] mkdir -p staging gh api "repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID" > staging/github-release.json jq -e '.prerelease == true' staging/github-release.json > /dev/null [[ "$(gh api "repos/$GITHUB_REPOSITORY/commits/refs%2Ftags%2F$RELEASE_TAG" --jq .sha)" == "$SOURCE_SHA" ]] gh release download "$RELEASE_TAG" --dir staging/npm --pattern '*.tgz' --pattern release-manifest.json cp staging/npm/release-manifest.json npm-publication.json node scripts/check-alpha-registry.mjs npm-publication.json snapshot node scripts/publish-npm.mjs staging/npm "${RELEASE_TAG#v}" "$SOURCE_SHA" --publish staging/github-release.json - name: Record published Release provenance if: needs.verify.outputs.repair != 'true' env: RELEASE_TAG: ${{ inputs.release_tag }} shell: bash run: | node --input-type=module <<'NODE' import { readFileSync, writeFileSync } from 'node:fs'; const release = JSON.parse(readFileSync('staging/github-release.json')); const snapshot = JSON.parse(readFileSync('npm-publication.json')); writeFileSync('npm-publication.json', JSON.stringify({ ...snapshot, releaseId: release.id, releaseTag: process.env.RELEASE_TAG })); NODE - name: Reuse the complete published nightly without rebuilding if: needs.verify.outputs.repair == 'true' run: cp nightly-source.json npm-publication.json - name: Verify provenance and align the nightly latest and alpha tags run: node scripts/check-alpha-registry.mjs npm-publication.json promote --apply - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: npm-publication overwrite: true path: npm-publication.json if-no-files-found: error retention-days: 1 acceptance: needs: [verify, publish] runs-on: ubuntu-latest timeout-minutes: 50 steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: persist-credentials: false - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version-file: .node-version package-manager-cache: false - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: name: npm-publication digest-mismatch: error - name: Verify npm tags, protected tags and both fresh dima installations run: node scripts/check-alpha-registry.mjs npm-publication.json npm - name: Sync the Alibaba mirror and verify both fresh dima installations run: node scripts/check-alpha-registry.mjs npm-publication.json mirror --apply - name: Record acceptance env: REPAIR: ${{ needs.verify.outputs.repair }} run: | jq -r '"Verified nightly \(.version) from \(.sourceCommit). npm and npmmirror latest/alpha agree; default and @alpha installations both passed package-version and dima --version checks with fresh prefixes and caches. All protected dist-tags are unchanged."' npm-publication.json >> "$GITHUB_STEP_SUMMARY" if [[ "$REPAIR" == true ]]; then echo 'No build is needed for this already published source.' >> "$GITHUB_STEP_SUMMARY" fi