forked from ChloePlanet/github-tag-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·139 lines (116 loc) · 3.2 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
echo ::set-output name=tag_generated::0
# config
with_v=${WITH_V:-false}
release_branches=${RELEASE_BRANCHES:-master}
custom_tag=${CUSTOM_TAG}
source=${SOURCE:-.}
# since https://github.blog/2022-04-12-git-security-vulnerability-announced/ runner uses?
git config --global --add safe.directory /github/workspace
cd ${GITHUB_WORKSPACE}/${source}
pre_release="true"
IFS=',' read -ra branch <<< "$release_branches"
for b in "${branch[@]}"; do
echo "Is $b a match for ${GITHUB_REF#'refs/heads/'}"
if [[ "${GITHUB_REF#'refs/heads/'}" =~ $b ]]
then
pre_release="false"
fi
done
echo "pre_release = $pre_release"
# fetch tags
git fetch --tags
# get latest tag that looks like a semver (with or without v)
tag=$(git for-each-ref --sort=-v:refname --count=1 --format '%(refname)' refs/tags/[0-9]*.[0-9]*.[0-9]* refs/tags/v[0-9]*.[0-9]*.[0-9]* | cut -d / -f 3-)
tag_commit=$(git rev-list -n 1 $tag)
echo $tag
last_major=$(semver get major $tag)
last_minor=$(semver get minor $tag)
last_patch=$(semver get patch $tag)
echo ::set-output name=last_major::$last_major
echo ::set-output name=last_minor::$last_minor
echo ::set-output name=last_patch::$last_patch
# get current commit hash for tag
commit=$(git rev-parse HEAD)
if [ "$tag_commit" == "$commit" ]; then
echo "No new commits since previous tag. Skipping the tag creation..."
echo ::set-output name=last_tag::$tag
exit 0
fi
# if there are none, start tags at 0.0.0
if [ -z "$tag" ]
then
log=$(git log --pretty=oneline)
tag=0.0.0
else
log=$(git log $tag..HEAD --pretty=oneline)
fi
echo $log
# get commit logs and determine home to bump the version
# supports #major, #minor, #patch
case "$log" in
*#major* )
new=$(semver bump major $tag)
bump_ver="major"
;;
*#minor* )
new=$(semver bump minor $tag)
bump_ver="minor"
;;
*#patch* )
new=$(semver bump patch $tag)
bump_ver="patch"
;;
* )
echo "This commit message doesn't include #major, #minor or #patch. Skipping the tag creation..."
echo ::set-output name=last_tag::$tag
exit 0
;;
esac
# did we get a new tag?
if [ ! -z "$new" ]
then
# prefix with 'v'
if $with_v
then
new="v$new"
fi
if $pre_release
then
new="$new-${commit:0:7}"
fi
fi
if [ ! -z $custom_tag ]
then
new="$custom_tag"
fi
echo $new
major=$(semver get major $new)
minor=$(semver get minor $new)
patch=$(semver get patch $new)
# set outputs
echo ::set-output name=last_tag::$tag
echo ::set-output name=new_tag::$new
echo ::set-output name=major::$major
echo ::set-output name=minor::$minor
echo ::set-output name=patch::$patch
echo ::set-output name=bump_ver::$bump_ver
if $pre_release
then
echo "This branch is not a release branch. Skipping the tag creation..."
exit 0
fi
# push new tag ref to github
dt=$(date '+%Y-%m-%dT%H:%M:%SZ')
full_name=$GITHUB_REPOSITORY
git_refs_url=$(jq .repository.git_refs_url $GITHUB_EVENT_PATH | tr -d '"' | sed 's/{\/sha}//g')
echo "$dt: **pushing tag $new to repo $full_name"
curl -s -X POST $git_refs_url \
-H "Authorization: token $GITHUB_TOKEN" \
-d @- << EOF
{
"ref": "refs/tags/$new",
"sha": "$commit"
}
EOF
echo ::set-output name=tag_generated::1