-
Notifications
You must be signed in to change notification settings - Fork 10
/
entrypoint.sh
executable file
·167 lines (149 loc) · 5.28 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
# Backwards compatibility mapping
if [ -z "$VERSION_REGEX" ]; then :; else
INPUT_VERSION_REGEX=$VERSION_REGEX
fi
if [ -z "$PRERELEASE_REGEX" ]; then :; else
INPUT_PRERELEASE_REGEX=$PRERELEASE_REGEX
fi
if [ -z "$DRAFT" ]; then :; else
INPUT_CREATE_DRAFT=$DRAFT
fi
if [ -z "$UPDATE_EXISTING" ]; then :; else
INPUT_UPDATE_EXISTING=$UPDATE_EXISTING
fi
if [ -z "$CHANGELOG_FILE" ]; then :; else
INPUT_CHANGELOG_FILE=$CHANGELOG_FILE
fi
if [ -z "$CHANGELOG_HEADING" ]; then :; else
INPUT_CHANGELOG_HEADING=$CHANGELOG_HEADING
fi
if [ -z "${INPUT_RELEASE_TEXT}" ]; then
PARSE_CHANGELOG=true
else
PARSE_CHANGELOG=false
RELEASE_BODY=${INPUT_RELEASE_TEXT}
fi
set -euo
set_tag() {
if [ -n "${INPUT_CREATED_TAG}" ]; then
TAG=${INPUT_CREATED_TAG}
else
TAG="$(echo "${GITHUB_REF}" | grep tags | sed --regexp-extended 's/^\w+\/\w+\///g' || true)"
fi
}
create_release_data() {
RELEASE_DATA="{}"
RELEASE_DATA=$(echo "${RELEASE_DATA}" | jq --arg tag "$TAG" '.tag_name = $tag')
if $PARSE_CHANGELOG; then
echo "::debug::Trying to parse change log"
if [ -e "$INPUT_CHANGELOG_FILE" ]; then
echo "::debug::Change log file found"
RELEASE_BODY=$(submark -O --"$INPUT_CHANGELOG_HEADING" "$TAG" "$INPUT_CHANGELOG_FILE")
if [ -n "${RELEASE_BODY}" ]; then
echo "::notice::Changelog entry found, adding to release"
RELEASE_BODY=$(echo "$RELEASE_BODY" | sed -z 's/%/%25/g')
RELEASE_BODY=$(echo "$RELEASE_BODY" | sed -z 's/\n/%0A/g')
RELEASE_BODY=$(echo "$RELEASE_BODY" | sed -z 's/\r/%0D/g')
echo "changelog=${RELEASE_BODY}" >>"$GITHUB_OUTPUT"
RELEASE_DATA=$(echo "${RELEASE_DATA}" | jq --arg body "${RELEASE_BODY}" '.body = $body')
else
echo "::warning::Changelog entry not found!"
fi
else
echo "::warning::Changelog file not found! ($INPUT_CHANGELOG_FILE)"
fi
else
echo "::notice::Using passed release text"
RELEASE_DATA=$(echo "${RELEASE_DATA}" | jq --arg body "${RELEASE_BODY}" '.body = $body')
fi
RELEASE_DATA=$(echo "${RELEASE_DATA}" | jq --argjson value "${INPUT_CREATE_DRAFT}" '.draft = $value')
_PRERELEASE_VALUE="false"
if [ -n "${INPUT_PRERELEASE_REGEX}" ]; then
if echo "${TAG}" | grep -qE "$INPUT_PRERELEASE_REGEX"; then
_PRERELEASE_VALUE="true"
fi
fi
RELEASE_DATA=$(echo "${RELEASE_DATA}" | jq --argjson value $_PRERELEASE_VALUE '.prerelease = $value')
if [ -n "${INPUT_RELEASE_TITLE}" ]; then
RELEASE_DATA=$(echo "${RELEASE_DATA}" | jq --arg name "${INPUT_RELEASE_TITLE}" '.name = $name')
fi
}
set_tag
if [ -z "$TAG" ]; then
echo "::error::This is not a tagged push." 1>&2
exit 1
fi
if ! echo "${TAG}" | grep -qE "$INPUT_VERSION_REGEX"; then
echo "::error::Bad version in tag, needs to be adhere to the regex '$INPUT_VERSION_REGEX'" 1>&2
exit 1
fi
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
RELEASE_ID=$TAG
echo "Starting release process for tag '$TAG'"
HTTP_RESPONSE=$(curl --write-out "HTTPSTATUS:%{http_code}" \
-sSL \
-H "${AUTH_HEADER}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/${RELEASE_ID}")
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
if [ "$HTTP_STATUS" -eq 200 ]; then
echo "Existing release found"
if [ "${INPUT_UPDATE_EXISTING}" = "true" ]; then
echo "Updating existing release"
create_release_data
RECEIVED_DATA=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
RELEASE_DATA=$(echo "$RELEASE_DATA" | jq --argjson r_value "$(echo "$RECEIVED_DATA" | jq '.draft')" '.draft = if ( $r_value != true or .draft != true ) then false else true end ')
RESPONSE="$(curl \
--write-out "%{http_code}" \
--silent \
--show-error \
--location \
--request PATCH \
--header "${AUTH_HEADER}" \
--header "Content-Type: application/json" \
--data "${RELEASE_DATA}" \
"$(echo "${RECEIVED_DATA}" | jq -r '.url')")"
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
CONTENT=$(echo "$RESPONSE" | sed "$ d" | jq --args)
if [ "$HTTP_STATUS" -eq 200 ]; then
echo "::notice::Release updated"
{
echo "id=$(echo "$CONTENT" | jq ".id")"
echo "html_url=$(echo "$CONTENT" | jq ".html_url")"
echo "upload_url=$(echo "$CONTENT" | jq ".upload_url")"
} >>"$GITHUB_OUTPUT"
else
echo "::error::Failed to update release ($HTTP_STATUS):"
echo "$CONTENT" | jq ".errors"
exit 1
fi
else
echo "::notice::Updating disabled, finishing workflow"
fi
else
echo "Creating new release"
create_release_data
RESPONSE=$(curl \
--write-out "%{http_code}" \
--silent \
--show-error \
--location \
--header "${AUTH_HEADER}" \
--header "Content-Type: application/json" \
--data "${RELEASE_DATA}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases")
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
CONTENT=$(echo "$RESPONSE" | sed "$ d" | jq --args)
if [ "$HTTP_STATUS" -eq 201 ]; then
echo "::notice::Release successfully created"
{
echo "id=$(echo "$CONTENT" | jq ".id")"
echo "html_url=$(echo "$CONTENT" | jq ".html_url")"
echo "upload_url=$(echo "$CONTENT" | jq ".upload_url")"
} >>"$GITHUB_OUTPUT"
else
echo "::error::Failed to update release ($HTTP_STATUS):"
echo "$CONTENT" | jq ".errors"
exit 1
fi
fi