name: CodeQL on: # Pushes to main are never path-filtered: every commit there needs an analysis for pull requests to be compared against. push: branches: [main] pull_request: branches: [main] paths-ignore: - "**/*.md" - "**/*.mdx" schedule: - cron: "41 5 * * 2" permissions: contents: read concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} cancel-in-progress: true jobs: analyze: name: Analyze (${{ matrix.language }}) runs-on: ubuntu-latest timeout-minutes: ${{ github.event_name == 'pull_request' && 15 || 30 }} permissions: contents: read security-events: write strategy: fail-fast: false matrix: language: [actions, javascript-typescript, python] steps: - name: Checkout code repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # ratchet:actions/checkout@v7.0.1 with: fetch-depth: 2 - name: Test CodeQL workflow scripts if: strategy.job-index == 0 run: node --test scripts/codeql-language-selection.test.mjs scripts/codeql-overlay-prune.test.mjs # A pull request only analyzes the languages it touches. Pushes and scheduled runs skip this step and analyze all three. - name: Select language id: selection if: github.event_name == 'pull_request' env: LANGUAGE: ${{ matrix.language }} run: | set -euo pipefail case "$LANGUAGE" in actions) paths=(.github/workflows ':(glob)**/action.yml' ':(glob)**/action.yaml') ;; javascript-typescript) paths=(. ':(exclude).github/workflows' ':(exclude)python' ':(exclude,glob)**/*.py' ':(exclude,glob)**/*.md' ':(exclude,glob)**/*.mdx') ;; python) paths=(':(glob)**/*.py') ;; *) exit 1 ;; esac run=true if git diff --quiet --no-renames HEAD^1 HEAD -- "${paths[@]}" && git diff --quiet --no-renames HEAD^1 HEAD -- .github/workflows/codeql.yaml; then run=false fi echo "run=$run" >> "$GITHUB_OUTPUT" - name: Initialize CodeQL if: steps.selection.outputs.run != 'false' uses: github/codeql-action/init@915412ff23e8b7659517e607487dbeae018f2934 # ratchet:github/codeql-action/init@v4.39.5 with: languages: ${{ matrix.language }} build-mode: none - name: Analyze if: steps.selection.outputs.run != 'false' uses: github/codeql-action/analyze@915412ff23e8b7659517e607487dbeae018f2934 # ratchet:github/codeql-action/analyze@v4.39.5 with: category: /language:${{ matrix.language }} # Each analysis on main caches an overlay-base database, and pull requests restore only the newest one for their language and CodeQL version, so the older ones only fill the repository's Actions cache. prune-overlay-databases: name: Prune superseded overlay databases needs: analyze if: always() && github.event_name != 'pull_request' runs-on: ubuntu-latest timeout-minutes: 5 permissions: actions: write steps: - name: Delete all but the newest overlay-base database env: GH_TOKEN: ${{ github.token }} run: | set -euo pipefail pattern='-[0-9a-f]{40}-[0-9]+-[0-9]+$' caches=$(gh api --paginate "repos/$GITHUB_REPOSITORY/actions/caches?ref=$GITHUB_REF&key=codeql-overlay-base-database-&per_page=100" --jq '.actions_caches[] | {id, key, created_at}') jq -r --arg pattern "$pattern" 'select(.key | test($pattern) | not) | "::warning::Left \(.key) in place because its key does not end in a commit, run and attempt."' <<< "$caches" jq -rs --arg pattern "$pattern" 'map(select(.key | test($pattern))) | group_by(.key | sub($pattern; "")) | map(sort_by(.created_at) | .[:-1][]) | .[] | "\(.id) \(.key)"' <<< "$caches" | while read -r id key; do # A prune running for a newer commit may already have deleted it. if ! output=$(gh api --method DELETE "repos/$GITHUB_REPOSITORY/actions/caches/$id" 2>&1) && ! grep -q '(HTTP 404)' <<< "$output"; then echo "$output" >&2 exit 1 fi echo "Deleted $key" done