Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Add GH workflow for publishing to npm. #43

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

junhaoliao
Copy link
Member

@junhaoliao junhaoliao commented Dec 22, 2024

Description

Changes made

Related: #44

  1. Add missing "package" step in package.json script release which is used for manual releases.
  2. Add GH workflow for publishing to npm.
  3. (TBD by repo maintainer) To add secrets.NPM_TOKEN into y-scope/clp-ffi-js. @kirkrodrigues steps documented at # Validation performed -> 2..

Making a release

With this automation workflow added, here's a proposed flow for making a release:

  1. npm version <major | minor | patch> -m "chore: Upgrade version for %s release."
    1. Submit a pull request to the "main" branch and wait for that to be merged.
  2. Go to https://github.com/y-scope/clp-ffi-js
  3. In the “Releases” panel, click “Create a new release”.
  4. Fill out the release information and publish the release.
    image
    image
    1. Click “Choose a tag” and fill the version number vA.B.C ; then click “Create new tag: vA.B.C on publish”.
    2. Fill the same version number in the “Release title” box.
    3. Fill out release details in the “Describe this release” box. The “Generate release notes” may be helpful.
    4. Check “Set as a pre-release” before publishing the release.
  5. Check the [“Actions”](https://github.com/y-scope/clp-ffi-js/actions) tab and wait for the publish workflow to finish.
  6. Test (npm install clp-ffi-js ) in a project. If good, edit the GitHub pre-release and “Update release”.

Validation performed

  1. Ran npm run release and observed
    1. The git diff check passed.
    2. The "package" task was run.
    3. npm package was run although the publish failed because the npm registry does not support overwriting an already published version:
      npm error code E403
      npm error 403 403 Forbidden - PUT https://registry.npmjs.org/clp-ffi-js - You cannot publish over the previously published versions: 0.3.1.
      npm error 403 In most cases, you or one of your dependencies are requesting
      npm error 403 a package version that is forbidden by your security policy, or
      npm error 403 on a server you do not have access to.
      npm error A complete log of this run can be found in: /home/junhao/.npm/_logs/2024-12-22T11_01_46_511Z-debug-0.log
      
  2. Tested the workflow in junhaoliao/clp-ffi-js
    1. Generated a "Granular Access Token" with one-day expiration at https://www.npmjs.com/settings/y-scope/tokens/granular-access-tokens/new
      1. Token name: GITHUB_CLP_FFI_JS_NPM_TOKEN.
      2. Packages and scopes -> Permissions -> Read and write -> Only select packages and scopes -> Select packages and scopes -> clp-ffi-js.
    2. Configured NPM_TOKEN with the generated npm token at https://github.com/junhaoliao/clp-ffi-js/settings/secrets/actions/new
    3. Made a release (also tested with a pre-release) following the steps documented above.
    4. Observed that:
      1. The workflow was triggered once the release / pre-release is published.
        1. In the workflow, the artifacts were built.
        2. The npm publish command was run although the publish failed because version v0.3.1 already exists.
        3. (By disabling the failed npm publish job), a branch was created with name as the version number.
      2. A tag was generated with the version number.

Summary by CodeRabbit

  • New Features
    • Introduced an automated workflow for publishing NPM packages upon release creation.
  • Changes
    • Updated the release process to include a packaging step before publishing the package.

Copy link

coderabbitai bot commented Dec 22, 2024

Walkthrough

This pull request introduces a new GitHub Actions workflow file publish-npm.yml to automate NPM package publishing and release management. The workflow is designed to trigger on release creation or manual dispatch, encompassing three primary jobs: build, publish-npm, and create-branch. These jobs handle package building, NPM publishing, and creating a new branch based on the release, streamlining the package release process with automated steps.

Changes

File Change Summary
.github/workflows/publish-npm.yml New GitHub Actions workflow for automated NPM package publishing and release management
package.json Updated release script to include task package before npm publish

Sequence Diagram

sequenceDiagram
    participant Release as Release Event
    participant Workflow as GitHub Actions
    participant Build as Build Job
    participant Publish as Publish NPM Job
    participant Branch as Create Branch Job

    Release->>Workflow: Trigger workflow
    Workflow->>Build: Execute build steps
    Build-->>Publish: Upload artifacts
    Publish->>Publish: Publish to NPM
    Publish-->>Branch: Trigger branch creation
    Branch->>Branch: Create and push new branch
Loading

Possibly Related PRs

Suggested Reviewers

  • davemarco
  • kirkrodrigues

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2a75075 and 5c41d16.

📒 Files selected for processing (1)
  • .github/workflows/publish-npm.yml (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • .github/workflows/publish-npm.yml

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@junhaoliao junhaoliao marked this pull request as ready for review December 22, 2024 12:22
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (4)
package.json (1)

16-16: Consider enhancing the release script with additional safeguards

The current release script could benefit from additional checks to ensure reliable publishing:

-    "release": "git diff --exit-code && task package && npm publish"
+    "release": "npm version check && git diff --exit-code HEAD && task package && npm pack --dry-run && npm publish"

This modification:

  • Validates the package version
  • Ensures clean working directory (comparing with HEAD)
  • Performs a dry-run package check before publishing
.github/workflows/publish-npm.yml (3)

18-22: Implement caching for task dependencies

Add caching for the task CLI to improve workflow performance.

+      - uses: actions/cache@v3
+        with:
+          path: ~/.npm
+          key: ${{ runner.os }}-task-${{ hashFiles('**/package-lock.json') }}
+          restore-keys: |
+            ${{ runner.os }}-task-
+
       - name: "Install task"
         run: "npm install -g @go-task/cli"

61-64: Add error handling for branch creation

The branch creation step should handle potential failures gracefully.

       - name: "Create and push branch"
         run: |
+          if git show-ref --quiet refs/heads/"${{github.ref_name}}"; then
+            echo "Branch already exists, skipping creation"
+            exit 0
+          fi
           git checkout -b "${{github.ref_name}}"
           git push origin "refs/heads/${{github.ref_name}}"

3-6: Consider adding version tag filtering

The workflow triggers on all releases. Consider filtering for specific version patterns.

 on:
   release:
     types: ["created"]
+    tags:
+      - 'v[0-9]+.[0-9]+.[0-9]+'
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 32b1220 and 2a75075.

📒 Files selected for processing (2)
  • .github/workflows/publish-npm.yml (1 hunks)
  • package.json (1 hunks)
🧰 Additional context used
🪛 GitHub Check: lint (macos-latest)
.github/workflows/publish-npm.yml

[failure] 50-50:
50:17 [quoted-strings] string value is not quoted with double quotes

🔇 Additional comments (1)
.github/workflows/publish-npm.yml (1)

8-8: Define explicit permissions for workflow jobs

The root-level permissions block is empty. Consider defining explicit permissions to follow the principle of least privilege.

[security]

-permissions: {}
+permissions:
+  contents: read
+  packages: read

Comment on lines +36 to +39
- uses: "actions/setup-node@v4"
with:
node-version: 22
registry-url: "https://registry.npmjs.org/"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using LTS version of Node.js

Node.js 22 is currently in development. For production publishing workflows, it's recommended to use an LTS version.

       with:
-          node-version: 22
+          node-version: 20
           registry-url: "https://registry.npmjs.org/"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- uses: "actions/setup-node@v4"
with:
node-version: 22
registry-url: "https://registry.npmjs.org/"
- uses: "actions/setup-node@v4"
with:
node-version: 20
registry-url: "https://registry.npmjs.org/"

Comment on lines +41 to +43
- run: "npm publish"
env:
NODE_AUTH_TOKEN: "${{secrets.NPM_TOKEN}}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add package validation steps before publishing

Consider adding validation steps before publishing to ensure package integrity.

+      - name: "Validate package"
+        run: |
+          npm pack --dry-run
+          npm audit
+
       - run: "npm publish"
         env:
           NODE_AUTH_TOKEN: "${{secrets.NPM_TOKEN}}"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- run: "npm publish"
env:
NODE_AUTH_TOKEN: "${{secrets.NPM_TOKEN}}"
- name: "Validate package"
run: |
npm pack --dry-run
npm audit
- run: "npm publish"
env:
NODE_AUTH_TOKEN: "${{secrets.NPM_TOKEN}}"

@junhaoliao junhaoliao changed the title ci: Add GH workflow for publishing to npm. ci: Add GH workflow for publishing to npm (fixes #44). Dec 22, 2024
@junhaoliao junhaoliao changed the title ci: Add GH workflow for publishing to npm (fixes #44). ci: Add GH workflow for publishing to npm. Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant