Gradle: Automatic Deployment


私が Gradle を使って Spring Boot の自動デプロイを実現した方法です。 自動というのは リポジトリのフックなどではなく、 自動テストの後のデプロイでもなく、 コマンドでデプロイする方法を指しています。

Environment

  • gradle 2.10

サーバ環境・デプロイ先は How To Deploy Spring Boot Application To AWS EC2 Amazon Linux に記載の方法と同じです。

Procedure

I used gradle-ssh-plugin for auto-deploying.

The deployment internal procedure I created is as below. The command is ./gradlew deployWar.

  1. Compile war
  2. Update war file in the server
  3. Restart tomcat

When you want only to deploy the war, without compiling, execute ./gradlew deployWar -x war.

Code

I created or edited 3 files. First, edit build.gradle, which is edited in the article How To Deploy Spring Boot Application To AWS EC2 Amazon Linux. The highlighted lines below are the lines I edited.

The above code read JSON and detect target servers, first. After that, simply execute linux commands through ssh. At How To Deploy Spring Boot Application To AWS EC2 Amazon Linux, I copied war with sudo -u tomcat ..., but this time it raise error so I decided to copy with sudo and change owner with chown.

The reason why I restart tomcat is to clear database cache.

And, create server setting file.

Create the file deploy_config.json.sample, and copy it to deploy_config.json when you use the command. 秘密鍵情報などあるので git に入れるべきか悩むところです。 Create .gitignore if you want to.