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,

appengineDeploy task properties

We are going to achieve this with the help of Gradle Project Properties

-P, --project-prop
Sets a project property of the root project, for example -Pmyprop=myvalue

So we are going to pass a property called mode with the following values for different environment deployment.

-Pmode=staging to represent staging deployment
-Pmode=alpha to represent alpha deployment
-Pmode=live to represent live deployment

Based on the mode value, we can set different values for deploy properties like below,

deploy config based on mode property

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,

appenigneDeploy task config using mode

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,

For staging: gradle appengineDeploy -Pmode=staging

For alpha: gradle appengineDeploy -Pmode=alpha

For production: gradle appengineDeploy -Pmode=live

If you are using IntelliJ you can also setup this as Run Configurations like below,

deploy Run Configuration for each environment

For the complete sample check out the demo application in GitHub, Click Here

Conclusion

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…

This story is published in The Startup, Medium’s largest entrepreneurship publication followed by +388,268 people.

Subscribe to receive our top stories here.