skip to Main Content

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

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

2

Answers


  1. You can use the strategy outlined in Shell – check if a git tag exists in an if/else statement to figure out whether the tag exists or not.

    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.1"
              if [ $(git tag -l "${TAG}") ]; then
                echo "Tag ${TAG} exists"
              else
                echo "Tag ${TAG} does not exist"
              fi
    

    I tested this in my own github repository, you can see that in this run, the tag was not found, but when I published tag 0.1, this run shows that it is successful.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search