diff --git a/Dockerfile b/Dockerfile index 63fb9ac2..56bc44a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,15 +39,19 @@ RUN apk add --no-cache \ ######################################### # Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7 # Slightly modified to always retrieve latest stable Powershell version -RUN mkdir -p /opt/microsoft/powershell/7 \ - && curl -s https://api.github.com/repos/powershell/powershell/releases/latest \ +# If changing PWSH_VERSION='latest' to a specific version, use format PWSH_VERSION='tags/v7.0.2' +ARG PWSH_VERSION='latest' +ARG PWSH_DIRECTORY='/opt/microsoft/powershell' +ARG PSSA_VERSION='latest' +RUN mkdir -p ${PWSH_DIRECTORY} \ + && curl -s https://api.github.com/repos/powershell/powershell/releases/${PWSH_VERSION} \ | grep browser_download_url \ | grep linux-alpine-x64 \ | cut -d '"' -f 4 \ | xargs -n 1 wget -O - \ - | tar -xzC /opt/microsoft/powershell/7 \ - && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ - && pwsh -c 'install-module psscriptanalyzer -Scope AllUsers -force' + | tar -xzC ${PWSH_DIRECTORY} \ + && ln -s ${PWSH_DIRECTORY}/pwsh /usr/bin/pwsh -f \ + && pwsh -c 'Install-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' ##################### # Run Pip3 Installs # diff --git a/lib/linter.sh b/lib/linter.sh index 0801a9ee..9b6d9dd6 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -82,10 +82,9 @@ CLOJURE_LINTER_RULES="$DEFAULT_RULES_LOCATION/$CLOJURE_FILE_NAME" LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck" "pylint" "perl" "rubocop" "coffeelint" "eslint" "standard" "ansible-lint" "/dockerfilelint/bin/dockerfilelint" "golangci-lint" "tflint" - "stylelint" "dotenv-linter" "powershell" "ktlint" "protolint" "clj-kondo" + "stylelint" "dotenv-linter" "pwsh" "ktlint" "protolint" "clj-kondo" "spectral" "cfn-lint") - ############################# # Language array for prints # ############################# @@ -642,6 +641,48 @@ GetGitHubVars() fi } ################################################################################ +#### Function ValidatePowershellModules ######################################## +function ValidatePowershellModules() +{ + VALIDATE_PSSA_MODULE=$(pwsh -c "(Get-Module -Name PSScriptAnalyzer -ListAvailable | Select-Object -First 1).Name" 2>&1) + # If module found, ensure Invoke-ScriptAnalyzer command is available + if [[ "$VALIDATE_PSSA_MODULE" == "PSScriptAnalyzer" ]]; then + VALIDATE_PSSA_CMD=$(pwsh -c "(Get-Command Invoke-ScriptAnalyzer | Select-Object -First 1).Name" 2>&1) + else + # Failed to find module + exit 1 + fi + + ######################################### + # validate we found the script analyzer # + ######################################### + if [[ "$VALIDATE_PSSA_CMD" != "Invoke-ScriptAnalyzer" ]]; then + # Failed to find module + exit 1 + fi + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + # Failed + echo "ERROR! Failed find module [PSScriptAnalyzer] for [$LINTER_NAME] in system!" + echo "ERROR:[PSSA_MODULE $VALIDATE_PSSA_MODULE] [PSSA_CMD $VALIDATE_PSSA_CMD]" + exit 1 + else + # Success + if [[ "$ACTIONS_RUNNER_DEBUG" == "true" ]]; then + echo "Successfully found module [$VALIDATE_PSSA_MODULE] in system" + echo "Successfully found command [$VALIDATE_PSSA_CMD] in system" + fi + fi +} +################################################################################ #### Function Footer ########################################################### Footer() { @@ -1088,11 +1129,16 @@ fi # POWERSHELL LINTING # ###################### if [ "$VALIDATE_POWERSHELL" == "true" ]; then + ############################################################### + # For POWERSHELL, ensure PSScriptAnalyzer module is available # + ############################################################### + ValidatePowershellModules + ############################# # Lint the powershell files # ############################# # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "POWERSHELL" "pwsh" "pwsh -c Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" + LintCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" fi ################### diff --git a/lib/worker.sh b/lib/worker.sh index fc1d65f1..82aac52b 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -35,9 +35,9 @@ function LintCodebase() PRINT_ARRAY+=("----------------------------------------------") PRINT_ARRAY+=("----------------------------------------------") - ####################################### - # Validate we have jsonlint installed # - ####################################### + ##################################### + # Validate we have linter installed # + ##################################### VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1) ####################### @@ -56,7 +56,7 @@ function LintCodebase() else # Success if [[ "$ACTIONS_RUNNER_DEBUG" == "true" ]]; then - echo "Successfully found binary in system" + echo "Successfully found binary for [$LINTER_NAME] in system" echo "Location:[$VALIDATE_INSTALL_CMD]" fi fi @@ -166,8 +166,8 @@ function LintCodebase() ################################ # Lint the file with the rules # ################################ - # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell - LINT_CMD=$(cd "$GITHUB_WORKSPACE" || exit; $LINTER_COMMAND "$FILE"; exit $? 2>&1) + # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell + LINT_CMD=$(cd "$GITHUB_WORKSPACE" || exit; pwsh -c "($LINTER_COMMAND $FILE)"; exit $? 2>&1) else ################################ # Lint the file with the rules # @@ -333,8 +333,8 @@ function TestCodebase() ################################ # Lint the file with the rules # ################################ - # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell - LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; $LINTER_COMMAND "$FILE"; exit $? 2>&1) + # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell + LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; pwsh -c "($LINTER_COMMAND $FILE)"; exit $? 2>&1) else ################################ # Lint the file with the rules #