generated from tj-actions/docker-action
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
go-install.sh
113 lines (96 loc) · 3.79 KB
/
go-install.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
#!/usr/bin/env bash
set -eou pipefail
TMPDIR=$(mktemp -d)
INPUT_REPOSITORY=$(basename "$INPUT_REPOSITORY")
if [[ "$INPUT_VERSION" == "latest" ]]; then
echo "Downloading the latest release"
# Set the latest release version
VERSION=$(curl --silent -H "Authorization: token $INPUT_TOKEN" "https://api.github.com/repos/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/latest" | jq -r '.tag_name')
# If the version is in the format like "v3", find the latest semver
if [[ $VERSION =~ ^v[0-9]+$ ]]; then
VERSION=$(curl --silent -H "Authorization: token $INPUT_TOKEN" "https://api.github.com/repos/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases" | jq -r '[.[] | .tag_name] | sort | reverse | .[0]')
fi
else
echo "Downloading version $INPUT_VERSION"
VERSION="$INPUT_VERSION"
fi
NAME_VERSION="${VERSION#"v"}"
# Determine the operating system and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
if [[ $OS == "darwin" ]]; then
FILENAME="${INPUT_REPOSITORY}_${NAME_VERSION}_Darwin_x86_64.tar.gz"
URL="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}/${FILENAME}"
SHA256SUM_FILE="checksums.txt"
SHA256SUM_URL="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}/${SHA256SUM_FILE}"
elif [[ $OS == "linux" ]]; then
if [[ $ARCH == "x86_64" ]]; then
FILENAME="${INPUT_REPOSITORY}_${NAME_VERSION}_Linux_x86_64.tar.gz"
URL="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}/${FILENAME}"
SHA256SUM_FILE="checksums.txt"
SHA256SUM_URL="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}/${SHA256SUM_FILE}"
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
elif [[ $OS == *"mingw64"* ]]; then
if [[ $ARCH == "x86_64" ]]; then
FILENAME="${INPUT_REPOSITORY}_${NAME_VERSION}_Windows_x86_64.zip"
URL="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}/${FILENAME}"
SHA256SUM_FILE="checksums.txt"
SHA256SUM_URL="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}/${SHA256SUM_FILE}"
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
else
echo "Unsupported operating system: $OS"
exit 1
fi
OUTPUT_FILE="$TMPDIR/$FILENAME"
SHA256SUM_OUTPUT_FILE="$TMPDIR/$SHA256SUM_FILE"
function download {
local output=$1
local url=$2
for i in $(seq 1 5); do
curl --fail --silent -H "Authorization: token $INPUT_TOKEN" --location --output "$output" "$url" && break
sleep 10
echo "$i retries"
done
}
# Download the binary and its checksum file to the temporary directory
echo "Downloading $URL"
download "$OUTPUT_FILE" "$URL"
echo "Downloading $SHA256SUM_URL"
download "$SHA256SUM_OUTPUT_FILE" "$SHA256SUM_URL"
# Verify the checksum
EXPECTED=$(grep "$FILENAME" "$SHA256SUM_OUTPUT_FILE" | awk '{print $1}')
if [[ $OS == "darwin" ]]; then
ACTUAL=$(shasum -a 256 "$OUTPUT_FILE" | awk '{print $1}')
elif [[ $OS == "linux" ]]; then
ACTUAL=$(sha256sum "$OUTPUT_FILE" | awk '{print $1}')
elif [[ $OS == *"mingw64"* ]]; then
ACTUAL=$(sha256sum "$OUTPUT_FILE" | awk '{print $1}')
else
echo "Unsupported operating system: $OS"
exit 1
fi
if [[ "$EXPECTED" != "$ACTUAL" ]]; then
echo "Checksum verification failed"
exit 1
fi
# Extract the binary in the temporary directory
if [[ $OS == "darwin" ]]; then
tar -xzf "$OUTPUT_FILE" -C "$TMPDIR"
elif [[ $OS == "linux" ]]; then
tar -xzf "$OUTPUT_FILE" -C "$TMPDIR"
elif [[ $OS == *"mingw64"* ]]; then
unzip "$OUTPUT_FILE" -d "$TMPDIR"
else
echo "Unsupported operating system: $OS"
exit 1
fi
# Make the binary executable
chmod +x "$TMPDIR"/"$INPUT_REPOSITORY"
# Return the binary path
echo "binary_path=$TMPDIR/$INPUT_REPOSITORY" >> "$GITHUB_OUTPUT"