|
| 1 | +# This workflow uses actions that are not certified by GitHub. |
| 2 | +# They are provided by a third-party and are governed by |
| 3 | +# separate terms of service, privacy policy, and support |
| 4 | +# documentation. |
| 5 | + |
| 6 | +# This workflow checks out code, performs a Codacy security scan |
| 7 | +# and integrates the results with the |
| 8 | +# GitHub Advanced Security code scanning feature. |
| 9 | +# For more information on the Codacy security scan action usage and |
| 10 | +# parameters, see https://github.com/codacy/codacy-analysis-cli-action. |
| 11 | +# For more information on Codacy Analysis CLI in general, see |
| 12 | +# https://github.com/codacy/codacy-analysis-cli. |
| 13 | + |
| 14 | +name: Codacy Security Scan |
| 15 | + |
| 16 | +concurrency: |
| 17 | + # This concurrency group ensures that only one Codacy analysis runs at a time |
| 18 | + group: codacy-${{ github.ref_name }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +on: |
| 22 | + push: |
| 23 | + branches: ["main"] |
| 24 | + pull_request: |
| 25 | + # The branches below must be a subset of the branches above |
| 26 | + branches: ["main"] |
| 27 | + schedule: |
| 28 | + - cron: '42 0 * * 1' |
| 29 | + workflow_dispatch: |
| 30 | + |
| 31 | +permissions: |
| 32 | + contents: read |
| 33 | + |
| 34 | +jobs: |
| 35 | + codacy-security-scan: |
| 36 | + permissions: |
| 37 | + # for actions/checkout to fetch code |
| 38 | + contents: read |
| 39 | + # for github/codeql-action/upload-sarif to upload SARIF results |
| 40 | + security-events: write |
| 41 | + # only required for a private repository by |
| 42 | + # github/codeql-action/upload-sarif to get the Action run status |
| 43 | + actions: read |
| 44 | + name: Codacy Security Scan |
| 45 | + runs-on: ubuntu-latest |
| 46 | + timeout-minutes: 30 |
| 47 | + steps: |
| 48 | + # Checkout the repository to the GitHub Actions runner |
| 49 | + - name: Checkout code |
| 50 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 51 | + |
| 52 | + - name: Set Codacy paths |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | + echo "CODACY_WORKDIR=$RUNNER_TEMP/codacy-src" >> "$GITHUB_ENV" |
| 56 | + echo "CODACY_SARIF=$RUNNER_TEMP/results.sarif" >> "$GITHUB_ENV" |
| 57 | +
|
| 58 | + - name: Prepare workspace copy without .git |
| 59 | + run: | |
| 60 | + set -euo pipefail |
| 61 | + mkdir -p "$CODACY_WORKDIR" |
| 62 | + rsync -a --delete --exclude '.git' ./ "$CODACY_WORKDIR/" |
| 63 | +
|
| 64 | + # Execute Codacy Analysis CLI and generate a SARIF output with |
| 65 | + # the security issues identified during the analysis |
| 66 | + - name: Run Codacy Analysis CLI |
| 67 | + uses: codacy/codacy-analysis-cli-action@562ee3e92b8e92df8b67e0a5ff8aa8e261919c08 |
| 68 | + with: |
| 69 | + # Check https://github.com/codacy/codacy-analysis-cli#project-token |
| 70 | + # to get your project token from your Codacy repository. |
| 71 | + # You can also omit the token and run the tools that support |
| 72 | + # default configurations |
| 73 | + project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} |
| 74 | + verbose: true |
| 75 | + directory: ${{ env.CODACY_WORKDIR }} |
| 76 | + output: ${{ env.CODACY_SARIF }} |
| 77 | + format: sarif |
| 78 | + skip-uncommitted-files-check: true |
| 79 | + # Adjust severity of non-security issues |
| 80 | + gh-code-scanning-compat: true |
| 81 | + # Force 0 exit code to allow SARIF file generation |
| 82 | + # This will handover control about PR rejection to the GitHub side |
| 83 | + max-allowed-issues: 2147483647 |
| 84 | + |
| 85 | + # Process SARIF file to split by tool |
| 86 | + - name: Split SARIF by tool |
| 87 | + run: | |
| 88 | + # Fail fast and surface errors clearly |
| 89 | + set -euo pipefail |
| 90 | + if [ -f "$CODACY_SARIF" ] && [ -s "$CODACY_SARIF" ]; then |
| 91 | + echo "$CODACY_SARIF present; preselecting for upload and skipping split." |
| 92 | + echo "SARIF_FILE=$CODACY_SARIF" >> "$GITHUB_ENV" |
| 93 | + exit 0 |
| 94 | + else |
| 95 | + echo "No SARIF file found or file is empty: $CODACY_SARIF" |
| 96 | + echo "Creating empty SARIF file to prevent workflow failure" |
| 97 | + # Create empty SARIF file with proper schema |
| 98 | + schema_url="https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" |
| 99 | + empty_sarif="$RUNNER_TEMP/sarif_empty.sarif" |
| 100 | + { |
| 101 | + echo '{' |
| 102 | + echo " \"\$schema\": \"$schema_url\"," |
| 103 | + echo ' "version": "2.1.0",' |
| 104 | + echo ' "runs": []' |
| 105 | + echo '}' |
| 106 | + } > "$empty_sarif" |
| 107 | + # Mark the empty SARIF for upload |
| 108 | + echo "SARIF_FILE=$empty_sarif" >> "$GITHUB_ENV" |
| 109 | + exit 0 |
| 110 | + fi |
| 111 | +
|
| 112 | + # Select SARIF file for upload |
| 113 | + - name: Select SARIF file for upload |
| 114 | + run: | |
| 115 | + set -euo pipefail |
| 116 | + # Honor preselected SARIF_FILE from earlier steps (e.g., empty SARIF case) |
| 117 | + if [ -n "${SARIF_FILE:-}" ]; then |
| 118 | + echo "Preselected SARIF_FILE=$SARIF_FILE; not overriding." |
| 119 | + exit 0 |
| 120 | + fi |
| 121 | + # First, try to upload the original SARIF file if it exists |
| 122 | + if [ -f "$CODACY_SARIF" ] && [ -s "$CODACY_SARIF" ]; then |
| 123 | + echo "Found $CODACY_SARIF, attempting upload..." |
| 124 | + echo "SARIF_FILE=$CODACY_SARIF" >> "$GITHUB_ENV" |
| 125 | + else |
| 126 | + echo "No valid SARIF files found" |
| 127 | + echo "SARIF_FILE=" >> "$GITHUB_ENV" |
| 128 | + fi |
| 129 | + continue-on-error: true |
| 130 | + |
| 131 | + # Upload the identified SARIF file |
| 132 | + - name: Upload identified SARIF file |
| 133 | + if: always() && env.SARIF_FILE != '' |
| 134 | + uses: github/codeql-action/upload-sarif@b36bf259c813715f76eafece573914b94412cd13 # v3 |
| 135 | + with: |
| 136 | + sarif_file: ${{ env.SARIF_FILE }} |
| 137 | + continue-on-error: true |
0 commit comments