forked from elgohr/Github-Release-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·192 lines (145 loc) · 3.63 KB
/
Copy pathentrypoint.sh
File metadata and controls
executable file
·192 lines (145 loc) · 3.63 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env bash
set -Eeuo pipefail
log() {
printf '%s\n' "$*"
}
error() {
printf 'ERROR: %s\n' "$*" >&2
}
setup_auth() {
local token="${GITHUB_TOKEN:-${INPUT_TOKEN:-}}"
if [ -z "$token" ]; then
error "The token input is not set."
exit 1
fi
# Mask it in logs as a safety net.
echo "::add-mask::$token"
# gh checks GH_TOKEN before GITHUB_TOKEN.
export GH_TOKEN="$token"
export GITHUB_TOKEN="$token"
}
release_exists() {
gh release view "$1" >/dev/null 2>&1
}
get_release_commit() {
local tag="$1"
git fetch --quiet --no-tags origin "refs/tags/$tag" || return 1
git rev-parse "FETCH_HEAD^{commit}" || return 1
}
get_target_commit() {
local commit="${INPUT_COMMIT:-}"
if [ -n "$commit" ]; then
git fetch --quiet --no-tags origin "$commit" || return 1
else
git fetch --quiet --no-tags origin HEAD || return 1
fi
git rev-parse "FETCH_HEAD^{commit}" || return 1
}
release_is_current() {
local tag="$1"
local title="$2"
local body="${INPUT_BODY:-}"
local release_commit
local target_commit
local release_title
local release_body
if ! release_commit="$(get_release_commit "$tag")"; then
error "Failed to resolve the commit for release $tag."
exit 1
fi
if ! target_commit="$(get_target_commit)"; then
error "Failed to resolve the target commit."
exit 1
fi
if [ "$release_commit" != "$target_commit" ]; then
return 1
fi
release_title="$(gh release view "$tag" --json name --jq '.name')"
if [ "$release_title" != "$title" ]; then
return 1
fi
if [ -n "$body" ]; then
release_body="$(gh release view "$tag" --json body --jq '.body')"
if [ "$release_body" != "$body" ]; then
return 1
fi
fi
log "Release $tag is unchanged, skipping."
}
wait_for_tag_removal() {
local tag="$1"
local elapsed=0
local timeout=120
local local_exists
local remote_exists
while :; do
git fetch --tags --prune-tags
local_exists=0
remote_exists=0
if git tag -l | grep -Fxq "$tag"; then
local_exists=1
fi
if git ls-remote --exit-code --tags origin "refs/tags/$tag" >/dev/null 2>&1; then
remote_exists=1
fi
if [ "$local_exists" -eq 0 ] && [ "$remote_exists" -eq 0 ]; then
break
fi
if [ "$elapsed" -ge "$timeout" ]; then
echo "ERROR: Timed out waiting for tag $tag to be deleted." >&2
echo "Local tag exists: $local_exists" >&2
echo "Remote tag exists: $remote_exists" >&2
exit 1
fi
echo "Waiting for tag $tag to be deleted..."
sleep 3
elapsed=$((elapsed + 3))
done
}
delete_release() {
local tag="$1"
log "Release $tag already exists, deleting it first..."
gh release delete "$tag" --cleanup-tag --yes
# Workaround for https://github.com/cli/cli/issues/8458
wait_for_tag_removal "$tag"
}
create_release() {
local tag="$1"
local title="$2"
local body="${INPUT_BODY:-}"
local commit="${INPUT_COMMIT:-}"
local args=()
if [ -n "$commit" ]; then
args+=(--target "$commit")
fi
args+=(--title "$title")
if [ -n "$body" ]; then
args+=(--notes "$body")
else
args+=(--generate-notes)
fi
local latest="${INPUT_LATEST:-}"
if [ -z "$latest" ] || [ "${latest,,}" = "true" ]; then
args+=(--latest)
else
args+=(--latest=false)
fi
log "Creating release $tag..."
gh release create "$tag" "${args[@]}"
}
setup_auth
tag="${INPUT_TAG:-}"
title="${INPUT_TITLE:-}"
if [ -z "$title" ]; then
title="Release"
fi
if [ -z "$tag" ]; then
tag="$(date +%Y%m%d%H%M%S)"
fi
if release_exists "$tag"; then
if release_is_current "$tag" "$title"; then
exit 0
fi
delete_release "$tag"
fi
create_release "$tag" "$title"