From f2b0375d5b892828f19f203ea4b47decce34a466 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Sat, 28 Jan 2023 12:00:40 +0100 Subject: [PATCH] Using GITHUB_TOKEN for API --- .github/workflows/push.yml | 1 + .github/workflows/release.yml | 1 + build-latest.sh | 34 +++++++++++++++++----------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index acd08d4..2bf855b 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -55,6 +55,7 @@ jobs: fail-fast: false env: GH_ACTION: enable + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} IMAGE_NAMES: docker.io/netboxcommunity/netbox runs-on: ubuntu-latest name: Builds new NetBox Docker Images diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3b622ea..86733e3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,6 +25,7 @@ jobs: name: Builds new NetBox Docker Images env: GH_ACTION: enable + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} IMAGE_NAMES: docker.io/netboxcommunity/netbox quay.io/netboxcommunity/netbox ghcr.io/netbox-community/netbox steps: - id: source-checkout diff --git a/build-latest.sh b/build-latest.sh index bcafda4..00abe97 100755 --- a/build-latest.sh +++ b/build-latest.sh @@ -11,16 +11,20 @@ if ! command -v jq; then exit 1 fi +CURL_ARGS=( + --silent +) + ### -# Checking for the presence of GITHUB_OAUTH_CLIENT_ID -# and GITHUB_OAUTH_CLIENT_SECRET +# Checking for the presence of GITHUB_TOKEN ### -if [ -n "${GITHUB_OAUTH_CLIENT_ID}" ] && [ -n "${GITHUB_OAUTH_CLIENT_SECRET}" ]; then +if [ -n "${GITHUB_TOKEN}" ]; then echo "🗝 Performing authenticated Github API calls." - GITHUB_OAUTH_PARAMS="client_id=${GITHUB_OAUTH_CLIENT_ID}&client_secret=${GITHUB_OAUTH_CLIENT_SECRET}" + CURL_ARGS+=( + --header "Authorization: Bearer ${GITHUB_TOKEN}" + ) else echo "🕶 Performing unauthenticated Github API calls. This might result in lower Github rate limits!" - GITHUB_OAUTH_PARAMS="" fi ### @@ -42,31 +46,27 @@ fi ### ORIGINAL_GITHUB_REPO="netbox-community/netbox" GITHUB_REPO="${GITHUB_REPO-$ORIGINAL_GITHUB_REPO}" -URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/releases?${GITHUB_OAUTH_PARAMS}" +URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/releases" # Composing the JQ commans to extract the most recent version number JQ_LATEST="group_by(.prerelease) | .[] | sort_by(.published_at) | reverse | .[0] | select(.prerelease==${PRERELEASE-false}) | .tag_name" -CURL="curl -sS" +CURL="curl" # Querying the Github API to fetch the most recent version number -VERSION=$($CURL "${URL_RELEASES}" | jq -r "${JQ_LATEST}") +VERSION=$($CURL "${CURL_ARGS[@]}" "${URL_RELEASES}" | jq -r "${JQ_LATEST}" 2>/dev/null) ### # Check if the prerelease version is actually higher than stable version ### if [ "${PRERELEASE}" == "true" ]; then JQ_STABLE="group_by(.prerelease) | .[] | sort_by(.published_at) | reverse | .[0] | select(.prerelease==false) | .tag_name" - STABLE_VERSION=$($CURL "${URL_RELEASES}" | jq -r "${JQ_STABLE}") + STABLE_VERSION=$($CURL "${CURL_ARGS[@]}" "${URL_RELEASES}" | jq -r "${JQ_STABLE}" 2>/dev/null) - # shellcheck disable=SC2003 - MAJOR_STABLE=$(expr match "${STABLE_VERSION}" 'v\([0-9]\+\)') - # shellcheck disable=SC2003 - MINOR_STABLE=$(expr match "${STABLE_VERSION}" 'v[0-9]\+\.\([0-9]\+\)') - # shellcheck disable=SC2003 - MAJOR_UNSTABLE=$(expr match "${VERSION}" 'v\([0-9]\+\)') - # shellcheck disable=SC2003 - MINOR_UNSTABLE=$(expr match "${VERSION}" 'v[0-9]\+\.\([0-9]\+\)') + MAJOR_STABLE=$(expr "${STABLE_VERSION}" : 'v\([0-9]\+\)') + MINOR_STABLE=$(expr "${STABLE_VERSION}" : 'v[0-9]\+\.\([0-9]\+\)') + MAJOR_UNSTABLE=$(expr "${VERSION}" : 'v\([0-9]\+\)') + MINOR_UNSTABLE=$(expr "${VERSION}" : 'v[0-9]\+\.\([0-9]\+\)') if { [ "${MAJOR_STABLE}" -eq "${MAJOR_UNSTABLE}" ] &&