3

I have npm publish github actions, I want to run this action if my commit has tag, otherwise I don't want to run my action because of that if I do not add any tag my commit then action is run and failed because it try to publish already publish npm package with same tag. For example with my last commit I have tag 1.2.3 and my npm package was publish with 1.2.3 version. When I add new commit to my branch without any tag actions try to publish my package with 1.2.3 version tag so it failed. Here my actions code below, is there any solution for it.

Thanks for advive.

name: NPM Publish

on:
  push:
    branches:
    - master
    tags: 
    - v*

jobs:
  build:
    name: Build  & Publish 
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v2.4.0
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/      
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

I need something like that on yml file

if(git_commit has tag) continue job else stop job;

EDITTED VERSION

I edit my yml file base on @Enrico Campidoglio suggestion but still is does not work. I made two commit first one without tag and it canceled the action but second one has tag it still canceled action. Is there any new suggestion or solution ?

name: NPM Publish

on:
  push:
    branches:
    - master

jobs:
  build:
    name: Build  & Publish 
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v2.4.0
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - run: echo "GIT_COMMIT=`echo $(git rev-parse --short HEAD)`" >> $GITHUB_ENV
      - run: echo "GIT_TAG=`echo $(git describe --tags --exact-match ${{ env.GIT_COMMIT }} || :)`" >> $GITHUB_ENV
      - run: echo ${{ env.GIT_TAG }} != v*
      - run: |
          if [[ ${{ env.GIT_TAG }} == v* ]] ; then
          echo "Tag found..."
          else
          echo "No git tag found, action cancelled..."
          exit 1
          fi


      - uses: andymckay/cancel-action@0.2
        if: ${{ env.GIT_TAG }} != v*
          
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

there is action result I cannot figure out what is the problem,

enter image description here

here the lastest failed action: https://github.com/sametcelikbicak/enum2array/runs/3513521031?check_suite_focus=true

Samet ÇELİKBIÇAK
  • 895
  • 3
  • 12
  • 25

2 Answers2

2

I found the solution finally after too many tried. I changed my mind and try to run shell script and it works :)

Just add that line in my yml file

      - name: Check Git Tag to continue publish
        run: ./scripts/publish.sh

and I created a sh file for control the commit tag. You can find the latest script and yml file definitions below

Here is my lastest yml file, npm-publish.yml

name: NPM Publish

on:
  push:
    branches:
    - master

jobs:
  build:
    name: Build  & Publish 
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v2.4.0
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/

      - name: Check Git Tag to continue publish
        run: ./scripts/publish.sh
          
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

Here is my script file, publish.sh

#!/usr/bin/env bash

GIT_COMMIT=$(git rev-parse --short HEAD)
GIT_TAG=$(git describe --tags --exact-match $COMMIT || :)

if [[ ${GIT_TAG} == v* ]] ; then
    echo "$GIT_TAG Tag found..."
else
    echo "No git tag found, action cancelled..."
    exit 1
fi
Samet ÇELİKBIÇAK
  • 895
  • 3
  • 12
  • 25
0

For the time being, there isn't an official action to cancel the current workflow. There is, however, an official GitHub API and a third-party action that invokes it. You could combine it with an if conditional and the github context to achieve what you want:

steps:
  - uses: andymckay/cancel-action@0.2
    if: startsWith(github.ref, 'refs/tags')

Be aware that cancelling a workflow through the API is an asynchronous operation, which means that later steps might still get executed until the workflow runner handles the request.

A much more solid approach would be to put a condition on your publishing step to only run when the workflow was triggered by a new tag:

steps:
  - run: npm publish --access public
    if: startsWith(github.ref, 'refs/tags')
    env:
      NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154