Repository navigation
120 lines (108 loc) · 4.57 KB
/
Copy pathcodeql-advanced-manual.yml
File metadata and controls
120 lines (108 loc) · 4.57 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
# This is a modified version of the CodeQL Advanced workflow template provided by GitHub.
name: "CodeQL Advanced"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: '33 2 * * 5'
permissions: {}
jobs:
check-changes:
runs-on: ubuntu-26.04
permissions:
contents: read
outputs:
matrix: ${{ steps.changes.outputs.matrix }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 1
# Determine which languages were actually changed so we can run only the relevant CodeQL analysis.
# Even if there were no languages changed, still run a quick check on `actions` to satisfy OpenSSF Scorecard that CodeQL analysis is performed on every PR.
- name: Check changed files
id: changes
shell: bash
env:
BASE_SHA: ${{ github.event.before }}
EVENT_NAME: ${{ github.event_name }}
run: |
changed=(false false false false false false false)
if [[ "$EVENT_NAME" == pull_request ]]; then
parent_lines=$(git cat-file -p HEAD | sed -n '1,/^$/s/^parent //p' | tr '\n' ' ')
read -r -a parents <<< "$parent_lines"
if [[ "${#parents[@]}" == 2 ]]; then
BASE_SHA="${parents[0]}"
else
echo "::warning::PR checkout is not a merge commit."
BASE_SHA=""
fi
fi
if [[ "$EVENT_NAME" != schedule && "$BASE_SHA" =~ ^[0-9a-f]{40}$ && ! "$BASE_SHA" =~ ^0+$ ]] &&
! git cat-file -e "${BASE_SHA}^{commit}" 2>/dev/null; then
if ! git fetch --no-tags --depth=1 origin "$BASE_SHA"; then
echo "::warning::Could not fetch the base commit."
fi
fi
if [[ "$EVENT_NAME" == schedule || ! "$BASE_SHA" =~ ^[0-9a-f]{40}$ || "$BASE_SHA" =~ ^0+$ ]] ||
! git cat-file -e "${BASE_SHA}^{commit}" 2>/dev/null; then
echo "::notice::Scheduled scan or base commit unavailable; analyzing all languages."
changed=(true true true true true true true)
else
git diff --no-renames --name-only -z "$BASE_SHA" HEAD > "$RUNNER_TEMP/codeql-changed-files"
while IFS= read -r -d '' file; do
case "$file" in
.github/workflows/*.yml|.github/workflows/*.yaml|action.yml|action.yaml|*/action.yml|*/action.yaml)
changed[0]=true ;;
esac
case "$file" in
*.go) changed[1]=true ;;
*.java|*.kt|*.kts) changed[2]=true ;;
*.js|*.jsx|*.ts|*.tsx|*.mjs|*.cjs|*.mts|*.cts|*.html|*.htm) changed[3]=true ;;
*.py|*.pyi) changed[4]=true ;;
*.rb|*.erb|*.gemspec|*.rake|Gemfile|*/Gemfile) changed[5]=true ;;
*.swift) changed[6]=true ;;
esac
done < "$RUNNER_TEMP/codeql-changed-files"
fi
languages=(actions go java-kotlin javascript-typescript python ruby swift)
matrix=""
for i in "${!languages[@]}"; do
if [[ "${changed[$i]}" == true ]]; then
build_mode=none
case "${languages[$i]}" in go|java-kotlin|swift) build_mode=autobuild ;; esac
matrix="${matrix:+$matrix,}{\"language\":\"${languages[$i]}\",\"build-mode\":\"$build_mode\"}"
fi
done
# Scorecard requires an analysis even when no supported files changed.
if [[ -z "$matrix" ]]; then matrix='{"language":"actions","build-mode":"none"}'; fi
echo "matrix={\"include\":[$matrix]}" >> "$GITHUB_OUTPUT"
analyze:
name: Analyze (${{ matrix.language }})
needs: check-changes
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
permissions:
contents: read
security-events: write
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.check-changes.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
with:
config-file: .github/workflows/config/codeql.yml
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
with:
category: "/language:${{matrix.language}}"