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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import groovy.json.JsonSlurper buildscript { repositories { mavenCentral() jcenter() } dependencies { classpath 'org.hidetake:gradle-ssh-plugin:2.8.0' } } apply plugin: 'org.hidetake.ssh' ssh.settings { dryRun = project.hasProperty('dryRun') } repositories { mavenCentral() } def setupServer(serverRole) { // read and set servers JsonSlurper slurper = new JsonSlurper() def deployConfig = slurper.parse(new File('deploy_config.json')) deployConfig.hosts.each { if (it.role != serverRole) return; def serverHost = it.host remotes.create(serverHost) { role serverRole host = serverHost user = deployConfig.user identity = file(deployConfig.key) passphrase = deployConfig.pass_phrase } } } /** * deploy */ task deploy << { // set target server role if (!project.hasProperty("serverRole")) return def serverRole = project.properties['serverRole'] JsonSlurper slurper = new JsonSlurper() def deployConfig = slurper.parse(new File('deploy_config.json')) def gitUserName = deployConfig.git.user_name def gitPassword = deployConfig.git.password setupServer serverRole ssh.settings { knownHosts = allowAnyHosts } def railsRoot = "/program_root_path" def backupRoot = "/backup_root_path" ssh.run { session(remotes) { def exitFlag = false execute("df -h") { result -> def loop = true while (loop) { println "Continue? Y or N: " switch (System.in.newReader().readLine()) { case ~/^[Yy]$/: loop = false break case ~/^[Nn]$/: loop = false exitFlag = true break } } } if (exitFlag) return execute "rm -rf ${backupRoot}" execute "cp -prv ${railsRoot} ${backupRoot}" execute "cd ${railsRoot}; git checkout -- db/schema.rb" execute("cd ${railsRoot}; git pull") execute "rake db:migrate RAILS_ENV=production" executeSudo("apachectl -k graceful; echo $?") { result -> switch (result) { case "0": println "Success" break default: println "Failed" } } } } } |
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

