-
Notifications
You must be signed in to change notification settings - Fork 28
feat: implement automated NPM publishing with GitHub Actions #2119
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: develop
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,14 @@ | ||
--- | ||
'@ag.ds-next/react': patch | ||
--- | ||
|
||
feat: add automated NPM publishing with GitHub Actions | ||
|
||
- Implement test-and-publish workflow with changesets integration | ||
- Add automated Release PR creation from develop branch | ||
- Configure secure permissions and NPM authentication | ||
- Optimize CI/CD with caching and parallel testing | ||
- Replace manual versioning with changeset-driven releases | ||
- Add prepublishOnly scripts to prevent manual publishing | ||
|
||
Requires: NPM_TOKEN secret for publishing to @ag.ds-next registry |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,103 @@ | ||||
name: Test & Publish | ||||
|
||||
# Explicit permissions for security | ||||
permissions: | ||||
contents: read | ||||
pull-requests: write | ||||
id-token: write | ||||
|
||||
on: | ||||
push: | ||||
branches: | ||||
- main | ||||
pull_request: | ||||
workflow_dispatch: | ||||
|
||||
jobs: | ||||
test-and-build: | ||||
name: Test, Lint & Build | ||||
runs-on: ubuntu-latest | ||||
steps: | ||||
- name: Checkout | ||||
uses: actions/checkout@v4 | ||||
|
||||
- name: Setup Node.js 22.15.1 | ||||
uses: actions/setup-node@v4 | ||||
with: | ||||
node-version: 22.15.1 | ||||
|
||||
- name: Get number of CPU cores | ||||
id: cpu-cores | ||||
uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2.0.0 | ||||
|
||||
- name: Get yarn cache directory path | ||||
id: yarn-cache-dir-path | ||||
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT | ||||
|
||||
- uses: actions/cache@v3 | ||||
id: yarn-cache | ||||
with: | ||||
path: | | ||||
${{ steps.yarn-cache-dir-path.outputs.dir }} | ||||
node_modules | ||||
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }} | ||||
|
||||
- name: Install dependencies | ||||
run: yarn install --frozen-lockfile | ||||
|
||||
- name: Unit tests | ||||
run: yarn test --max-workers ${{ steps.cpu-cores.outputs.count }} | ||||
|
||||
- name: Generate component props | ||||
run: yarn docs:generate-component-props | ||||
|
||||
- name: Lint | ||||
run: yarn lint | ||||
|
||||
- name: Build packages | ||||
run: yarn build | ||||
|
||||
publish: | ||||
name: Publish to NPM | ||||
runs-on: ubuntu-latest | ||||
needs: [test-and-build] | ||||
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | ||||
permissions: | ||||
contents: write # Needed to create releases and tags | ||||
pull-requests: write # Needed to create Release PRs | ||||
id-token: write # Needed for NPM provenance | ||||
steps: | ||||
- name: Checkout Repository | ||||
uses: actions/checkout@v4 | ||||
with: | ||||
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits | ||||
fetch-depth: 0 | ||||
|
||||
- name: Setup Node.js | ||||
uses: actions/setup-node@v4 | ||||
with: | ||||
node-version: 22.15.1 | ||||
cache: 'yarn' | ||||
registry-url: 'https://registry.npmjs.org' | ||||
|
||||
- name: Install Dependencies | ||||
run: yarn install --frozen-lockfile | ||||
|
||||
- name: Configure npm for public publishing | ||||
run: | | ||||
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc | ||||
echo "@ag.ds-next:registry=https://registry.npmjs.org" >> ~/.npmrc | ||||
echo "access=public" >> ~/.npmrc | ||||
env: | ||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||||
|
||||
- name: Publish to NPM | ||||
run: | | ||||
echo "Publishing packages to NPM registry..." | ||||
yarn publish-changed | ||||
env: | ||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both NPM_TOKEN and NODE_AUTH_TOKEN are set to the same secret value, which creates redundancy. Since NODE_AUTH_TOKEN is the standard environment variable used by actions/setup-node for NPM authentication, you can remove NPM_TOKEN and rely solely on NODE_AUTH_TOKEN.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||||
|
||||
- name: Display notification if a publish happens | ||||
run: echo "Packages published to NPM successfully!" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,5 @@ yarn-error.log* | |
|
||
# typescript | ||
*.tsbuildinfo | ||
|
||
.npmrc |
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 configuration writes the auth token to ~/.npmrc in plain text. Consider using the NODE_AUTH_TOKEN environment variable with actions/setup-node's built-in registry authentication instead, which is more secure and already configured on line 81.
Copilot uses AI. Check for mistakes.