|
| 1 | +name: Code Scan with Mend CLI |
| 2 | +description: Run Mend SAST security scanning on your codebase |
| 3 | + |
| 4 | +inputs: |
| 5 | + mend-app-name: |
| 6 | + description: "Mend Application Name" |
| 7 | + required: true |
| 8 | + |
| 9 | + mend-project-name: |
| 10 | + description: "Mend Project Name" |
| 11 | + required: true |
| 12 | + |
| 13 | + mend-api-key: |
| 14 | + description: "Mend API Key (pass from secrets)" |
| 15 | + required: true |
| 16 | + |
| 17 | + mend-user: |
| 18 | + description: "Mend User Email (pass from secrets)" |
| 19 | + required: true |
| 20 | + |
| 21 | + scan-name: |
| 22 | + description: "Name of the scan" |
| 23 | + required: false |
| 24 | + default: "" |
| 25 | + |
| 26 | + update: |
| 27 | + description: "Update results to Mend (true/false)" |
| 28 | + required: false |
| 29 | + default: "true" |
| 30 | + |
| 31 | + target-directory: |
| 32 | + description: "Target directory to scan, relative to GitHub workspace" |
| 33 | + required: false |
| 34 | + default: "" |
| 35 | + |
| 36 | + log-level: |
| 37 | + description: "Log level (INFO, DEBUG, WARNING, ERROR)" |
| 38 | + required: false |
| 39 | + default: "INFO" |
| 40 | + |
| 41 | + secrets-detection: |
| 42 | + description: "Enable secrets detection (true/false)" |
| 43 | + required: false |
| 44 | + default: "true" |
| 45 | + |
| 46 | + incremental-scan: |
| 47 | + description: "Enable incremental scan (true/false)" |
| 48 | + required: false |
| 49 | + default: ${{ github.event_name == 'pull_request' }} |
| 50 | + |
| 51 | + github-upload: |
| 52 | + description: "Upload results to GitHub Code Scanning Alerts (true/false)" |
| 53 | + required: false |
| 54 | + default: "true" |
| 55 | + |
| 56 | + enabled-engines: |
| 57 | + description: | |
| 58 | + **Recommended:** Comma-separated list of SAST engine IDs (e.g., '101,108' for Java and JavaScript). Omit for auto-recognition. |
| 59 | + Useful ones: Ruby(5), C/C++(12), Go(18), Java(101), Python(104), JavaScript(108) |
| 60 | + [See documentation for complete list](https://docs.mend.io/platform/latest/configure-the-mend-cli-for-sast#ConfiguretheMendCLIforSAST-Mend-CLI-SAST-supported-languages-and-engine-IDsMendCLISAST-supportedlanguagesandengineIDs) |
| 61 | + required: false |
| 62 | + default: "" |
| 63 | + |
| 64 | +runs: |
| 65 | + using: "composite" |
| 66 | + steps: |
| 67 | + - name: Install Mend CLI |
| 68 | + shell: bash |
| 69 | + env: |
| 70 | + ARCH: ${{ contains(fromJson('["X86", "X64"]'), runner.arch) && 'amd64' || 'arm64' }} |
| 71 | + run: | |
| 72 | + CLI_URL="https://downloads.mend.io/cli/linux_${ARCH}/mend" |
| 73 | + echo "📥 Downloading Mend CLI from ${CLI_URL}" |
| 74 | + curl -fsSL "${CLI_URL}" -o /tmp/mend |
| 75 | + sudo mv /tmp/mend /usr/local/bin/mend |
| 76 | + sudo chmod +x /usr/local/bin/mend |
| 77 | + mend version |
| 78 | +
|
| 79 | + - name: Run Mend Code Scan |
| 80 | + shell: bash |
| 81 | + id: scan |
| 82 | + env: |
| 83 | + # Authentication variables |
| 84 | + MEND_USER_KEY: ${{ inputs.mend-api-key }} |
| 85 | + MEND_EMAIL: ${{ inputs.mend-user }} |
| 86 | + MEND_URL: https://saas-eu.mend.io |
| 87 | + |
| 88 | + # Scope |
| 89 | + MEND_APP: ${{ inputs.mend-app-name }} |
| 90 | + MEND_PROJ: ${{ inputs.mend-project-name }} |
| 91 | + |
| 92 | + # Scan configuration |
| 93 | + MEND_SAST_TARGET_DIRECTORY: ${{ github.workspace }}/${{ inputs.target-directory }} |
| 94 | + MEND_LOG_LEVEL: ${{ inputs.log-level }} |
| 95 | + MEND_SAST_ENGINES: ${{ inputs.enabled-engines }} |
| 96 | + |
| 97 | + MEND_SAST_TIMEOUT_TOTAL: "3600" |
| 98 | + MEND_SAST_SCAN_RETRIES: "3" |
| 99 | + |
| 100 | + run: | |
| 101 | + mend_args=( |
| 102 | + "--non-interactive" |
| 103 | + # Mend Organization in scope is assumed from API key |
| 104 | + "--scope" "${MEND_APP}//${MEND_PROJ}" |
| 105 | + ) |
| 106 | +
|
| 107 | + if [[ -n "${{ inputs.scan-name }}" ]]; then |
| 108 | + mend_args+=("--name" "${{ inputs.scan-name }}") |
| 109 | + fi |
| 110 | +
|
| 111 | + if [[ "${{ inputs.update }}" != "true" ]]; then |
| 112 | + mend_args+=("--no-logs") |
| 113 | + fi |
| 114 | +
|
| 115 | + if [[ "${{ inputs.secrets-detection }}" == "true" ]]; then |
| 116 | + mend_args+=("--secrets-detection") |
| 117 | + fi |
| 118 | +
|
| 119 | + if [[ "${{ inputs.incremental-scan }}" == "true" ]]; then |
| 120 | + mend_args+=("--inc") |
| 121 | + fi |
| 122 | +
|
| 123 | + if [[ "${{ inputs.github-upload }}" == "true" ]]; then |
| 124 | + mend_args+=( |
| 125 | + "--report" |
| 126 | + # Format extension is appended by CLI |
| 127 | + "--filename" "mend" |
| 128 | + "--formats" "sarif" |
| 129 | + ) |
| 130 | + fi |
| 131 | +
|
| 132 | + set +e |
| 133 | + mend sast "${mend_args[@]}" |
| 134 | + exit_code=$? |
| 135 | +
|
| 136 | + # Save exit code for next step |
| 137 | + echo "app-name=${MEND_APP}" >> $GITHUB_OUTPUT |
| 138 | + echo "proj-name=${MEND_PROJ}" >> $GITHUB_OUTPUT |
| 139 | + echo "mend-exit=$exit_code" >> $GITHUB_OUTPUT |
| 140 | + echo "cli-args=${mend_args[*]}" >> $GITHUB_OUTPUT |
| 141 | +
|
| 142 | + # Export options not exposed via CLI args |
| 143 | + echo "engines=${MEND_SAST_ENGINES}" >> $GITHUB_OUTPUT |
| 144 | +
|
| 145 | +
|
| 146 | + - name: Upload SARIF Report |
| 147 | + if: ${{ inputs.github-upload == 'true' && steps.scan.outputs.mend-exit != '' }} |
| 148 | + uses: github/codeql-action/upload-sarif@v4 |
| 149 | + with: |
| 150 | + sarif_file: 'mend.sarif' |
| 151 | + category: 'Mend SAST' |
| 152 | + |
| 153 | + - name: Process Scan Results |
| 154 | + shell: bash |
| 155 | + env: |
| 156 | + MEND_APP: "${{ steps.scan.outputs.app-name }}" |
| 157 | + MEND_PROJ: "${{ steps.scan.outputs.proj-name }}" |
| 158 | + CLI_ARGS: "${{ steps.scan.outputs.cli-args }}" |
| 159 | + SCAN_EXIT_CODE: "${{ steps.scan.outputs.mend-exit }}" |
| 160 | + |
| 161 | + ENABLED_ENGINES: "${{ steps.scan.outputs.engines }}" |
| 162 | + MEND_ENGINES_DOC_URL: "https://docs.mend.io/platform/latest/configure-the-mend-cli-for-sast#ConfiguretheMendCLIforSAST-Mend-CLI-SAST-supported-languages-and-engine-IDsMendCLISAST-supportedlanguagesandengineIDs" |
| 163 | + run: | |
| 164 | + printf '## 🔍 SAST Scan for %s//%s\n' "${MEND_APP}" "${MEND_PROJ}" >> $GITHUB_STEP_SUMMARY |
| 165 | + printf '\n\n' >> $GITHUB_STEP_SUMMARY |
| 166 | + printf '**Run Arguments:** `%s`\n' "${CLI_ARGS}" >> $GITHUB_STEP_SUMMARY |
| 167 | + printf '**Enabled [Engine IDs (ref. docs)](%s):** %s\n' "${MEND_ENGINES_DOC_URL}" "${ENABLED_ENGINES}" >> $GITHUB_STEP_SUMMARY |
| 168 | + printf '\n\n' >> $GITHUB_STEP_SUMMARY |
| 169 | +
|
| 170 | + case "${{ steps.scan.outputs.mend-exit }}" in |
| 171 | + 0) |
| 172 | + echo "### ✅ Success" >> $GITHUB_STEP_SUMMARY |
| 173 | + echo "Scan completed successfully with no policy violations." >> $GITHUB_STEP_SUMMARY |
| 174 | + ;; |
| 175 | + 1) |
| 176 | + echo "### ❌ Invalid Configuration" >> $GITHUB_STEP_SUMMARY |
| 177 | + echo "An invalid configuration parameter was passed. Check for typos in the parameters." >> $GITHUB_STEP_SUMMARY |
| 178 | + ;; |
| 179 | + 2) |
| 180 | + echo "### ❌ Connection Error" >> $GITHUB_STEP_SUMMARY |
| 181 | + echo "Unable to access the update or license details from the Mend server URL. Check internet connection." >> $GITHUB_STEP_SUMMARY |
| 182 | + ;; |
| 183 | + 4) |
| 184 | + echo "### ❌ Unsupported Language" >> $GITHUB_STEP_SUMMARY |
| 185 | + echo "Unable to detect a supported language within the project based on the file extensions provided." >> $GITHUB_STEP_SUMMARY |
| 186 | + ;; |
| 187 | + 7) |
| 188 | + echo "### ❌ Permission Error" >> $GITHUB_STEP_SUMMARY |
| 189 | + echo "Could not create a cache subdirectory. Check that the Mend CLI permissions include 'create'." >> $GITHUB_STEP_SUMMARY |
| 190 | + ;; |
| 191 | + 9) |
| 192 | + echo "### ⚠️ Policy Violation" >> $GITHUB_STEP_SUMMARY |
| 193 | + echo "Results contain too many vulnerabilities, which contravenes the defined policy." >> $GITHUB_STEP_SUMMARY |
| 194 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 195 | + echo "Please review the findings in the Mend platform." >> $GITHUB_STEP_SUMMARY |
| 196 | + ;; |
| 197 | + 10) |
| 198 | + echo "### ❌ Scanning Engine Failure" >> $GITHUB_STEP_SUMMARY |
| 199 | + echo "A scanning engine stalled or failed." >> $GITHUB_STEP_SUMMARY |
| 200 | + ;; |
| 201 | + *) |
| 202 | + echo "### ❌ Unknown Error" >> $GITHUB_STEP_SUMMARY |
| 203 | + echo "Scan failed with exit code: ${SCAN_EXIT_CODE}" >> $GITHUB_STEP_SUMMARY |
| 204 | + ;; |
| 205 | + esac |
| 206 | +
|
| 207 | + exit $SCAN_EXIT_CODE |
0 commit comments