forked from dsaltares/fetch-gh-release-asset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_github_asset.sh
executable file
·62 lines (51 loc) · 1.42 KB
/
fetch_github_asset.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
#!/bin/bash
if [[ -z "$INPUT_FILE" ]]; then
echo "Missing file input in the action"
exit 1
fi
if [[ -z "$GITHUB_REPOSITORY" ]]; then
echo "Missing GITHUB_REPOSITORY env variable"
exit 1
fi
REPO=$GITHUB_REPOSITORY
if ! [[ -z ${INPUT_REPO} ]]; then
REPO=$INPUT_REPO
fi
# Optional target file path
TARGET=$INPUT_FILE
if ! [[ -z ${INPUT_TARGET} ]]; then
TARGET=$INPUT_TARGET
fi
# Optional personal access token for external repository
TOKEN=$GITHUB_TOKEN
if ! [[ -z ${INPUT_TOKEN} ]]; then
TOKEN=$INPUT_TOKEN
fi
API_URL="https://api.github.com/repos/$REPO"
RELEASE_DATA=$(curl -H "Authorization: token $TOKEN" $API_URL/releases/${INPUT_VERSION})
MESSAGE=$(echo $RELEASE_DATA | jq -r ".message")
if [[ "$MESSAGE" == "Not Found" ]]; then
echo "[!] Release asset not found"
echo "Release data: $RELEASE_DATA"
echo "-----"
echo "repo: $REPO"
echo "asset: $INPUT_FILE"
echo "target: $TARGET"
echo "version: $INPUT_VERSION"
exit 1
fi
ASSET_ID=$(echo $RELEASE_DATA | jq -r ".assets | map(select(.name == \"${INPUT_FILE}\"))[0].id")
TAG_VERSION=$(echo $RELEASE_DATA | jq -r ".tag_name" | sed -e "s/^v//" | sed -e "s/^v.//")
if [[ -z "$ASSET_ID" ]]; then
echo "Could not find asset id"
exit 1
fi
curl \
-J \
-L \
-H "Accept: application/octet-stream" \
-H "Authorization: token $TOKEN" \
"$API_URL/releases/assets/$ASSET_ID" \
--create-dirs \
-o ${TARGET}
echo "::set-output name=version::$TAG_VERSION"