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
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
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.
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.
You need the complete git history to query that.
Use
fetch-depth: 0
withactions/checkout
.Here’s your updated workflow:
See https://github.com/actions/checkout for more details.