Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
---
title: "Inject secrets into Gradle builds"
sidebarTitle: "Gradle"
description: "How to use Infisical to inject environment variables and secrets into Gradle builds."
---
# Using Infisical with Gradle
By integrating [Infisical CLI](../../cli/overview) with Gradle, you can configure your builds and scripts to different environments, CI/CD pipelines, and more without explicitly setting variables in the command line.
This documentation provides an overview of how to use Infisical with [Gradle](https://gradle.org/).
## Basic usage
To run a Gradle task with Infisical, you can use the `run` command. The basic structure is:
```
infisical sm run -- [Your command here]
```
For example, to run the `generateFile` task in Gradle:
```groovy build.gradle
task generateFile {
doLast {
String content = System.getenv('ENV_NAME_FROM_INFISICAL') ?: 'Default Content'
file('output.txt').text = content
println "Generated output.txt with content: $content"
}
}
```
```
infisical sm run -- gradle generateFile
```
With this command, Infisical will automatically inject the environment variables associated with the current Infisical project into the Gradle process.
Your Gradle script can then access these variables using `System.getenv('VARIABLE_NAME')`.
## More examples
### 1. Building a project with a specific profile
Assuming you have different build profiles (e.g., 'development', 'production'), you can use Infisical to switch between them:
```
infisical sm run -- gradle build
```
Inside your `build.gradle`, you might have:
```groovy build.gradle
if (System.getenv('PROFILE') == 'production') {
// production-specific configurations
}
```
### 2. Running tests with different database configurations
If you want to run tests against different database configurations:
```
infisical sm run -- gradle test
```
Your test configuration in `build.gradle` can then adjust the database URL accordingly:
```groovy build.gradle
test {
systemProperty 'db.url', System.getenv('DB_URL')
}
```
### 3. Generating artifacts with versioning
For automated CI/CD pipelines, you might want to inject a build number or version:
```
infisical sm run -- gradle assemble
```
And in `build.gradle`:
```groovy build.gradle
version = System.getenv('BUILD_NUMBER') ?: '1.0.0-SNAPSHOT'
```
## Advantages of using Infisical with Gradle
1. **Flexibility**: Easily adapt your Gradle builds to different environments without modifying the build scripts or setting environment variables manually.
2. **Reproducibility**: Ensure consistent builds by leveraging the environment variables from the related Infisical project.
3. **Security**: Protect sensitive information by using Infisical's secrets management without exposing them in scripts or logs.