3

I am creating my own NPM packages for the first time. For each commit, 1/ The package version should increase on the NPM registry, 2/ Update the package.json file in the github repository.

.github/workflows/publish.yml


on: 
  push:
    branches:
      - main
      
env:
  version: 0

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: AutoModality/action-clean@v1
      - uses: actions/checkout@v2
        with:
          ref: 'main'
          fetch-depth: 0
          
      - uses: actions/setup-node@v2
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
          scope: "@nandhirajan-tfi"
      - run: echo "version=$(npm show @nandhirajan-tfi/my-package version)" >> $GITHUB_ENV
      - run: npm version ${{env.version}} --no-git-tag-version --allow-same-version
      - run: npm install 
      - run: npm build
      - run: npm version patch -m "[RELEASE] %s" --no-git-tag-version --allow-same-version
      - run: npm publish
        env: 
          credentials: ${{secrets.GITHUB_TOKEN}}

GitHub Actions Output: enter image description here

The above log says that the npm publish command has updated the NPM Version to 1.11.18. But the changes are not reflecting on the NPM registry.

enter image description here

Any help would be appreciated.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Nandy
  • 666
  • 1
  • 12
  • 27

1 Answers1

0

First of all you need to retrieve your package.json version. You can use the action ga-project-version for that.

Example:

name: release

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      
      # This is how you use the ga-project-version action
      - name: Get version of the project
        id: project-version
        uses: 'euberdeveloper/ga-project-version@main'

      # In this step the exposed version is used as tag of a github release to publish
      - name: Add release
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          # This is how you access the exposed version
          automatic_release_tag: "${{ steps.project-version.outputs.version }}"
          title: "Deploy"
          files: |
            backend.tar.gz

As you can see, you will have the version available in "${{ steps.project-version.outputs.version }}". After that the question is with which logic you will augment your version, e.g. the simplest one would be increasing the last number, but it's not a good idea, it'd not be semver.

After that you can use your custom way to get the newer version by using the current one as input ("${{ steps.project-version.outputs.version }}"`).

After having the new version, just edit the package.json (e.g. sed command) to write the new version, and commit it (e.g. stefanzweifel/git-auto-commit-action action).

In the end you can publish your npm package from the github action (follow this link)

EuberDeveloper
  • 874
  • 1
  • 14
  • 38