-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.sh
executable file
·86 lines (70 loc) · 2.02 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
#!/bin/bash
# config
DEFAULT_BUMP=${INPUT_DEFAULTBUMP:-patch}
PREFIX=${INPUT_PREFIX}
NPM=${INPUT_NPM}
PACKAGE_JSON_PATH=${INPUT_PACKAGE_JSON_PATH}
# fetch tags
git fetch --tags
# get latest tag
REFLIST=$(git rev-list --tags --max-count=1)
if [ -n "$PREFIX" ]
then
TAG=$(git describe --tags --match "$PREFIX*" "$REFLIST")
# Remove prefix from semantic version field
VERSION=${TAG//${PREFIX}/}
else
TAG=$(git describe --tags "$REFLIST")
VERSION=TAG
fi
TAG_COMMIT=$(git rev-list -n 1 "$TAG")
# get current commit hash for tag
COMMIT=$(git rev-parse HEAD)
if [ "$TAG_COMMIT" == "$COMMIT" ]; then
echo "No new commits since previous tag. Skipping..."
echo "::set-output name=tag::$TAG"
exit 0
fi
# if there are none, start tags at 0.0.0
if [ -z "$TAG" ]
then
LOG=$(git log --pretty=oneline)
VERSION=0.0.0
else
LOG=$(git log "$TAG..HEAD" --pretty=oneline)
fi
# get commit logs and determine home to bump the version
# supports #major, #minor, #patch (anything else will be 'minor')
case "$LOG" in
*#${PREFIX}major* ) VERSION=$(semver bump major "$VERSION");;
*#${PREFIX}minor* ) VERSION=$(semver bump minor "$VERSION");;
*#${PREFIX}patch* ) VERSION=$(semver bump patch "$VERSION");;
* ) VERSION=$(semver bump "$DEFAULT_BUMP" "$VERSION");;
esac
NEW=$VERSION
# prefix with custom string
if [ -n "$PREFIX" ]
then
NEW="$PREFIX$NEW"
fi
echo "$NEW"
# set outputs
echo "::set-output name=tag::$NEW"
echo "::set-output name=version::$VERSION"
if [[ "$NPM" = true && -f "$PACKAGE_JSON_PATH" ]]; then
# update package.json
jq ".version = \"$VERSION\"" "$PACKAGE_JSON_PATH" > "tmp" && mv "tmp" "$PACKAGE_JSON_PATH"
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 $INPUT_TOKEN" \
-d @- << EOF
{
"ref": "refs/tags/$NEW",
"sha": "$COMMIT"
}
EOF