Dynamic AppEngine Configurations using Gradle Part 1
When working with AppEngine applications, often times we need to work with configuration files like appengine-web.xml, cron.xml, dispatch.xml etc. And also we have multiple environments to deal with like staging, alpha, and production.
The problem is we need different configurations for each of those environments for example,
- In cron.xml, we need different configurations to schedule jobs based on the environment. Ex: Schedule a job to run once in every two hours in staging and once every 15 minutes in production.
- In dispatch.xml, we need different routing rules to be configured based on staging and production, etc.
Likewise, we might have several reasons to have different configurations per environment. In this series of articles, we are going to explore how to set up these configurations for each environment using Gradle. Let’s get started.
Note: If you like to learn more on how to setup AppEngine with Gradle, check out my other article Multi-Module Projects with AppEngine and Gradle
Dynamic Deployment Configurations
In this article, we are going to cover how to have dynamic deployment configuration for performing appengine deployment
When you are performing appengine deployment (gcloud app deploy) via Gradle appengineDeploy task, we need the following information for each environment,
We are going to achieve this with the help of Gradle Project Properties
--project-propSets a project property of the root project, for example
So we are going to pass a property called mode with the following values for different environment deployment.
-Pmode=stagingto represent staging deployment
-Pmode=alphato represent alpha deployment
-Pmode=liveto represent live deployment
Based on the mode value, we can set different values for deploy properties like below,
Here is what we are doing,
- In gaeVersions property we are setting different version number for each target environments, these values can be updated when we need to deploy to a new version
- In getDeployConfig() method, based on the mode value we are setting different project ID, version and promote options. By default, it will take mode as staging.
And we can call this method from appengine deploy configuration section like below,
As you can see, all of the deploy configurations are set using the config dictionary returned from getDeployConfig() method.
Finally, we can now deploy to multiple environments with a simple Gradle command,
gradle appengineDeploy -Pmode=staging
gradle appengineDeploy -Pmode=alpha
gradle appengineDeploy -Pmode=live
If you are using IntelliJ you can also setup this as Run Configurations like below,
For the complete sample check out the demo application in GitHub, Click Here
Thank you for reading, hope this helps. In the upcoming articles we will explore how to have dynamic configuration files based on environment.
Stay Tuned and Happy Coding…