# Add CodeQL Scanning to a Gradle Project

Yes. An agent adds a CodeQL workflow to a Gradle project in build mode none, runs the analysis in its sandbox to preview the findings, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-codeql-scanning/gradle/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the build and the workflows before it writes one. In a Spring Boot app like spring-projects/spring-petclinic, `build.gradle` applies `java`, `checkstyle`, `org.springframework.boot` 4.1.0, `io.spring.dependency-management`, the GraalVM native plugin, `org.cyclonedx.bom` and `io.spring.javaformat`, with a toolchain of `JavaLanguageVersion.of(17)`; `gradle/wrapper/gradle-wrapper.properties` pins Gradle 9.5.1; `settings.gradle` names the single project; `pom.xml` builds the same code with Maven and the same `java.version` of 17. `.github/workflows/gradle-build.yml` runs on pushes and pull requests to `main` with a one-entry matrix of Java 17, `actions/setup-java@v4` with the `adopt` distribution and, oddly, `cache: maven`, then `gradle/actions/setup-gradle@v4` and `./gradlew build`; `maven-build.yml` is its twin with `./mvnw -B verify`; `deploy-and-test-cluster.yml` runs the app on a kind cluster. There is no CodeQL workflow and no `security` section in the README.

Then it decides what CodeQL should look at. `src/main/java` is the Java to scan; `src/main/resources/static` holds only CSS, fonts and a favicon, so there is no JavaScript to add; `src/checkstyle` holds the nohttp rules the build already runs; and the three workflows themselves are a scan target, since CodeQL analyses GitHub Actions workflows as the `actions` language. For Java the choice is the build mode. CodeQL's `none` mode builds a database without running the build and is what default setup picks for Java without Kotlin; `autobuild` would run `./gradlew` through CodeQL's own detection, and `manual` runs a command you give. The project has no Kotlin and no generated sources beyond Spring's annotation processing, so `none` is the right fit, and it avoids the question of which of the two build tools the scan should invoke.

The agent writes `.github/workflows/codeql.yml`: on `push` and `pull_request` to `main` and on a weekly `schedule`, with `permissions` of `security-events: write`, `contents: read` and `actions: read`, a matrix of `java-kotlin` with `build-mode: none` and `actions` with no build mode, `actions/checkout`, `github/codeql-action/init@v4` with the matrix language and build mode, and `github/codeql-action/analyze@v4` with a `category` per language so the two results do not overwrite each other. It leaves `actions/setup-java` out of the job, since `none` needs no JDK, and turns on the `security-extended` suite through `queries` only if the maintainers ask, keeping the default suite otherwise. It also fixes nothing in the other workflows, but the description notes the `cache: maven` line in `gradle-build.yml`, because a reviewer who reads one workflow will read the next.

Then it proves the workflow works before anyone enables code scanning on the repository. In its own sandbox it downloads the CodeQL CLI bundle, runs `codeql database create --language=java --build-mode=none` over the checkout, then `codeql database analyze` with the default `java-code-scanning` suite into a SARIF file, and reads the result count and the rules that fired, so the pull request can say what the first scan will show rather than promising a green check. It runs the same two commands for `actions` over `.github/workflows`. The `cyclonedx` plugin and the `checkstyleNohttp` task in `build.gradle` are left as they are; CodeQL adds to them rather than replacing them, and the README gets a short security section that names all three.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add CodeQL scanning to this Gradle project. Read build.gradle, settings.gradle, the Gradle wrapper properties, any Maven build beside it, every workflow under .github/workflows, the source tree and the README first, and list the languages present, the Java version the toolchain pins, and which security checks the build already runs. Choose the CodeQL build mode for Java from what you found and say why; use none unless Kotlin or generated code requires a build. Write .github/workflows/codeql.yml on push and pull_request to the default branch plus a weekly schedule, with security-events write permission, a matrix of java-kotlin and actions, init and analyze from github/codeql-action pinned to a major, and a category per language. Download the CodeQL CLI bundle in the sandbox and run database create and analyze for both languages with the default suites, and record how many results each produced and which rules fired. Add a security section to the README that names CodeQL and the checks the build already had. Open a pull request with the workflow and the local analysis results.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request with `.github/workflows/codeql.yml` and the README section. Its description says which build mode it chose and why, lists the two languages and their query suites, and gives the result of the local analysis for each: how many findings, which rules, and the file and line of each, so the maintainers know what the first scan will post before they enable code scanning; the SARIF files and the CLI output are in the run's record.

A reviewer checks the build mode against their plans for the code: a Kotlin module arriving next quarter means switching to `autobuild` then, and the description says so. They also decide what to do with the findings the local run listed, since merging the workflow turns them into alerts on the repository's Security tab, and the `actions` results in particular tend to be permission and pinning advice on the existing workflows.

## Run it on every terminal hand-off

A maintainer asked by their organisation to turn on code scanning starts this from the terminal with this body and the repository in `resources`, and goes back to the release. The run reads the real build and the real workflows, so the mode, the languages and the schedule follow the project rather than the template in the Actions tab.

Send a `request_id`, a UUID, with the body so a retried start returns the run it already began. When the turn ends, `GET /o/{org}/sessions/{id}` names the pull request, and a follow-up to the same session can ask for the `security-extended` suite or for a fix to one of the findings the local analysis listed.

## Why run it on Rush?

A CodeQL workflow is easy to add and hard to judge: the build mode, the languages and the suite decide whether the first scan is useful or noise, and the Actions template cannot know that this project has two build tools and no Kotlin. The agent runs the analysis itself in its own sandbox with the CodeQL CLI, so the pull request arrives with the findings the workflow will produce, and the maintainers decide with the results in front of them.

The same run fits any Gradle project, single or multi-module, with or without Kotlin, because it reads the build files and picks the mode from them. Each repository gets a workflow matched to its languages and a local analysis that previews the alerts, and every run keeps the full record of the database it built.

## Frequently asked questions

### Why build mode none instead of running Gradle?

For Java without Kotlin, `none` builds the CodeQL database from the source alone, needs no JDK or Gradle in the workflow, and sidesteps the choice between the Gradle and Maven builds the project keeps. The pull request says to switch to `autobuild` if Kotlin or build-time code generation arrives.

### Does it scan the workflows too?

Yes. CodeQL's `actions` language analyses the YAML under `.github/workflows`, and the matrix includes it with its own category. Its findings are typically about permissions and unpinned actions, and the local analysis in the pull request shows what it says about the existing workflows.

### Will the pull request create alerts on my repository?

Not by itself. Alerts appear once the workflow runs on the default branch with code scanning enabled for the repository. The local analysis in the description previews them so the maintainers can fix or dismiss with the list in hand.

### Can it use the extended query suite?

Yes. Adding `queries: security-extended` to the init step turns it on, and a follow-up to the same session can do that and re-run the local analysis so the description shows the larger result set. The default suite is on first because it has fewer low-confidence results.

## More Gradle jobs

- [Enable the Gradle Configuration Cache for a Spring Boot Build](https://getrush.ai/run/add-configuration-cache/gradle/)
- [Add Dependabot to a Gradle Project](https://getrush.ai/run/add-dependabot/gradle/)
- [Add Dependency Locking to a Gradle Build](https://getrush.ai/run/add-dependency-locking/gradle/)

## The same job on other stacks

- [Add CodeQL Scanning to a Maven Spring Boot Project](https://getrush.ai/run/add-codeql-scanning/maven/)
- [Add CodeQL Scanning to a TypeScript Library](https://getrush.ai/run/add-codeql-scanning/typescript-library/)
- [Add CodeQL Scanning to a CMake C++ Project](https://getrush.ai/run/add-codeql-scanning/cmake/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
