Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert "feat: change downloadurl and add fallback on setup action" #150

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 20 additions & 43 deletions setup/setup_snyk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,36 @@ die () {
exit 1
}

# Check if correct number of arguments is provided
[ "$#" -eq 2 ] || die "Setup Snyk requires two arguments, $# provided"
[ "$#" -eq 2 ] || die "Setup Snyk requires two argument, $# provided"

cd "$(mktemp -d)"

echo "Installing the $1 version of Snyk on $2"

VERSION=$1
MAIN_URL="https://downloads.snyk.io/cli"
BACKUP_URL="https://static.snyk.io/cli"
BASE_URL="https://static.snyk.io/cli"
SUDO_CMD="sudo"
GH_ACTIONS="GITHUB_ACTIONS"

# Determine the prefix based on the platform
case "$2" in
Linux) PREFIX=linux ;;
macOS) PREFIX=macos ;;
Alpine) PREFIX=alpine ;;
Windows) die "Windows runner not currently supported" ;;
*) die "Invalid runner specified: $2" ;;
Linux)
PREFIX=linux
;;
Windows)
die "Windows runner not currently supported"
;;
macOS)
PREFIX=macos
;;
Alpine)
PREFIX=alpine
;;
*)
die "Invalid runner specified: $2"
esac

{
echo "#!/bin/bash"
echo export SNYK_INTEGRATION_NAME=\"$GH_ACTIONS\"
echo export SNYK_INTEGRATION_NAME="GITHUB_ACTIONS"
echo export SNYK_INTEGRATION_VERSION=\"setup \(${2}\)\"
echo export FORCE_COLOR=2
echo eval snyk-${PREFIX} \$@
Expand All @@ -57,40 +63,11 @@ fi

chmod +x snyk
${SUDO_CMD} mv snyk /usr/local/bin
# Function to download a file with fallback to backup URL
# Parameters:
# $1: File name to download
# $2: Output file name
download_file() {
# Try to download from the main URL
if curl --compressed --retry 2 --output "$2" "$MAIN_URL/$1?utm_source="$GH_ACTIONS; then
echo "Downloaded from $MAIN_URL/$1"
# If main URL fails, try the backup URL
elif curl --compressed --retry 2 --output "$2" "$BACKUP_URL/$1?utm_source="$GH_ACTIONS; then
echo "Downloaded from $BACKUP_URL/$1"
# If both URLs fail, return an error
else
echo "Failed to download $1 from both URLs"
return 1
fi
}

# Download Snyk binary
if ! download_file "$VERSION/snyk-${PREFIX}" "snyk-${PREFIX}"; then
die "Failed to download Snyk binary"
fi
curl --compressed --retry 2 --output snyk-${PREFIX} "$BASE_URL/$VERSION/snyk-${PREFIX}"
curl --compressed --retry 2 --output snyk-${PREFIX}.sha256 "$BASE_URL/$VERSION/snyk-${PREFIX}.sha256"

# Download SHA256 checksum file
if ! download_file "$VERSION/snyk-${PREFIX}.sha256" "snyk-${PREFIX}.sha256"; then
die "Failed to download SHA256 file"
fi

# Verify the checksum
sha256sum -c snyk-${PREFIX}.sha256

# Make the binary executable
chmod +x snyk-${PREFIX}

# Move the binary to /usr/local/bin
${SUDO_CMD} mv snyk-${PREFIX} /usr/local/bin
rm -rf snyk*