Skip to content

Commit 76b30ac

Browse files
committed
feat(docker): Allow authenticated calls to GitHub API
Accept build arg `GITHUB_TOKEN` to authenticate calls made to GitHub API in `common::install_from_gh_release` function. Closes #946
1 parent 3e855bb commit 76b30ac

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ RUN if [ "$INSTALL_ALL" != "false" ]; then \
6565
echo "TRIVY_VERSION=latest" >> /.env \
6666
; fi
6767

68+
ARG GITHUB_TOKEN=""
69+
ENV GITHUB_TOKEN=${GITHUB_TOKEN}
70+
6871
# Docker `RUN`s shouldn't be consolidated here
6972
# hadolint global ignore=DL3059
7073
RUN /install/opentofu.sh

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ docker build -t pre-commit-terraform \
136136

137137
Set `-e PRE_COMMIT_COLOR=never` to disable the color output in `pre-commit`.
138138

139+
> [!NOTE]
140+
> The build install scripts are calling the GitHub API to resolve the release URL. If you need to authenticate those calls, you can pass a GitHub token (the `GITHUB_TOKEN` environment variable is expected to be set with an [access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)):
141+
> `docker build -t pre-commit-terraform --build-arg GITHUB_TOKEN .`
142+
139143
</details>
140144

141145

tools/install/_common.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ function common::install_from_gh_release {
5858
;;
5959
esac
6060

61+
set -eux
62+
6163
# Download tool
6264
local -r RELEASES="https://api.github.com/repos/${GH_ORG}/${TOOL}/releases"
65+
local CURL_OPTS=()
66+
67+
[[ $GITHUB_TOKEN ]] && CURL_OPTS+=('-H' "Authorization: Bearer $GITHUB_TOKEN")
68+
69+
local -r CURL_CMD=("curl" "${CURL_OPTS[@]}")
6370

6471
if [[ $VERSION == latest ]]; then
65-
curl -L "$(curl -s "${RELEASES}/latest" | grep -o -E -i -m 1 "$GH_RELEASE_REGEX_LATEST")" > "$PKG"
72+
"${CURL_CMD[@]}" -L "$("${CURL_CMD[@]}" -s "${RELEASES}/latest" | grep -o -E -i -m 1 "$GH_RELEASE_REGEX_LATEST")" > "$PKG"
6673
else
67-
curl -L "$(curl -s "$RELEASES" | grep -o -E -i -m 1 "$GH_RELEASE_REGEX_SPECIFIC_VERSION")" > "$PKG"
74+
"${CURL_CMD[@]}" -L "$("${CURL_CMD[@]}" -s "$RELEASES" | grep -o -E -i -m 1 "$GH_RELEASE_REGEX_SPECIFIC_VERSION")" > "$PKG"
6875
fi
6976

7077
# Make tool ready to use

0 commit comments

Comments
 (0)