Arsalan Khattak
Arsalan Khattak

Reputation: 958

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. The problem is in my .gitignore I have added the /dist/ folder. So when I push the code to cloud, it ignores the dist folder and I want only dist folder to be published. 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. 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?

Here are my .yml file

name: NPM Publish

on:
  push:
    branches:
      - master

jobs:
  release:
    name: Pubish
    runs-on: ubuntu-latest
    steps:
      - name: ⏬ checkout
        uses: actions/[email protected]
      - name: 📦 Node
        uses: actions/[email protected]
        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}}

Upvotes: 3

Views: 1688

Answers (2)

Iicytower
Iicytower

Reputation: 1

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)

Upvotes: 0

Antoine Eskaros
Antoine Eskaros

Reputation: 851

You have to build it before publishing or add a pre-publish script in your package.json file.

i.e:

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

Or In 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.

So you would need to add to your yaml file before building.

- 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.

Upvotes: 2

Related Questions