0

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

user2650277
  • 6,289
  • 17
  • 63
  • 132
  • With `actions/checkout`, use `fetch-depth: 0` to fetch the complete git history. – Azeem May 04 '23 at 06:04
  • See your updated workflow [here](https://rhysd.github.io/actionlint/#eJx9T0tOwzAQ3fsUTxFbkwp2RkigLrhAL+C649hQ7JAZ9yPg7jikhCwQK3vedybZVzJYB3Iv8HnAxnZK5WTQFw5KPectGwW4UaDFduMADCWxHlVlW5IUvbdCLOqbY6GeJxmgkX4LcpELDBQmNrBOYk7cugv9cLidFccowcwT4Elc0DvqK4zVTPxVNF/yI6r7GnwswjaPT/fN6vqmWWDRo4sCDvmoB/LQ471cnwMN0Z/r561EEjSV5Hbk2qv3GvTZ3EECpUUUQC5kNHUHTBrQKbLwso/2TP97dpkYKctkXnp9VF94eXYZ). – Azeem May 04 '23 at 06:12
  • @Azeem Argh can't believe i missed that. Post the comment as an answer – user2650277 May 04 '23 at 06:28

2 Answers2

1

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.

Azeem
  • 11,148
  • 4
  • 27
  • 40
0

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.

eten
  • 803
  • 3
  • 14