最近公司有個項目須要部署到公網上去測試,因爲頻繁更新,手動去服務器更新太麻煩,仔細研究github
有web hook
,開發個自動獲取代碼的小工具咯php
動手搞小工具code-get,以及部分代碼html
md5sum 41a4c2615848eebedb261c61fd0d3074 code-get 25d3637a6fd821f419486548c32666af code-get.exe sha256sum b9b6ddcdb77af002a0f14f61192bfe90effaa5c533d3c5d2e0bdc7a3466d0a0c code-get 3172dcfa76d9986520b96c998529571002cb3a25e285a6f6da0dba98a024b38a code-get.exe
if !Debug { mac := hmac.New(sha1.New, []byte(*token)) _, _ = mac.Write(data) expectedMAC := hex.EncodeToString(mac.Sum(nil)) if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) { writer.WriteHeader(http.StatusInternalServerError) writer.Write([]byte("簽名不一致")) return } } event := request.Header.Get("X-GitHub-Event") switch strings.ToLower(event) { case "ping": if github.PingEvent(data) { writer.WriteHeader(http.StatusOK) writer.Write([]byte("鏈接成功")) return } case "push": if github.PushEvent(data) { writer.WriteHeader(http.StatusOK) writer.Write([]byte("更新成功")) return } }
服務器部署nginx
code-get code-get.exe code-get.sum repositories.conf
ssh-keygen -t rsa -C "<project>.$(hostname)" -f <path-to-save-ssh-key> -N ""
repositories.conf
中的配置,注意目錄權限 [test-repos] # 能夠包含多個,爲項目名 key=<path-to-save-ssh-key> #填入上一步生成的private key路徑 path=<path-to-clone-repos> #填入你要保存github項目的目錄,注意目錄權限 branch=master #默認響應的分支,可換成其餘分支 remote_path=git@github.com:xxxxx/test-repos.git #要響應的github地址,ssh方式可用於私有庫 #高級功能,接收到hook觸發後響應的操做,沒有則使用內置操做 #可用於不單單只是clone代碼, 還附帶其餘操做 #script=/var/www/<my-diy-clone-script.sh>
例如git
#!/bin/bash REPOS_PATH=${WORK_PATH:-/var/www/html/$REPOS} cd $REPOS_PATH || exit 2 CUR_BRANCH=${BRANCH:-master} if [[ "$(git rev-parse --abbrev-ref HEAD)" != "$CUR_BRANCH" ]]; then git checkout -b $CUR_BRANCH git branch --set-upstream-to=origin/$CUR_BRANCH fi if ! git pull origin $CUR_BRANCH:$CUR_BRANCH; then git fetch origin/$CUR_BRANCH git reset --hard origin/$CUR_BRANCH fi #若是是composer管理的話要作一點而外工做 if [[ -f composer.json ]]; then if git log -1 --name-only | grep -q '^composer\.json'; then if [[ -f composer.lock ]]; then composer update --optimize-autoloader --no-dev --no-plugins --no-scripts else composer install --optimize-autoloader --no-dev --no-plugins --no-scripts fi else #若是已經初始化好了,直接更新緩存 composer dump-autoload --optimize fi fi if [ -f think ]; then php think optimize:autoload php think optimize:config php think optimize:route php think optimize:schema elif [ -f artisan ]; then php artisan route:cache php artisan view:cache php artisan config:cache fi
生成daemon
服務,提供systemd
的模板,並啓用systemctl enable <abs-path-to>/code-get.service && systemctl start code-get.service
。github
[Unit] Description=code-get [Service] User=www-data ExecStart=/usr/local/bin/code-get -token "112233" -port 17293 -c /etc/code-get/repositories.conf TimeoutStopSec=3s Restart=always [Install] WantedBy=multi-user.target
netstat -lntp
查看是否啓用成功,並開啓防火牆容許該服務端口github
端配置web
ssh
公鑰填入github
項目的驗證服務的code-get
通信, 默認時`http://<your-public-ip-or-domain>:17293
若是不想暴露過多端口可用nginx轉發json
location = /code-get-server { proxy_pass http://127.0.0.1:17293; }
這樣共用網站的端口,http://<your-public-ip-or-domain>/code-get-server
緩存
token
push
到github
,服務器收到通知就自動拉取代碼了😎若是網絡緣由致使push沒有被及時拉取,可手動更新-u
bash
code-get -token "112233" -c /etc/code-get/repositories.conf -u
會更新全部repositories.conf
列出來的項目服務器