~aleteoryx/muditaos

ref: 441b9b4667d284698f585f9302eace456861a73f muditaos/tools/upload_release.sh -rwxr-xr-x 2.2 KiB
441b9b46 — Maciej Janicki [BH-715] Add switches handling 4 years ago
                                                                                
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
#!/bin/bash
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

# base taken from: https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 and modified
# modman: Adam
# license: mit

set -eou pipefail

for line in "$@"; do
  eval "$line"
done

[ -v "tag"  ]  ||   { echo "script requires tag as parameter i.e. latest release tag"; exit 1; }
[ -v "token" ] ||  { echo "script requires token as parameter to access server (please see GitHub tags)"; exit 1; }
[ -v "files" ] ||  { echo "script requires files as parameters to know what files to upload to server"; exit 1; }
[ -v "repo" ]  ||  { echo "script requires repo to push to"; exit 1; }

github_api_token="$token"
filenames=( $(echo $files | tr "," " ") )

for file in "${filenames[@]}"; do
    [ -f "$file" ] || { echo "file '$file' doesn't exist!"; exit 1; }
done

# Define variables.
GH_REPO="https://api.github.com/repos/mudita/$repo"
GH_TAGS="$GH_REPO/releases/tags/$tag"
GH_RELEASE="$GH_REPO/releases"
AUTH="Authorization: token $github_api_token"

echo "test token"
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!";  exit 1; }

echo "create release"
curl -sH "$AUTH" -H "Accept: application/vnd.github.v3+json" -X POST $GH_RELEASE -d '{"tag_name":"'"$tag"'", "prerelease":true, "name":"'"$tag"'","body":"WARNING: this is daily prerelease"}' &>/dev/null

response=$(curl -sH "$AUTH" $GH_TAGS)
eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }

echo "release ID : ${id}"


for filename in "${filenames[@]}"; do
    GH_ASSET="https://uploads.github.com/repos/mudita/$repo/releases/$id/assets?name=$(basename $filename)"
    [[ "$filename" =~ .*.tar ]] && content_type='Content-Type: application/x-tar' || content_type='Content-Type: application/x-xz'
    curl -s --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "$content_type" $GH_ASSET  &>/dev/null
    echo "asset $filename added to release under: tag $tag"
done

echo "uploading files done!"