forked from sanfelice/create-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·68 lines (51 loc) · 1.97 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
#!/bin/bash -l
set -e
# Sets git username and email
sh -c "git config --global user.name '${GITHUB_ACTOR}' \
&& git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'"
CHANGELOG="CHANGELOG.md"
#Execute build script available through $1 parameter
NEW_TAG=$(bash -c "$1")
echo "NEW TAG $NEW_TAG"
#Temp file to store commit messages
TEMP_FILE="/tmp/log"
BARE="/tmp/bare"
#Bare clone to get last tag and all the commits since that tag
git clone --bare $(git remote get-url origin) $BARE
#Getting tags and commit messages from bare repo
LAST_TAG="$(git -C $BARE describe --abbrev=0 || echo "-1")"
if [ "$LAST_TAG" == "-1" ]; then
(git -C $BARE log --format="- %B" --no-merges | tr '"' ' ') > $TEMP_FILE
else
(git -C $BARE log --format="- %B" $LAST_TAG... --no-merges | tr '"' ' ') > $TEMP_FILE
fi
#Creates CHANGELOG.md file if it doesn't exists
if [ ! -f "$CHANGELOG" ]; then
echo "Creating CHANGELOG.md"
touch "$CHANGELOG"
echo -e "# CHANGELOG\n\n" > $CHANGELOG
fi
#Update changelog file with the new version and commit messages
sed -i "3s/^/## $NEW_TAG\n\n\n/" CHANGELOG.md
sed -i "4r $TEMP_FILE" CHANGELOG.md
#Add new and changed files and makes a commit
git add .
git commit -m "Entrega da versão $NEW_TAG"
#Push recently created commit along with tags
git push
COMMIT=$(git log --format="%H" -n 1)
TAG_MESSAGE="$(cat $TEMP_FILE)"
echo "$TAG_MESSAGE"
OUT=$(curl \
-X POST \
-H 'authorization: Bearer '"$TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$GITHUB_REPOSITORY/git/tags \
-d '{"tag":"'"$NEW_TAG"'","message":"'"${TAG_MESSAGE//$'\n'/'\n'}"'","object":"'"$COMMIT"'","type":"commit"}')
TAG_SHA=$(echo $OUT | python3 -c "import sys, json; print(json.load(sys.stdin)['sha'])")
curl \
-X POST \
-H 'authorization: Bearer '"$TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$GITHUB_REPOSITORY/git/refs \
-d '{"ref":"refs/tags/'"$NEW_TAG"'","sha":"'"${TAG_SHA}"'"}'