Matt Simon
Matt Simon

Reputation: 23

Github action npm publish use tag name

I'm migrating our existing Travis tasks to GH actions. For Travis the command below would publish to npm and use the release tag name for the npm version.

script: yarn npm-bundle && npm version $TRAVIS_BRANCH --allow-same-version -m
      "chore - release version %s [skip ci]" --allow-empty

Unfortunately changing to the below doesn't work...

        run: |
          yarn npm-bundle && npm version ${{ github.event.release.tag_name }} --allow-same-version -m "chore - release version %s [skip ci]" --allow-empty
          npm publish --access public --dry-run

Its obviously empty as npm is using the version from package.json. I've tried some other variables such as ${{ github.head_ref }}

also...

run: |
          yarn npm-bundle -m "chore - release version %s [skip ci]" --allow-empty
          npm publish --tag ${{ github.event.release.tag_name }} --allow-same-version --access public --dry-run

Upvotes: 0

Views: 1793

Answers (2)

Matt Simon
Matt Simon

Reputation: 23

I have resolved the issue by refactoring to the following...

      - uses: actions/setup-node@v1
        with:
          node-version: 14.15.0
          registry-url: https://registry.npmjs.org/
      - run: yarn install
      - run: git config --global user.name "${{ github.actor }}"
      - run: git config --global user.email "github-action-${{ github.actor }}@users.noreply.github.com"
      - run: npm version ${{ github.event.release.tag_name }}
      - run: yarn npm-bundle
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

Upvotes: 1

GuiFalourd
GuiFalourd

Reputation: 22960

You can use the npm-publish action in your workflow instead of the script you used in Travis.

If you look for more actions available with NPM, you can find them on thee Github Marketplace

For example here, you could use something like this in your workflow, adapting in with your context with other run step if you need to use yarn or other commands:

on: push

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 10
      - run: npm install
      - run: npm test
      - uses: JS-DevTools/npm-publish@v1
        with:
          token: ${{ secrets.NPM_TOKEN }}
          tag: <your release tag name>

For more information regarding how this action work, check here.

Other Action

This other action (publish-to-npm) could be interesting as well if you want to check.

Upvotes: 0

Related Questions