Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cancel a GitHub Actions workflow if the commit has no tag

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/[email protected]
        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/[email protected]
        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/[email protected]
        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

like image 629
Samet ÇELİKBIÇAK Avatar asked Nov 25 '25 07:11

Samet ÇELİKBIÇAK


1 Answers

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/[email protected]
        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
like image 158
Samet ÇELİKBIÇAK Avatar answered Nov 26 '25 20:11

Samet ÇELİKBIÇAK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!