From a9c2193f873a356daa25d9e2377ef9ab680e4b2b Mon Sep 17 00:00:00 2001 From: Antonio Aversa Date: Wed, 27 Nov 2024 09:12:49 +0100 Subject: [PATCH] Add fallback mechanism --- .github/workflows/qa.yml | 39 ++++++++++++++++++++++++++++++++---- install-sonar-scanner-cli.sh | 38 +++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml index a33dc1f..8b4240a 100644 --- a/.github/workflows/qa.yml +++ b/.github/workflows/qa.yml @@ -269,8 +269,8 @@ jobs: - name: Assert run: | ./test/assertFileExists ./test/example-project/.scannerwork/report-task.txt - dontFailOnMissingWget: - name: Don't fail on missing wget + dontFailWhenMissingWgetButCurlAvailable: + name: Don't fail when missing wget but curl available runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -295,8 +295,8 @@ jobs: - name: Assert run: | ./test/assertFileExists ./output.properties - failOnMissingCurl: - name: Fail on missing curl + dontFailWhenMissingCurlButWgetAvailable: + name: Don't fail when missing curl but wget available runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -310,6 +310,37 @@ jobs: then exit 1 fi + - name: Run action + id: runTest + uses: ./ + env: + NO_CACHE: true + SONAR_HOST_URL: http://not_actually_used + SONAR_SCANNER_JSON_PARAMS: '{"sonar.scanner.internal.dumpToFile": "./output.properties"}' + with: + args: -Dsonar.scanner.internal.dumpToFile=./output.properties + - name: Assert + run: | + ./test/assertFileExists ./output.properties + failWhenBothWgetAndCurlMissing: + name: Fail when both wget and curl are missing + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - name: Remove wget and curl + run: sudo apt-get remove -y wget curl + - name: Assert wget and curl are not available + run: | + if command -v wget 2>&1 >/dev/null + then + exit 1 + fi + if command -v curl 2>&1 >/dev/null + then + exit 1 + fi - name: Run action id: runTest uses: ./ diff --git a/install-sonar-scanner-cli.sh b/install-sonar-scanner-cli.sh index 7e9c755..d87c8e4 100755 --- a/install-sonar-scanner-cli.sh +++ b/install-sonar-scanner-cli.sh @@ -8,31 +8,47 @@ set -eou pipefail # - INPUT_SCANNERVERSION: e.g. 6.2.1.4610 # - INPUT_SCANNERBINARIESURL: e.g. https://github.com/me/my-repo/raw/refs/heads/main/binaries -CURL=curl -if [[ "$RUNNER_OS" == "Linux" && "$RUNNER_ARCH" == "X64" ]]; then +if [[ "$RUNNER_OS" == "Linux" && "$RUNNER_ARCH" == "X64" ]]; then FLAVOR="linux-x64" -elif [[ "$RUNNER_OS" == "Linux" && "$RUNNER_ARCH" == "ARM64" ]]; then +elif [[ "$RUNNER_OS" == "Linux" && "$RUNNER_ARCH" == "ARM64" ]]; then FLAVOR="linux-aarch64" -elif [[ "$RUNNER_OS" == "Windows" && "$RUNNER_ARCH" == "X64" ]]; then +elif [[ "$RUNNER_OS" == "Windows" && "$RUNNER_ARCH" == "X64" ]]; then FLAVOR="windows-x64" - CURL="C:\\msys64\\usr\\bin\\curl.exe" -elif [[ "$RUNNER_OS" == "macOS" && "$RUNNER_ARCH" == "X64" ]]; then +elif [[ "$RUNNER_OS" == "macOS" && "$RUNNER_ARCH" == "X64" ]]; then FLAVOR="macosx-x64" -elif [[ "$RUNNER_OS" == "macOS" && "$RUNNER_ARCH" == "ARM64" ]]; then +elif [[ "$RUNNER_OS" == "macOS" && "$RUNNER_ARCH" == "ARM64" ]]; then FLAVOR="macosx-aarch64" else - echo "$RUNNER_OS $RUNNER_ARCH not supported" + echo "::error title=SonarScanner::$RUNNER_OS $RUNNER_ARCH not supported" exit 1 fi +SCANNER_FILE_NAME="sonar-scanner-cli-$INPUT_SCANNERVERSION-$FLAVOR.zip" +SCANNER_URI="${INPUT_SCANNERBINARIESURL%/}/$SCANNER_FILE_NAME" + +if command -v wget &> /dev/null; then + DOWNLOAD_COMMAND="wget" + DOWNLOAD_ARGS="--no-verbose --user-agent=sonarqube-scan-action $SCANNER_URI" +elif command -v curl &> /dev/null; then + DOWNLOAD_COMMAND="curl" + DOWNLOAD_ARGS="--silent --show-error --user-agent sonarqube-scan-action --output $SCANNER_FILE_NAME $SCANNER_URI" +elif [ "$RUNNER_OS" == "Windows" ] && [ -t "C:\\msys64\\usr\\bin\\wget.exe" ]; then + DOWNLOAD_COMMAND="C:\\msys64\\usr\\bin\\wget.exe" + DOWNLOAD_ARGS="--no-verbose --user-agent=sonarqube-scan-action $SCANNER_URI" +elif [ "$RUNNER_OS" == "Windows" ] && [ -t "C:\\msys64\\usr\\bin\\curl.exe" ]; then + DOWNLOAD_COMMAND="C:\\msys64\\usr\\bin\\curl.exe" + DOWNLOAD_ARGS="--silent --show-error --user-agent sonarqube-scan-action --output $SCANNER_FILE_NAME $SCANNER_URI" +else + echo "::error title=SonarScanner::Neither wget nor curl found on the machine" + exit 1 +fi + set -x mkdir -p $RUNNER_TEMP/sonarscanner cd $RUNNER_TEMP/sonarscanner -SCANNER_FILE_NAME="sonar-scanner-cli-$INPUT_SCANNERVERSION-$FLAVOR.zip" -SCANNER_URI="${INPUT_SCANNERBINARIESURL%/}/$SCANNER_FILE_NAME" -$CURL --user-agent "sonarqube-scan-action" --output $SCANNER_FILE_NAME $SCANNER_URI +$DOWNLOAD_COMMAND $DOWNLOAD_ARGS unzip -q $SCANNER_FILE_NAME