How to publish package to npm in github ci
·
To publish a package to npm as part of a GitHub CI (Continuous Integration) workflow, you can follow these steps:
-
Create an npm account:
- If you don’t have an npm account, create one by visiting the npm website (https://www.npmjs.com/signup).
- Verify your email address associated with the npm account.
-
Generate an npm token:
- Generate an npm token that will be used to authenticate the CI workflow when publishing the package. You can generate a token by following the instructions provided by npm: https://docs.npmjs.com/creating-and-viewing-authentication-tokens.
-
Add the npm token as a secret in your GitHub repository:
- Go to your GitHub repository and navigate to “Settings” > “Secrets”.
- Click on “New repository secret” and add a secret with the name
NPM_TOKENand the value set to your npm token.
-
Configure your GitHub CI workflow:
- In your repository, create or modify the
.github/workflows/main.ymlfile to define your CI workflow. - Configure the workflow to run on the desired events, such as pushes or pull requests.
- Add the necessary steps to build and test your package.
- Add a step to publish the package to npm using the npm token. Here’s an example step:
- name: Publish to npm run: | echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc npm publish env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- In your repository, create or modify the
-
Commit and push your changes:
- Commit the changes to your
.github/workflows/main.ymlfile and push them to your GitHub repository.
- Commit the changes to your
Now, whenever the configured events trigger the CI workflow, it will build, test, and publish your package to npm using the provided npm token.
Please note that it’s important to ensure that your CI workflow is properly configured and tested before publishing packages automatically. Additionally, make sure to update the version number in your package.json file each time you make changes to your package to ensure proper versioning.
更多推荐




所有评论(0)