Table of Contents
I wrote the script to deploy application controlled by Git.
Prerequisites
今回前提としたデプロイは、単純に git pull
をしてコードを更新した後、 サーバを再起動するものです。 サーバから Git の リモートリポジトリへの認証は、 鍵ファイルを使って行っているものとします。
また Rails を前提としているので、 Rails 専用 の処理もあります。 他の言語でも応用できます。
応用次第で、 git で管理されているコードの中の一部をサーバにコピーすることもできます。
Environment
- Gradle 3.2.1
Preparation
Install gradle, and execute gradle init
. Then gradle templates will be generated. There, write the script.
Code
As follows.
Explanation
デプロイ部分の処理は次の通りです。 現場によって変えて使う部分です。
- Connect to the server with ssh.
- Check the available amount of the storage with
df
. ここで Y または N をタイプして、後続の処理を実行するか否か決定します。 - Copy old code as backup.
- Get the new code and deploy it with
git pull
. - Execute
rake db:migrate
. This is for Rails. - Restart Apache. Show “Success” or “Failed” according to the previous command result.
Attention
You have to register the private key of the server forgit pull
. If you change the code, SSH agent forward is available so you can use your own key for git pull
.
GitHub
The code is on GitHub. gradle-deploy