forked from hole19/git-tag-action
-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.sh
executable file
·57 lines (48 loc) · 1.13 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# input validation
if [[ -z "${TAG}" ]]; then
echo "No tag name supplied"
exit 1
fi
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "No github token supplied"
exit 1
fi
# get GitHub API endpoints prefix
git_refs_url=$(jq .repository.git_refs_url $GITHUB_EVENT_PATH | tr -d '"' | sed 's/{\/sha}//g')
# check if tag already exists in the cloned repo
tag_exists="false"
if [ $(git tag -l "$TAG") ]; then
tag_exists="true"
else
# check if tag exists in the remote repo
getReferenceStatus=$(curl "$git_refs_url/tags/$TAG" \
-H "Authorization: token $GITHUB_TOKEN" \
--write-out "%{http_code}" -s -o /dev/null)
if [ "$getReferenceStatus" = '200' ]; then
tag_exists="true"
fi
fi
echo "**pushing tag $TAG to repo $GITHUB_REPOSITORY"
if $tag_exists
then
# update tag
curl -s -X PATCH "$git_refs_url/tags/$TAG" \
-H "Authorization: token $GITHUB_TOKEN" \
-d @- << EOF
{
"sha": "$GITHUB_SHA",
"force": true
}
EOF
else
# create new tag
curl -s -X POST "$git_refs_url" \
-H "Authorization: token $GITHUB_TOKEN" \
-d @- << EOF
{
"ref": "refs/tags/$TAG",
"sha": "$GITHUB_SHA"
}
EOF
fi