npm 发布到github_如何设置github操作以将monorepo发布到npm

孙梓
2023-12-01

npm 发布到github

In my last post, I showed you how to create, test, and build a monorepo repository. In this article, I will show you how to automate the publishing of your monorepo to NPM using GitHub Actions.

在上一篇文章中 ,我向您展示了如何创建,测试和构建monorepo存储库。 在本文中,我将向您展示如何使用GitHub Actions将monorepo自动发布到NPM。

什么是GitHub操作? (What are GitHub Actions?)

GitHub Actions allow for automating workflows based on repository events such as push, issue creation, or the creation of a new release.

GitHub Actions允许基于存储库事件(例如推送,问题创建或新版本创建)使工作流程自动化。

Workflows are composed of jobs, which run concurrently by default. Each job should represent a separate part of your workflow described using steps.

工作流由作业组成,这些作业默认情况下并发运行。 每个作业应代表您的工作流的独立部分,并使用步骤进行描述。

For the propose of this article, we will have one job that will describe what steps must be followed to publish our package.

对于本文的建议,我们将完成一项工作,该工作将描述发布包时必须遵循的步骤。

设定档 (Config)

You need to set the NPM_AUTH_TOKEN in your repo settings. This is the token the action will use to authenticate to NPM. You need to generate one in NPM, then you can add it to your secrets (settings -> secrets) so that it can be passed to the step.

您需要在回购设置中设置NPM_AUTH_TOKEN 。 这是操作将用于向NPM进行身份验证的令牌。 您需要在NPM中生成一个,然后可以将其添加到您的秘密(设置->秘密)中,以便可以将其传递到步骤。

Pro tip: DO NOT put the token directly in your workflow file.

专家提示: 请勿将令牌直接放入您的工作流程文件中。

创建我们的工作流文件 (Creating Our Workflow File)

We will define our workflow by creating a YAML file.

我们将通过创建YAML文件来定义工作流程。

You can create this file directly on your GitHub repository page. You will just have to click on the “Actions” tab and then on “set up a workflow yourself”. You can delete all the generated code and rename the workflow file.

您可以直接在GitHub存储库页面上创建此文件。 您只需要单击“操作”选项卡,然后单击“自行设置工作流程”。 您可以删除所有生成的代码并重命名工作流文件。

Or you can create this file on your project directory. At the root of your repository, create a directory named .github/workflows to store your workflow files. In .github/worflows, add a .yml or .yaml file for your workflow. For example, .github/workflows/npm-publish.yml.

或者,您可以在项目目录中创建此文件。 在存储库的根目录中,创建一个名为.github/workflows的目录来存储您的工作流文件。 在.github/worflows ,为您的工作流添加.yml.yaml文件。 例如, .github/workflows/npm-publish.yml

With our workflow file created, we can start editing it. First, we define when the workflow will be triggered. For example, this workflow is triggered when changes are pushed to the master branch or when a pull request is created.

创建工作流程文件后,我们可以开始对其进行编辑。 首先,我们定义何时触发工作流程。 例如,当将更改推送到master分支或创建拉取请求时,将触发此工作流。

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

工作和机器 (Job and Machine)

Now it’s time to create our “Publish” job and its steps.

现在该创建我们的“发布”作业及其步骤了。

First, we will define a name and the type of machine to run our job. You can set any name you like. In that case, I’ll call it “Publish”. The machine can be either a GitHub-hosted runner or a self-hosted runner.

首先,我们将定义名称和机器类型来运行我们的工作。 您可以设置任何喜欢的名称。 在这种情况下,我将其称为“发布”。 该机器可以是GitHub托管的运行程序,也可以是自托管的运行程序。

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  publish:
    name: Publish
    runs-on: ubuntu-latest

脚步 (Steps)

To publish our package on NPM, we will have the following steps:

要在NPM上发布我们的软件包,我们将执行以下步骤:

  • Checkout: Checks-out our repository, so our workflow can access it.

    检出:检出我们的存储库,以便我们的工作流可以访问它。

  • Cache node_modules: Caches dependencies and build outputs to improve workflow execution time.

    缓存node_modules:缓存依赖项并构建输出以缩短工作流执行时间。

  • Git Identity: Grants access to GitHub Actions to modify our repository files.

    Git身份:授予对GitHub操作的访问权限,以修改我们的存储库文件。

  • Install: Installs the project dependencies.

    安装:安装项目依赖项。

  • Build: Builds our project.

    构建:构建我们的项目。

  • Set up Node: Sets up the Node version and the repository URL.

    设置节点:设置节点版本和存储库URL。

  • Publish: Publishes our package on NPM.

    发布:发布 我们在NPM上的软件包。

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  publish:
    name: Publish
    runs-on: ubuntu-latest
      publish:
    name: Publish
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Cache node_modules
        id: cache-modules
        uses: actions/cache@v1
        with:
          path: node_modules
          key: 12.x-${{ runner.OS }}-build-${{ hashFiles('package.json') }}
      - name: Git Identity
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'
          git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/$GITHUB_REPOSITORY
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Install
        if: steps.cache-modules.outputs.cache-hit != 'true'
        run: yarn install
      - name: Build
        run: yarn build
      - name: Setup Node ${{ matrix.node_version }}
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
          registry-url: 'https://registry.npmjs.org'
      - name: Publish
        run: yarn publish:all
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

结论 (Conclusion)

That is our final workflow file. You can improve this workflow by adding more jobs and steps. For example, test your code or run the project in other environments.

那是我们最终的工作流程文件。 您可以通过添加更多作业和步骤来改进此工作流程。 例如,测试您的代码或在其他环境中运行该项目。

翻译自: https://medium.com/@cesarwilliam/how-to-set-up-github-actions-to-publish-a-monorepo-to-npm-f881e10f01d9

npm 发布到github

 类似资料: