diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7905fd4..bce18d5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,8 @@ name: Release # # Required repo secrets (Settings -> Secrets and variables -> Actions): # PYPI_API_TOKEN - PyPI account -> API tokens -# NPM_TOKEN - npmjs.com account -> Access Tokens -> Automation +# NPM_PROD_TOKEN - npmjs.com account -> Access Tokens -> Automation +# (NPM_TOKEN is still read as a fallback) # GITHUB_TOKEN (GHCR push) is provided automatically, no setup needed. on: @@ -21,6 +22,10 @@ on: tag: description: "Tag to release (e.g. v0.3.0) -- must already exist and match src/secfoo/__about__.py" required: true + npm-only: + description: "Only publish npm, from the binaries already attached to this tag's GitHub Release" + type: boolean + default: false # Read-only by default; the two jobs that need to write (the GitHub # Release, the GHCR push) grant themselves exactly that permission. @@ -56,6 +61,7 @@ jobs: build-binaries: name: Build binary (${{ matrix.name }}) needs: check-version + if: ${{ !inputs.npm-only }} strategy: fail-fast: false matrix: @@ -101,6 +107,7 @@ jobs: release: name: Create GitHub Release needs: build-binaries + if: ${{ !inputs.npm-only }} runs-on: ubuntu-latest permissions: contents: write @@ -108,6 +115,8 @@ jobs: - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: path: artifacts + # Only the binaries: the Docker job also uploads a build-record artifact. + pattern: secfoo-* - name: Flatten artifacts and compute checksums run: | mkdir -p release @@ -122,6 +131,7 @@ jobs: publish-pypi: name: Publish to PyPI needs: [check-version] + if: ${{ !inputs.npm-only }} runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -142,22 +152,51 @@ jobs: publish-npm: name: Publish npm packages needs: [check-version, release] + # Runs after a fresh GitHub Release, or alone with npm-only for a tag + # whose release already exists. + if: ${{ !cancelled() && needs.check-version.result == 'success' && (needs.release.result == 'success' || inputs.npm-only) }} runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: - path: artifacts + ref: ${{ inputs.tag }} + - name: Download the release binaries + # From the GitHub Release itself, so npm ships exactly the files (and + # SHA256SUMS) users download there, and an npm-only run needs no rebuild. + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ inputs.tag }} + run: gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --pattern 'secfoo-*' --pattern SHA256SUMS --dir release-bin + - name: Verify checksums + working-directory: release-bin + run: grep -E ' secfoo-(darwin|linux|win32)-' SHA256SUMS | sha256sum -c - - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: "20" registry-url: "https://registry.npmjs.org" - name: Populate platform binaries and publish all 6 packages env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.NPM_PROD_TOKEN || secrets.NPM_TOKEN }} VERSION: ${{ needs.check-version.outputs.version }} run: | set -euo pipefail + if [ -z "${NODE_AUTH_TOKEN:-}" ]; then + echo "::error::NPM_PROD_TOKEN secret is not set (Settings -> Secrets and variables -> Actions)." + exit 1 + fi + # A version can't be re-published; skip any package already at it, + # so a re-run after a partial publish finishes the rest. + publish() { + local dir="$1" name + name=$(node -p "require('./$dir/package.json').name") + npm version "$VERSION" --no-git-tag-version --prefix "$dir" --allow-same-version + if npm view "$name@$VERSION" version >/dev/null 2>&1; then + echo "$name@$VERSION is already on npm, skipping" + return + fi + # "./" makes it a folder: "npm/secfoo-x" alone is read as a GitHub user/repo. + npm publish --access public "./$dir" + } declare -A DIRS=( [darwin-arm64]=npm/secfoo-darwin-arm64 [darwin-x64]=npm/secfoo-darwin-x64 @@ -169,20 +208,26 @@ jobs: pkgdir="${DIRS[$plat]}" mkdir -p "$pkgdir/bin" if [ "$plat" = "win32-x64" ]; then - cp "artifacts/secfoo-$plat/secfoo-$plat.exe" "$pkgdir/bin/secfoo.exe" + cp "release-bin/secfoo-$plat.exe" "$pkgdir/bin/secfoo.exe" else - cp "artifacts/secfoo-$plat/secfoo-$plat" "$pkgdir/bin/secfoo" + cp "release-bin/secfoo-$plat" "$pkgdir/bin/secfoo" chmod +x "$pkgdir/bin/secfoo" fi - npm version "$VERSION" --no-git-tag-version --prefix "$pkgdir" --allow-same-version - npm publish --access public "$pkgdir" + publish "$pkgdir" done - npm version "$VERSION" --no-git-tag-version --prefix npm/secfoo --allow-same-version - npm publish --access public npm/secfoo + # The meta-package last: its optionalDependencies must already exist, + # and must point at this version (package.json keeps a placeholder). + node -e ' + const fs = require("fs"), f = "./npm/secfoo/package.json", p = require(f); + for (const k of Object.keys(p.optionalDependencies)) p.optionalDependencies[k] = process.env.VERSION; + fs.writeFileSync(f, JSON.stringify(p, null, 2) + "\n"); + ' + publish npm/secfoo publish-docker: name: Publish Docker image (GHCR) needs: [check-version, publish-pypi] + if: ${{ !inputs.npm-only }} runs-on: ubuntu-latest permissions: contents: read