How to publish package to npm in github ci

发布时间:2024年01月04日

To publish a package to npm as part of a GitHub CI (Continuous Integration) workflow, you can follow these steps:

  1. 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.
  2. 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.
  3. 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_TOKEN and the value set to your npm token.
  4. Configure your GitHub CI workflow:

    • In your repository, create or modify the .github/workflows/main.yml file 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 }}
      
  5. Commit and push your changes:

    • Commit the changes to your .github/workflows/main.yml file and push them to your GitHub repository.

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.

文章来源:https://blog.csdn.net/yuguo_im/article/details/135370448
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。