简体   繁体   English

如何使用GitHub Actions自动发布Typescript Package?

[英]How to use GitHub Actions to Auto Publish Typescript Package?

I am using GitHub actions to auto-publish my npm package whenever I push the code to master branch.每当我将代码推送到主分支时,我正在使用 GitHub 操作自动发布我的npm package The problem is in my .gitignore I have added the /dist/ folder.问题出在我的.gitignore中,我添加了/dist/文件夹。 So when I push the code to cloud, it ignores the dist folder and I want only dist folder to be published.因此,当我将代码推送到云端时,它会忽略dist文件夹,我只想发布 dist 文件夹。 How do I do this?我该怎么做呢?

I tried using npm install and npm run build to create a dist folder and in the cloud and then publish the package but it doesn't work.我尝试使用npm installnpm run build来创建dist文件夹并在云中然后发布 package 但它不起作用。 It doesn't create a compiled version.它不会创建编译版本。 The only solution I see is I remove /dist/ from .gitignore and upload it to the cloud but is there any other way that I can ignore dist and also auto-publish it to npm using Github Actions?我看到的唯一解决方案是从.gitignore中删除/dist/并将其上传到云端,但是有没有其他方法可以忽略 dist 并使用 Github 操作将其自动发布到 npm?

Here are my .yml file这是我的.yml文件

name: NPM Publish

on:
  push:
    branches:
      - master

jobs:
  release:
    name: Pubish
    runs-on: ubuntu-latest
    steps:
      - name: ⏬ checkout
        uses: actions/checkout@v2.1.1
      - name: 📦 Node
        uses: actions/setup-node@v1.4.2
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org
      - name: 🚀 Publish
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}

You have to build it before publishing or add a pre-publish script in your package.json file.您必须在发布之前构建它或在 package.json 文件中添加预发布脚本。

ie: IE:

  - name: Build
    run: npm run build
  - name: 🚀 Publish
    run: npm publish --access public

Or In package.json或者在 package.json

  ...
  "scripts": {
  ...
  "prepublish": "npm run build",
}

The reason why it's not building is that it doesn't have the node_modules so you also need to install them and ideally use cache.它没有构建的原因是它没有 node_modules 所以你还需要安装它们并最好使用缓存。

So you would need to add to your yaml file before building.所以你需要在构建之前添加到你的 yaml 文件中。

- name: install
  run: npm install

or if you are using a lock file, (not ideal for packages)或者如果您使用的是锁定文件,(不适合包)

- name: install
  run: npm ci

Please try to also use a cache instead of overloading npm registry.请尝试也使用缓存而不是重载 npm 注册表。

I had a similar problem.我有一个类似的问题。 After i did everything written here, action doesn't add dist/ folder to npm. Solution was create.npmignore file and add inside tsconfig.json and all ts files(*.ts)在我完成此处编写的所有内容后,操作不会将 dist/ 文件夹添加到 npm。解决方案是创建.npmignore 文件并在 tsconfig.json 和所有 ts 文件(*.ts)中添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM