Google App Engine

You first need to install the cli. You can grab it from here: https://cloud.google.com/sdk/docs/ and follow the described steps to install it.

After that, you can start a new shell, and you should have access to the gcloud cli. For example:

  1. > gcloud --version
  2. Google Cloud SDK 194.0.0
  3. bq 2.0.30
  4. core 2018.03.16
  5. gsutil 4.29

You will also need to install some components with the cli (gcloud components install app-engine-java):

For your project, you can use gradle and the official appengine-gradle-plugin. So a build.gradle would look like this:

  1. buildscript {
  2. ext.appengine_version = '1.9.60'
  3. ext.appengine_plugin_version = '1.3.4'
  4. repositories {
  5. jcenter()
  6. }
  7. dependencies {
  8. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  9. classpath "com.google.cloud.tools:appengine-gradle-plugin:$appengine_plugin_version"
  10. }
  11. }
  12. apply plugin: 'kotlin'
  13. apply plugin: 'war'
  14. apply plugin: 'com.google.cloud.tools.appengine'
  15. // appengine does not honor this property, so we are forced to use deep Maven tree layout
  16. // webAppDirName = file('webapp')
  17. sourceSets {
  18. main.kotlin.srcDirs = [ 'src/main/kotlin' ]
  19. repositories {
  20. jcenter()
  21. }
  22. dependencies {
  23. compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  24. compile "io.ktor:ktor-server-servlet:$ktor_version"
  25. compile "io.ktor:ktor-html-builder:$ktor_version"
  26. compile "org.slf4j:slf4j-jdk14:$slf4j_version"
  27. providedCompile "com.google.appengine:appengine:$appengine_version"
  28. }
  29. kotlin.experimental.coroutines = 'enable'
  30. task run(dependsOn: appengineRun)

Once everything is configured, you can now run the application locally, using the gradle task appengineRun:

It should start the server in and the admin in http://localhost:8080/_ah/admin.

Deploying

First, we need to create a project gcloud projects create demo-demo-123456 --set-as-default:

  1. > gcloud projects create demo-demo-123456 --set-as-default
  2. Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/demo-demo-123456].
  3. Waiting for [operations/pc.7618150612308930095] to finish...done.
  4. Updated property [core/project] to [demo-demo-123456].

And then we need to create an application using gcloud app create:

Now we can deploy the application using gradle appengineDeploy:

  1. > gradle :google-appengine-standard:appengineDeploy
  2. Starting a Gradle Daemon (subsequent builds will be faster)
  3. Reading application configuration data...
  4. Mar 23, 2018 6:32:09 AM com.google.apphosting.utils.config.IndexesXmlReader readConfigXml
  5. INFORMATION: Successfully processed /Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/exploded-google-appengine-standard/WEB-INF/appengine-generated/datastore-indexes-auto.xml
  6. Beginning interaction for module default...
  7. 0% Scanning for jsp files.
  8. Success.
  9. Services to deploy:
  10. descriptor: [/Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/staged-app/app.yaml]
  11. source: [/Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/staged-app]
  12. target project: [demo-demo-123456]
  13. target service: [default]
  14. target version: [20180323t063212]
  15. target url: [https://demo-demo-123456.appspot.com]
  16. Beginning deployment of service [default]...
  17. Some files were skipped. Pass `--verbosity=info` to see which ones.
  18. You may also view the gcloud log file, found at
  19. [/Users/user/.config/gcloud/logs/2018.03.23/06.32.10.739209.log].
  20. #============================================================#
  21. #= Uploading 38 files to Google Cloud Storage =#
  22. #============================================================#
  23. File upload done.
  24. Updating service [default]...
  25. ..............done.
  26. Setting traffic split for service [default]...
  27. .......done.
  28. Deployed service [default] to [https://demo-demo-123456.appspot.com]
  29. You can stream logs from the command line by running:
  30. $ gcloud app logs tail -s default
  31. To view your application in the web browser run:
  32. $ gcloud app browse
  33. BUILD SUCCESSFUL in 42s