miles@Oscar há 9 anos atrás
commit
8d769eff8c
4 ficheiros alterados com 132 adições e 0 exclusões
  1. 35 0
      README.md
  2. 10 0
      auto_deploy.sh
  3. 60 0
      index.php
  4. 27 0
      post-receive.sample

+ 35 - 0
README.md

@@ -0,0 +1,35 @@
+Git Auto Deploy
+=========================================
+Simple tools for git auto deploy in linux
+
+Settings
+---
+### Githooks
+Copy the contents in [post-receive.sample] to your server's hooks, you may replace the ```url``` and ```token``` with your own settings
+
+### Server
+Put the files ```auto_deploy.sh``` and ```index.php``` to your website in the same directory. For example, you may access ```index.php``` from the url ```http://myweb.com/auto_deploy/index.php```, also it is to-post url in the githooks
+
+Replace your deploy path in ```auto_deploy.sh``` by setting the path value
+
+Replace your ```toCheckToken``` and ```validIP``` in ```index.php```
+
+### Permission
+Web user should have permission to run the git command!
+Let's say that your ```nginx``` user is ```www``` (of course you can check this by ```exec(whoami)```), and it's recommended ssh key of ```www``` should be added to this project's SSH deploy key.
+
+Usage
+---
+Just with the text ```[deploy]``` included in your commit message, like
+
+    git commit -m "Bug fix [deploy]"
+    git push
+
+Remote handle message may show to you while pushing.
+
+FYI
+---
+```exec``` function is required in ```php.ini```
+
+
+

+ 10 - 0
auto_deploy.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+path=YOUR_DEPLOY_DIR
+cd $path
+
+# Save local changes
+git stash
+
+# Auto deploy by using hard reset
+git fetch --all
+git reset --hard origin/master

+ 60 - 0
index.php

@@ -0,0 +1,60 @@
+<?php
+
+// Your Token
+$toCheckToken = 'your_to_verify_token';
+
+// Your valid ip address
+$validIP = array('123.45.67.89');
+
+// Uncomment to set log
+//$logFile = '/tmp/deploy.log';
+
+$remoteIP = $_SERVER['REMOTE_ADDR'];
+$time = date("Y-m-d H:i:s");
+
+if ($logFile) {
+    $log = true;
+    $fs = fopen($logFile, 'a');
+    fwrite($fs, "[start deploy] time:{$time}".PHP_EOL);
+}
+
+# Check IP
+if (!in_array($remoteIP, $validIP)) {
+    $msg = "Invalid IP:".$remoteIP;
+    echo $msg;
+    $log && fwrite($fs, $msg.PHP_EOL);
+    exit(0);
+}
+
+# Decode data
+$json = file_get_contents("php://input");
+$data = json_decode($json);
+if (!$data || empty($data)) {
+    $data = (object) $_POST;
+}
+
+# Check token
+if ($data->token != $toCheckToken) {
+    $msg = "Invalid token:".$data->token;
+    echo $msg;
+    $log && fwrite($fs, $msg.PHP_EOL);
+    exit(0);
+}
+
+# Check function
+if (!function_exists('exec')) {
+    echo "exec funciton is not avaiable!";
+    exit(0);
+}
+
+# Exec update script
+exec("sh auto_deploy.sh",$output);
+foreach ($output as $val) {
+	echo $val."\n";
+}
+
+# log
+$msg = "Finish auto deploy at ".$time;
+$log && fwrite($fs, $msg.PHP_EOL);
+$fs && fclose($fs);
+

+ 27 - 0
post-receive.sample

@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# A git hook script for the "post-receive" event.
+#
+# The "post-receive" script is run after receive-pack has accepted a pack
+# see contrib/hooks/ for a sample, or uncomment the next line and
+# rename the file to "post-receive".
+
+# Change your post url and token if needed
+
+# Get the lastest commit log
+msg=$(git log -1 --pretty=format:"%s")
+
+# Check whether to active the notice
+is_active=$(echo "$msg" | grep "\[deploy\]")
+if [ -z "$is_active" ]; then
+    echo >&2 "[info] Not to auto deploy"
+    exit 1
+fi
+
+# Curl
+url=WEB_URL
+token=YOUR_TOKEN
+echo "================Auto Deploy=================="
+curl -H "Content-Type: application/json" -X POST -s -d "{\"token\":\"$token\"}" $url
+echo "================Auto Deploy=================="
+