-
Notifications
You must be signed in to change notification settings - Fork 78
Add GitHub Actions workflow for npm package publishing #15
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||||||||||||||||||||||||
| # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created | ||||||||||||||||||||||||||||||||
| # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| name: Node.js Package | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| on: | ||||||||||||||||||||||||||||||||
| release: | ||||||||||||||||||||||||||||||||
| types: [created] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| jobs: | ||||||||||||||||||||||||||||||||
| build: | ||||||||||||||||||||||||||||||||
| runs-on: ubuntu-latest | ||||||||||||||||||||||||||||||||
| steps: | ||||||||||||||||||||||||||||||||
| - uses: actions/checkout@v4 | ||||||||||||||||||||||||||||||||
| - uses: actions/setup-node@v4 | ||||||||||||||||||||||||||||||||
| with: | ||||||||||||||||||||||||||||||||
| node-version: 20 | ||||||||||||||||||||||||||||||||
| - run: npm ci | ||||||||||||||||||||||||||||||||
| - run: npm test | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| - run: npm test |
Copilot
AI
Nov 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow runs npm ci and npm test at the repository root, but this repository appears to be a monorepo with multiple example projects in subdirectories (e.g., github-actions/, nodejs/, express/, etc.), and there's no package.json file at the root level.
This will cause the workflow to fail with an error when trying to run npm ci because npm cannot find a package.json file.
Consider one of the following solutions:
- If you want to publish a specific package, add a
working-directoryto the workflow steps (e.g.,working-directory: ./github-actions) - If you want to publish the root as a package, add a
package.jsonfile at the repository root - If you want to publish multiple packages, consider using a matrix strategy to publish each package separately
Copilot
AI
Nov 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding npm dependency caching here as well to speed up the publish job. Add the cache: 'npm' parameter to the actions/setup-node step:
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://npm.pkg.github.com/
cache: 'npm'This will reuse the cached dependencies and reduce workflow execution time.
| registry-url: https://npm.pkg.github.com/ | |
| registry-url: https://npm.pkg.github.com/ | |
| cache: 'npm' |
Copilot
AI
Nov 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The npm publish step will fail without proper package.json configuration for GitHub Packages. For successful publishing to GitHub Packages, the package.json file must include:
- A scoped package name in the format
@owner/package-name(e.g.,"name": "@your-username/your-package") - A
repositoryfield pointing to this GitHub repository - Optionally, a
publishConfigsection specifying the registry:"publishConfig": { "registry": "https://npm.pkg.github.com" }
Without these configurations, the publish step will either fail or attempt to publish to the public npm registry instead of GitHub Packages.
| - run: npm ci | |
| - run: npm ci | |
| - name: Ensure package.json is configured for GitHub Packages | |
| run: | | |
| OWNER=$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f1) | |
| REPO=$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f2) | |
| PACKAGE_NAME=$(jq -r '.name' package.json) | |
| # Ensure scoped package name | |
| if [[ "$PACKAGE_NAME" != "@$OWNER/"* ]]; then | |
| jq --arg owner "$OWNER" --arg name "$PACKAGE_NAME" '.name = "@" + $owner + "/" + $name' package.json > package.tmp.json && mv package.tmp.json package.json | |
| fi | |
| # Ensure repository field | |
| jq --arg repo "github:${GITHUB_REPOSITORY}" '.repository = $repo' package.json > package.tmp.json && mv package.tmp.json package.json | |
| # Ensure publishConfig.registry | |
| jq '.publishConfig.registry = "https://npm.pkg.github.com"' package.json > package.tmp.json && mv package.tmp.json package.json |
Copilot
AI
Nov 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding the --access public flag to the npm publish command if you intend to make this package publicly accessible. By default, scoped packages (required for GitHub Packages) are published as private.
If the package should be publicly accessible, update the command to:
- run: npm publish --access publicNote: This assumes the package.json has a scoped name. Without specifying access level, the publish may fail or create a private package depending on your GitHub organization settings.
| - run: npm publish | |
| - run: npm publish --access public |
Copilot
AI
Nov 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] There's a spacing issue in the secret reference. The expression ${{secrets.GITHUB_TOKEN}} should have spaces around the braces for better readability and consistency with GitHub Actions best practices.
Update to:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}While the current syntax will work, adding spaces is the recommended style in GitHub Actions documentation.
| NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using cache for npm dependencies to speed up the workflow. Both the
buildandpublish-gprjobs runnpm ci, which re-downloads all dependencies each time. You can improve workflow performance by caching the npm dependencies.Add caching to the
actions/setup-nodestep:This will cache the npm dependencies and significantly reduce workflow execution time, especially for projects with many dependencies.