aboutsummaryrefslogtreecommitdiffstats
path: root/.github/actions/release/upload
blob: 758fc1bc4b729d010708456617daf48c70ca6b34 (plain) (blame)
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
#!/bin/bash
# SPDX

if [[ -z "$GITHUB_TOKEN" ]]; then
	echo "missing GITHUB_TOKEN"
	exit 1
fi

if [[ -z "$GITHUB_REPO" ]] ; then
	echo "missing GITHUB_REPO"
	exit 1
fi

if [[ -z "$REF" ]] ; then
	echo "missing REF"
	exit 1
fi

if [[ -z "$COMMIT" ]] ; then
	echo "missing COMMIT"
	exit 1
fi

TAG="ci-release"

echo $REF
if [[ $REF = refs/heads/* ]] ; then
	branch="$(echo $REF | cut -d/ -f 3)"
	body="CI build of branch $branch. These binaries are for testing purposes. They are not signed and installations on Mac and Android will create warnings and errors without some extra work."
	branch="-$branch"
	echo "it's refs/heads with an added $branch"
	if [[ $REF != refs/heads/master ]] ; then
		TAG="${TAG}${branch}"
	fi
elif [[ $REF = refs/tags/* ]] ; then
	TAG=$(echo $REF | cut -d/ -f 3)
	body="Release build ($TAG)"
fi

# check if there is a tag of that name
# if it is starts with ci (so this is a continous integration tag) and it's on a different SHA, delete it
tag_url="https://api.github.com/repos/$GITHUB_REPO/git/refs/tags/$TAG"
release_url="https://api.github.com/repos/$GITHUB_REPO/releases/tags/$TAG"

echo "get tag infos: curl -XGET --header \"Authorization: token xxxx\" \"${tag_url}\""
tag_infos=$(curl -XGET --header "Authorization: token ${GITHUB_TOKEN}" "${tag_url}")
echo "information received for tag $TAG"
echo $tag_infos
existing_tag_sha=$(echo $tag_infos | jq --raw-output .object.sha)

need_new_release="0"
if [[ "$existing_tag_sha" != "null" ]] ; then
	echo "existing tag on SHA $existing_tag_sha"
	existing_release=$(curl -XGET --header "Authorization: token ${GITHUB_TOKEN}" "${release_url}")
	release_id=$(echo $existing_release | jq --raw-output .id )
	echo "information received for the release with release ID \"$release_id\""
	echo $existing_release
	if [[ "$release_id" == "null" ]] ; then
		need_new_release="1"
	fi

	if [[ "$existing_tag_sha" != "$COMMIT" ]] ; then
		need_new_release="1"
		echo "tag was on different SHA, delete it and the corresponding release (if it exists)"
		echo "deleting tag $TAG"
		curl -XDELETE --header "Authorization: token ${GITHUB_TOKEN}" "${tag_url}"
		if [[ "$release_id" != "null" ]] ; then
			echo "Delete the release $release_id"
			delete_url="https://api.github.com/repos/$GITHUB_REPO/releases/$release_id"
			curl -XDELETE --header "Authorization: token ${GITHUB_TOKEN}" "${delete_url}"
		fi
	fi
else
	echo "this is a new tag"
	need_new_release="1"
fi

if [[ "$need_new_release" = "1" ]] ; then
	echo "create a new release and implicitly a new tag"
	release=$(curl -H "Authorization: token ${GITHUB_TOKEN}" --data '{"tag_name": "'"$TAG"'","target_commitish": "'"$COMMIT"'","name": "'"$TAG"'","body": "'"$body"'","draft": false,"prerelease": true}' "https://api.github.com/repos/$GITHUB_REPO/releases")
	echo "response to release creation"
	echo "$release"
fi

# get the upload URL
release_info=$(curl -XGET --header "Authorization: token ${GITHUB_TOKEN}" "${release_url}")
echo "release info for $TAG"
echo $release_info
upload_url=$(echo $release_info | jq --raw-output .upload_url | cut -d '{' -f 1)

if [[ "$upload_url" = "null" ]] ; then
	echo "error determining release upload URL, aborting"
	exit 1
fi

# accept up to 9 binaries via environment variables
for FILENAME in $BIN1 $BIN2 $BIN3 $BIN4 $BIN5 $BIN6 $BIN7 $BIN8 $BIN9
do
	if [[ -z "$FILENAME" ]] ; then
		break
	fi
	if [[ ! -f $FILENAME ]] ; then
		echo "Cannot find binary $FILENAME"
		continue
	fi
	echo "upload $FILENAME to release"
	BASENAME="$(basename $FILENAME)"
	echo "curl -H \"Authorization: token xxxxx\" -H \"Accept: application/vnd.github.manifold-preview\" -H \"Content-Type: application/octet-stream\" --data-binary @$FILENAME \"$upload_url?name=$BASENAME\""
	curl -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.manifold-preview" -H "Content-Type: application/octet-stream" --data-binary @$FILENAME "$upload_url?name=$BASENAME"
done

# vim: tw=0