Skip to content

Commit 10dcd70

Browse files
authored
Use GH token in installer (#638)
* Use GH token in installer * run CI * avoid GH rate limits using GH_TOKEN * fix auth header * add DEFANG_INSTALL_VERSION test
1 parent fce569d commit 10dcd70

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

.github/workflows/install.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
types: [published]
66
push:
77
branches:
8-
- main
8+
- "**"
99
paths:
1010
- '.github/workflows/install.yml'
1111
- 'src/bin/install'
@@ -15,8 +15,19 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- name: Install defang
18+
- name: Install defang (latest)
1919
run: . <(curl -Ls https://s.defang.io/install)
20+
env:
21+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # avoid rate limiting
22+
23+
- name: Sanity check
24+
run: defang --version
25+
26+
- name: Install defang (specific version)
27+
run: . <(curl -Ls https://s.defang.io/install)
28+
env:
29+
DEFANG_INSTALL_VERSION: v0.5.36
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # alt name
2031

2132
- name: Sanity check
2233
run: defang --version

src/bin/install

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ fi
2828
if [[ -z "$DEFANG_INSTALL_VERSION" ]]; then
2929
RELEASE_PATH="latest"
3030
else
31-
RELEASE_PATH="tags/$DEFANG_INSTALL_VERSION"
31+
RELEASE_PATH="tags/v${DEFANG_INSTALL_VERSION#v}"
3232
fi
3333

34-
# Echo fetching the release path either latest or strip the tags/ from the version
35-
echo "Fetching the ${DEFANG_INSTALL_VERSION:-latest} release of defang..."
36-
RELEASE_JSON=$(curl -s -f -L https://api.github.com/repos/DefangLabs/defang/releases/$RELEASE_PATH)
34+
# Anonymous API request to GitHub are rate limited to 60 requests per hour.
35+
# Check whether the user has set a GitHub token to increase the rate limit.
36+
AUTH_HEADER=""
37+
if [[ -n "$GITHUB_TOKEN" ]]; then
38+
AUTH_HEADER="Authorization: Bearer $GITHUB_TOKEN"
39+
elif [[ -n "$GH_TOKEN" ]]; then
40+
AUTH_HEADER="Authorization: Bearer $GH_TOKEN"
41+
fi
42+
43+
# Echo fetching the release path either latest or the version
44+
echo "Fetching the ${RELEASE_PATH#tags/} release of defang..."
45+
# Download the release information from GitHub, using the token if available, but falling back to anonymous access
46+
RELEASE_JSON=$([[ -n "$AUTH_HEADER" ]] &&
47+
curl -sfL -H "$AUTH_HEADER" https://api.github.com/repos/DefangLabs/defang/releases/$RELEASE_PATH ||
48+
curl -sfL https://api.github.com/repos/DefangLabs/defang/releases/$RELEASE_PATH)
3749

3850
# Check for curl failure
3951
if [ -z "$RELEASE_JSON" ]; then
@@ -80,7 +92,7 @@ elif [ "$OS" = "Linux" ]; then
8092
fi
8193

8294
# Download the file
83-
if ! curl -s -f -L "$DOWNLOAD_URL" -o "$FILENAME"; then
95+
if ! curl -sfL "$DOWNLOAD_URL" -o "$FILENAME"; then
8496
echo "Download failed. Please check your internet connection and try again."
8597
return 4
8698
fi
@@ -239,5 +251,5 @@ rm "$FILENAME"
239251
echo "Installation completed. You can now use defang by typing '$BINARY_NAME' in the terminal."
240252

241253
# Unset the variables and functions to avoid polluting the user's environment
242-
unset EXTRACT_DIR DOWNLOAD_URL RELEASE_JSON RELEASE_PATH ARCH_SUFFIX ARCH OS FILENAME INSTALL_DIR BINARY_NAME REPLY EXPORT_PATH CURRENT_SHELL FOUND_PROFILE_FILE
254+
unset EXTRACT_DIR DOWNLOAD_URL RELEASE_JSON RELEASE_PATH ARCH_SUFFIX ARCH OS FILENAME INSTALL_DIR BINARY_NAME REPLY EXPORT_PATH CURRENT_SHELL FOUND_PROFILE_FILE AUTH_HEADER
243255
unset -f _prompt_and_append_to_file _generate_completion_script _install_completion_script

0 commit comments

Comments
 (0)