Skip to content

Commit 3c55445

Browse files
authored
Merge npm publishing jobs into a single file (Trusted Publishing - OICD) (#2999)
## Summary: This PR co-locates the two jobs that publish to npm (release and snapshot). This is a necessity for enabling Trusted Publishing (https://docs.npmjs.com/trusted-publishers#how-trusted-publishing-works). Once this PR has landed, we'll be able to go into each npm package's settings on npmjs.com and enable Trusted Publishing. Once that's enabled, we no longer need a npm token (stored in `NPM_TOKEN`) in this repo at all and will no longer have to manage an auth token for publishing to npm. Issue: LEMS-3681 ## Test plan: 😅 This will be tricky. * I plan to land this PR and then enable trusted publishing for all perseus packages. * Once that's enabled, I'll try a snapshot publish by invoking it manually using workflow_dispatch * If that succeeds, I'll create a tiny release that touches _all_ packages (I'll update/add a comment in each package's code) and then cut a release to test the release flow works also. * Finally, I'll work through the workflow files and remove references to `NPM_TOKEN` and remove that secret from this repo. Author: jeremywiebe Reviewers: jeremywiebe, handeyeco, somewhatabstract, jandrade, nishasy, mark-fitzgerald, ivyolamit, Myranae, catandthemachines Required Reviewers: Approved By: handeyeco, somewhatabstract Checks: ✅ 10 checks were successful, ⏭️ 1 check has been skipped Pull Request URL: #2999
1 parent 8859e97 commit 3c55445

File tree

4 files changed

+277
-242
lines changed

4 files changed

+277
-242
lines changed
File renamed without changes.

.github/workflows/node-ci.yml

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -284,149 +284,3 @@ jobs:
284284
path-to-artifact: packages/perseus-core/dist/es/index.item-splitting.js
285285
label-name: item-splitting-change
286286
comment-title: 🛠️ Item Splitting
287-
288-
publish_snapshot:
289-
name: Publish npm snapshot
290-
# We don't publish snapshots on Changeset "Version Packages" PRs
291-
if: |
292-
!startsWith(github.head_ref, 'changeset-release/')
293-
runs-on: ${{ matrix.os }}
294-
permissions:
295-
id-token: write # required for publishing to npm with provenance
296-
pull-requests: write # required because we write a comment on the PR
297-
strategy:
298-
matrix:
299-
os: [ubuntu-latest]
300-
node-version: [20.x]
301-
steps:
302-
# We need to checkout all history, so that the changeseat tool can diff it
303-
- name: Checkout current commit
304-
uses: actions/checkout@v4
305-
with:
306-
fetch-depth: "0"
307-
308-
- name: Ensure main branch is available
309-
run: |
310-
REF=$(git rev-parse HEAD)
311-
git checkout main
312-
git checkout $REF
313-
314-
# Helper to get the URL of the current run, if we need it.
315-
- name: Get workflow run URL
316-
id: get-run-url
317-
run: echo "run_url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_OUTPUT
318-
319-
# We need to see if any releases are in progress.
320-
# We do not want to try and publish anything if a publish is
321-
# pending. We fail here, but we make sure to update the
322-
# PR comment later. This has to come after the checkout.
323-
- name: Check for release
324-
id: check-release
325-
env:
326-
GH_TOKEN: ${{ github.token }}
327-
run: |
328-
# Releases are triggered by merging "Version Packages" PRs.
329-
# So we look for instances of the release.yml workflow, with
330-
# a title containing "Version Packages", that are in progress.
331-
release_count=$(gh run list --workflow release.yml --json status,displayTitle --jq '[.[] | select(.status == "in_progress" and ((.displayTitle | contains("Version Packages")) or (.displayTitle | contains("RELEASING:"))))] | length')
332-
echo "release_count=$release_count" >> $GITHUB_OUTPUT
333-
if [ "$release_count" -ne 0 ]; then
334-
echo "Error: There are $release_count releases in progress."
335-
exit 1
336-
else
337-
echo "No releases in progress."
338-
fi
339-
340-
- name: Install & cache node_modules
341-
uses: ./.github/actions/shared-node-cache
342-
with:
343-
node-version: ${{ matrix.node-version }}
344-
345-
- name: Publish Snapshot Release to npm
346-
id: publish-snapshot
347-
run: ./utils/publish-snapshot.sh # All config is via Github env vars
348-
env:
349-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
350-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
351-
352-
- name: Calculate short SHA for this commit
353-
id: short-sha
354-
# Why not GITHUB_SHA here? Because that is the last merge-commit
355-
# for the PR (ie. the ephemeral commit that Github creates for
356-
# each PR merging the base branch into the pull request HEAD) for
357-
# Github Action runs). We want to reference the commit that was
358-
# pushed, not this ephemeral commit.
359-
run: echo "short_sha=$(echo ${{ github.event.pull_request.head.sha }} | cut -c1-8)" >> $GITHUB_OUTPUT
360-
361-
# Note: these two actions are locked to the latest version that were
362-
# published when I created this yml file (just for security).
363-
- name: Find existing comment
364-
# Even if we're failing, we want to update the comments.
365-
if: always()
366-
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e
367-
id: find-comment
368-
with:
369-
issue-number: ${{ github.event.pull_request.number }}
370-
comment-author: "github-actions[bot]"
371-
body-includes: "npm Snapshot:"
372-
373-
- name: Create or update npm snapshot comment - success
374-
if: steps.publish-snapshot.outputs.npm_snapshot_tag != ''
375-
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043
376-
with:
377-
issue-number: ${{ github.event.pull_request.number }}
378-
comment-id: ${{ steps.find-comment.outputs.comment-id }}
379-
edit-mode: replace
380-
body: |
381-
# npm Snapshot: Published
382-
383-
Good news!! We've packaged up the latest commit from this PR (${{
384-
steps.short-sha.outputs.short_sha }}) and published it to npm. You
385-
can install it using the tag `${{
386-
steps.publish-snapshot.outputs.npm_snapshot_tag }}`.
387-
388-
Example:
389-
```sh
390-
pnpm add @khanacademy/perseus@${{
391-
steps.publish-snapshot.outputs.npm_snapshot_tag }}
392-
```
393-
394-
If you are working in Khan Academy's frontend, you can run the below command.
395-
```sh
396-
./dev/tools/bump_perseus_version.ts -t PR${{ github.event.pull_request.number }}
397-
```
398-
399-
If you are working in Khan Academy's webapp, you can run the below command.
400-
```sh
401-
./dev/tools/bump_perseus_version.js -t PR${{ github.event.pull_request.number }}
402-
```
403-
404-
- name: Create or update npm snapshot comment - failure, snapshot publish failed
405-
if: steps.publish-snapshot.outputs.npm_snapshot_tag == ''
406-
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043
407-
with:
408-
issue-number: ${{ github.event.pull_request.number }}
409-
comment-id: ${{ steps.find-comment.outputs.comment-id }}
410-
edit-mode: replace
411-
body: |
412-
# npm Snapshot: **NOT** Published
413-
414-
Oh noes!! We couldn't find any changesets in this PR (${{
415-
steps.short-sha.outputs.short_sha }}). As a result, we did not
416-
publish an npm snapshot for you.
417-
418-
- name: Create or update npm snapshot comment - failure, concurrent with release
419-
if: failure() && steps.check-release.outputs.release_count != '0'
420-
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043
421-
with:
422-
issue-number: ${{ github.event.pull_request.number }}
423-
comment-id: ${{ steps.find-comment.outputs.comment-id }}
424-
edit-mode: replace
425-
body: |
426-
# npm Snapshot: **NOT** Published
427-
428-
Oh noes!! We couldn't publish an npm snapshot for you because
429-
there is a release in progress. Please wait for the release to
430-
finish, then retry this workflow.
431-
432-
[View the workflow run](${{ steps.get-run-url.outputs.run_url }})

0 commit comments

Comments
 (0)