docs/integrations/build-tools/gradle.mdx
By integrating Infisical CLI 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.
To run a Gradle task with Infisical, you can use the run command. The basic structure is:
infisical run -- [Your command here]
For example, to run the generateFile task in 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 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').
Assuming you have different build profiles (e.g., 'development', 'production'), you can use Infisical to switch between them:
infisical run -- gradle build
Inside your build.gradle, you might have:
if (System.getenv('PROFILE') == 'production') {
// production-specific configurations
}
If you want to run tests against different database configurations:
infisical run -- gradle test
Your test configuration in build.gradle can then adjust the database URL accordingly:
test {
systemProperty 'db.url', System.getenv('DB_URL')
}
For automated CI/CD pipelines, you might want to inject a build number or version:
infisical run -- gradle assemble
And in build.gradle:
version = System.getenv('BUILD_NUMBER') ?: '1.0.0-SNAPSHOT'