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