Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if a github tag exists in a repo within a github action

I am trying to check if a specific tag exists in a repository. Here is a simplified version of what I have tried so far.

Attempt 1:

name: Check for Tag

on: push

jobs:
  check-tag:
    runs-on: ubuntu-latest

    steps:
      - name: Check for Tag
        run: |
          TAG="0.1"
          if git show-ref --tags --verify --quiet "refs/tags/${TAG}"; then
            echo "Tag ${TAG} exists"
          else
            echo "Tag ${TAG} does not exist"
          fi

Output: Tag 0.1 does not exist

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However if i execute the following command on CLI, the tag exists
enter image description here

git show-ref --tags --verify returns an exit code of 0 if the tag has been found.

Attempt 2:

name: Check for Tag

on: push

jobs:
  check-tag:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        
      - name: Check for Tag
        run: |
          TAG="0.2"
          git show-ref --tags --verify --quiet "refs/tags/${TAG}" 
          exit_code=$?
          echo ${exit_code}
          if [[ $exit_code -eq 0 ]]; then
            echo "Tag ${TAG} exists"
          else
            echo "Tag ${TAG} does not exist"
          fi

The workflow fails because 0.2 tag does not exist and returns 1 as exit code

>Solution :

You need the complete git history to query that.

Use fetch-depth: 0 with actions/checkout.

Here’s your updated workflow:

name: Check for Tag

on: push

jobs:
  check-tag:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
        
      - name: Check for Tag
        run: |
          TAG="0.2"
          if git show-ref --tags --verify --quiet "refs/tags/${TAG}"; then
            echo "Tag ${TAG} exists"
          else
            echo "Tag ${TAG} does not exist"
          fi

See https://github.com/actions/checkout for more details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading