-
Notifications
You must be signed in to change notification settings - Fork 6
/
entrypoint.sh
executable file
·134 lines (105 loc) · 3.89 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
#!/bin/bash
set -e
main() {
echo "" # see https://github.com/actions/toolkit/issues/168
sanitize "${GITHUB_TOKEN}" "GITHUB_TOKEN"
sanitize "${INPUT_FILE_NAME}" "INPUT_FILE_NAME"
arrBRANCH_NAME=(${GITHUB_REF//// })
BRANCH_NAME=${arrBRANCH_NAME[@]:(-1)}
IS_MASTER=false
if [ "$BRANCH_NAME" = "master" ] || [ "$BRANCH_NAME" = "main" ]; then
IS_MASTER=true
fi
TAG_NAME=v$(date +%m-%d-%Y.%H.%M)
if [ $IS_MASTER = false ]; then
TAG_NAME=${BRANCH_NAME}_${TAG_NAME}
fi
INPUT_EXTENSION="tex"
OUTPUT_EXTENSION="pdf"
OUTPUT_FILE=${INPUT_FILE_NAME/$INPUT_EXTENSION/$OUTPUT_EXTENSION}
if ! uses "${INPUT_LATEST_TAG}"; then
INPUT_LATEST_TAG="true"
fi
echo "=====> INPUTS <====="
echo "FILE_NAME: $INPUT_FILE_NAME"
echo "GENERATED TAG_NAME: $TAG_NAME"
echo "GITHUB REPOSITORY: $GITHUB_REPOSITORY"
echo "BRANCH: $BRANCH_NAME"
echo "IS_MASTER: $IS_MASTER"
echo "INPUT_EXTENSION: $INPUT_EXTENSION"
echo "OUTPUT_EXTENSION: $OUTPUT_EXTENSION"
echo "OUTPUT_FILE: $OUTPUT_FILE"
echo "=====> / INPUTS <====="
echo ""
set +e
echo "==> TRYING TO GENERATE THE DOCUMENT"
xelatex -file-line-error -halt-on-error -interaction=nonstopmode $INPUT_FILE_NAME
if [ ! $? -eq 0 ]; then
echo "ERROR : ❌ > THE PDF DOCUMENT CAN'T BE GENERATED‼️"
exit 1
else
echo "✅ $OUTPUT_FILE was successfully generated"
fi
set -e
createRelease $GITHUB_REPOSITORY $GITHUB_TOKEN $TAG_NAME $OUTPUT_FILE
if usesBoolean "${INPUT_LATEST_TAG}" && usesBoolean "${IS_MASTER}"; then
cleanLatest $GITHUB_REPOSITORY $GITHUB_TOKEN
createRelease $GITHUB_REPOSITORY $GITHUB_TOKEN "latest" $OUTPUT_FILE
fi
echo "::set-output name=TAG_NAME::${TAG_NAME}"
}
cleanLatest() {
echo "====> CLEANING LATEST RELEASE <===="
LATEST_RELEASE_ID=$(curl -s -X GET --url https://api.github.com/repos/$1/releases/tags/latest --header "authorization: token $2" | jq -r ".id")
if [ ! -z "${LATEST_RELEASE_ID}" ]; then
echo "-> DELETE latest tag"
curl -sS -X DELETE --url https://api.github.com/repos/$1/git/refs/tags/latest --header "authorization: token $2"
echo "-> DELETE latest release $LATEST_RELEASE_ID"
curl -sS -X DELETE --url https://api.github.com/repos/$1/releases/$LATEST_RELEASE_ID --header "authorization: token $2"
fi
}
createRelease() {
echo "==> CREATE TAG $3"
OUTPUT_TAG="$(curl -sS -X POST --url https://api.github.com/repos/$1/git/refs --header "authorization: token $2" --header 'content-type: application/json' \
--data '{
"ref": "refs/tags/'"$3"'",
"sha": "'"$GITHUB_SHA"'"
}')"
responseHandler "$OUTPUT_TAG"
echo "===> CREATE RELEASE $3"
OUTPUT_RELEASE="$(curl -sS -X POST --url https://api.github.com/repos/$1/releases --header "authorization: token $2" --header 'content-type: application/json' \
--data '{
"tag_name": "'"$3"'",
"name": "'"$3"'",
"body": "Document generated at '"$(date +%m-%d-%Y.%H:%M)"'"
}')"
responseHandler "$OUTPUT_RELEASE"
RELEASE_ID=$(echo $OUTPUT_RELEASE | jq -r '.id')
echo "====> UPLOAD ASSET TO RELEASE $RELEASE_ID ($3)"
UPLOAD_URL="https://uploads.github.com/repos/$1/releases/$RELEASE_ID/assets?name=$4"
OUTPUT_UPLOAD=$(curl -sS -X POST --header "authorization: token $2" --header 'content-type: application/pdf' --url $UPLOAD_URL -F "data=@$4")
responseHandler "$OUTPUT_UPLOAD"
ASSET_URL="https://github.com/$1/releases/download/$3/$4"
ROCKET_EMOJI="🚀"
echo -e "=====> $ROCKET_EMOJI -> Your Document is available at the addres $ASSET_URL"
}
responseHandler() {
if echo "${1}" | jq -e 'has("message")' > /dev/null; then
MSG=$(echo ${1} | jq -r '.message')
>&2 echo -e "-> ERROR the receive message is : \\n ${MSG}"
exit 1
fi
}
uses() {
[ ! -z "${1}" ]
}
usesBoolean() {
[ ! -z "${1}" ] && [ "${1}" = "true" ]
}
sanitize() {
if [ -z "${1}" ]; then
>&2 echo "Unable to find the ${2}. Did you set with.${2}?"
exit 1
fi
}
main