Gradle
How to use Hanzo KMS to inject environment variables with Gradle
Using Hanzo KMS with Gradle
By integrating KMS 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 Hanzo KMS with Gradle.
Basic Usage
To run a Gradle task with Hanzo KMS, you can use the run command. The basic structure is:
kms 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"
}
}kms run -- gradle generateFileWith this command, Hanzo KMS will automatically inject the environment variables associated with the current Hanzo KMS project into the Gradle process.
Your Gradle script can then access these variables using System.getenv('VARIABLE_NAME').
More Examples
Building a Project with a Specific Profile
Assuming you have different build profiles (e.g., 'development', 'production'), you can use Hanzo KMS to switch between them:
kms run -- gradle buildInside your build.gradle, you might have:
if (System.getenv('PROFILE') == 'production') {
// production-specific configurations
}Running Tests with Different Database Configurations
If you want to run tests against different database configurations:
kms run -- gradle testYour test configuration in build.gradle can then adjust the database URL accordingly:
test {
systemProperty 'db.url', System.getenv('DB_URL')
}Generating Artifacts with Versioning
For automated CI/CD pipelines, you might want to inject a build number or version:
kms run -- gradle assembleAnd in build.gradle:
version = System.getenv('BUILD_NUMBER') ?: '1.0.0-SNAPSHOT'Advantages of Using Hanzo KMS with Gradle
- Flexibility: Easily adapt your Gradle builds to different environments without modifying the build scripts or setting environment variables manually.
- Reproducibility: Ensure consistent builds by leveraging the environment variables from the related Hanzo KMS project.
- Security: Protect sensitive information by using Hanzo KMS's secrets management without exposing them in scripts or logs.
How is this guide?
Last updated on