Skip to content

Commit 6b6296d

Browse files
Add CodeQL Advanced Scanning workflow
1 parent 9c8e9c6 commit 6b6296d

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
name: "CodeQL Advanced Scanning"
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
build-modes:
7+
description: "JSON map of language to build-mode (autobuild, manual, none) e.g.'{\"csharp\":\"manual\"}'"
8+
type: string
9+
default: "{}"
10+
codeql-config:
11+
description: "CodeQL config file reference."
12+
type: string
13+
default: "moonpig-spike/SPIKE-WORKFLOW-TEST/codeql/codeql-config.yaml@master"
14+
secrets:
15+
MNPG_GITHUB_READ_APP_ID:
16+
required: true
17+
MNPG_GITHUB_READ_APP_PEM_FILE:
18+
required: true
19+
MNPG_CODEQL_SLACK_WEBHOOK_URL:
20+
required: false
21+
22+
jobs:
23+
create-matrix:
24+
runs-on: ubuntu-latest
25+
outputs:
26+
matrix: ${{ steps.set-matrix.outputs.languages }}
27+
steps:
28+
- name: Get languages from repo
29+
id: set-matrix
30+
uses: advanced-security/set-codeql-language-matrix@v1
31+
with:
32+
access-token: ${{ secrets.GITHUB_TOKEN }}
33+
endpoint: ${{ github.event.repository.languages_url }}
34+
35+
analyze:
36+
needs: create-matrix
37+
if: ${{ needs.create-matrix.outputs.matrix != '[]' }}
38+
name: Analyze (${{ matrix.language }})
39+
runs-on: ubuntu-latest
40+
timeout-minutes: 360
41+
permissions:
42+
# required for all workflows
43+
security-events: write
44+
# only required for workflows in private repositories
45+
actions: read
46+
contents: read
47+
# required to fetch internal or private CodeQL packs
48+
packages: read
49+
50+
strategy:
51+
fail-fast: false
52+
matrix:
53+
language: ${{ fromJSON(needs.create-matrix.outputs.matrix) }}
54+
55+
steps:
56+
- name: Generate GH App Token
57+
id: generate-token
58+
uses: actions/create-github-app-token@v2
59+
with:
60+
app-id: ${{ secrets.MNPG_GITHUB_READ_APP_ID }}
61+
owner: ${{ github.repository_owner }}
62+
private-key: ${{ secrets.MNPG_GITHUB_READ_APP_PEM_FILE }}
63+
repositories: "moonpig-ops-pipeline-workflows"
64+
65+
- if: ${{ matrix.language == 'csharp' }}
66+
name: Setup Nuget Source
67+
uses: moonpig/moonpig-ops-pipeline-workflows/.github/actions/dotnet-nuget-setup@master
68+
with:
69+
github-token: ${{ secrets.GITHUB_TOKEN }}
70+
71+
- name: Checkout repository
72+
uses: actions/checkout@v6
73+
74+
- name: Initialize CodeQL
75+
uses: github/codeql-action/init@v4
76+
with:
77+
config-file: ${{ inputs.codeql-config }}
78+
external-repository-token: ${{ steps.generate-token.outputs.token }}
79+
languages: ${{ matrix.language }}
80+
build-mode: ${{ fromJSON(inputs.build-modes)[matrix.language] || 'none' }}
81+
82+
- if: ${{ fromJSON(inputs.build-modes)[matrix.language] == 'manual' }}
83+
id: check-codeql-build-action
84+
name: Check if custom build action exists
85+
run: |
86+
if [ ! -d ".github/actions/codeql-build" ]; then
87+
echo "❌ Error: Custom build action not found at .github/actions/codeql-build"
88+
echo "Please create the custom build action and include steps to build the ${{ matrix.language }} application."
89+
exit 1
90+
fi
91+
92+
- if: ${{ fromJSON(inputs.build-modes)[matrix.language] == 'manual' }}
93+
name: Build ${{ matrix.language }} application
94+
uses: ./.github/actions/codeql-build
95+
with:
96+
language: ${{ matrix.language }}
97+
98+
- name: Perform CodeQL Analysis
99+
uses: github/codeql-action/analyze@v4
100+
with:
101+
category: "/language:${{ matrix.language }}"
102+
103+
- if: ${{always() && (failure() || cancelled())}}
104+
name: Slack Notification
105+
uses: slackapi/slack-github-action@v2.0.0
106+
with:
107+
webhook: ${{ secrets.MNPG_CODEQL_SLACK_WEBHOOK_URL }}
108+
webhook-type: webhook-trigger
109+
payload: |
110+
{
111+
"blocks": [
112+
{
113+
"type": "section",
114+
"text": {
115+
"type": "mrkdwn",
116+
"text": "*🚨 CodeQL Analyze `${{ matrix.language }}` Job Failed:* <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}| ${{ github.repository }}>"
117+
}
118+
}
119+
]
120+
}

0 commit comments

Comments
 (0)