├── .gitignore ├── package.json ├── LICENSE ├── readme.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gitlab-pages-webhook", 3 | "version": "0.2.0", 4 | "description": "Set up your gitlab-pages server base on gitlab's web hook!", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "bin": { 10 | "gitlab-pages-webhook": "index.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/unbug/gitlab-pages-webhook.git" 15 | }, 16 | "keywords": [ 17 | "gitlab-pages" 18 | ], 19 | "author": "unbug", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/unbug/gitlab-pages-webhook/issues" 23 | }, 24 | "homepage": "https://github.com/unbug/gitlab-pages-webhook", 25 | "devDependencies": { 26 | "colors": "^1.0.3" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 unbug 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Readme 2 | ========= 3 | Github-pages is cool,but what about gitlab?This project helps you to set up a web hook server for your gitlab,so you can deoply your gitlab-pages,it's easier and powerful then your image,try it out. 4 | 5 | Install 6 | ========== 7 | ```shell 8 | npm install -g gitlab-pages-webhook 9 | ``` 10 | 11 | Web hook server configuration 12 | ================ 13 | ## Start a web hook server 14 | ```shell 15 | //gitlab-pages-webhook [listen port] [gitlab host,...] 16 | gitlab-pages-webhook 8163 http://mygitlab.com,http://mygitlab2.com 17 | ``` 18 | gitlab hosts tells the hook to filter which host to listene,in this case,only gitlab host http://mygitlab.com and http://mygitlab2.com will deploy to your server,http://mygitlab3.com will not. 19 | 20 | ## Set a nginx proxy to your web hook server,such as 21 | ```shell 22 | server { 23 | listen 80; 24 | server_name myserver.com; 25 | root /home; 26 | index index.html index.htm; 27 | 28 | error_page 405 =200 @405; 29 | location @405{ 30 | root /home; 31 | proxy_method GET; 32 | proxy_pass http://static_backend; 33 | } 34 | access_log off; 35 | error_log off; 36 | location / { 37 | # First attempt to serve request as file, then 38 | # as directory, then fall back to displaying a 404. 39 | try_files $uri $uri/ /index.html; 40 | # Uncomment to enable naxsi on this location 41 | #include /etc/nginx/naxsi.rules 42 | #autoindex on; 43 | } 44 | location ~* ^/.*/$ { 45 | #alias /usr/share/doc/; 46 | #autoindex on; 47 | #allow 127.0.0.1; 48 | #allow ::1; 49 | #deny all; 50 | } 51 | location /doc/ { 52 | alias /usr/share/doc/; 53 | autoindex on; 54 | allow 127.0.0.1; 55 | allow ::1; 56 | deny all; 57 | } 58 | location /deploy/ { 59 | proxy_pass http://127.0.0.1:8163; 60 | } 61 | } 62 | ``` 63 | 64 | Web Hook Guide 65 | =============== 66 | 67 | ## Add your web hook server URL to your repo 68 | YourPorject --> Settings --> Web Hooks --> URL values is Your web hook server RUL --> click "Add web Hook"。 69 | 70 | ### Make sure your repo is Publick 71 | YourPorject --> Settings --> Edit Project --> select Public mode: Public access。 72 | 73 | ### deploy a branch(default is `master`) and a tag 74 | The web hook server is `http://myserver.com/deploy/[?branch=branch1&tag=v1.0.0]` 75 | 76 | eg. switch to `branch1` 77 | 78 | ```javascript 79 | http://myserver.com/deploy/?branch=branch1 80 | ``` 81 | 82 | switch `branch1` and tag `v1.0.1`: 83 | 84 | ```javascript 85 | http://myserver.com/deploy/?branch=branch1&tag=v1.0.1 86 | ``` 87 | 88 | ### use bower|grunt|gulp task 89 | ```javascript 90 | //run bower update task 91 | http://myserver.com/deploy/?bower=update 92 | 93 | //run grunt default task,task name is required! 94 | http://myserver.com/deploy/?grunt=default 95 | 96 | //run gulp default task,task name is required! 97 | http://myserver.com/deploy/?gulp=default 98 | 99 | //run bower update then run gulp default task 100 | http://myserver.com/deploy/?bower=update&gulp=default 101 | ``` 102 | 103 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var http = require('http'); 3 | var Url = require('url'); 4 | var util = require('util'); 5 | var colors = require('colors'); 6 | var spawn = require('child_process').spawn; 7 | var exec = require('child_process').exec; 8 | var fs = require('fs'); 9 | 10 | //prevent node.js from crashing 11 | process.on('uncaughtException', function (err) { 12 | console.log("process uncaughtException error"); 13 | console.error(err); 14 | }); 15 | 16 | var excuteArgvs = (function(){ 17 | var p = process.argv[2] || 8163, 18 | g = process.argv[3]?process.argv[3].split(','):[]; 19 | return { 20 | port: p, 21 | gitHosts: g 22 | } 23 | })(); 24 | 25 | var HOST = '0.0.0.0'; 26 | var PORT = excuteArgvs.port; 27 | var GIT_HOSTS = excuteArgvs.gitHosts || []; 28 | /** 29 | * @param params={ 30 | * branch, 31 | * tag, 32 | * bower, 33 | * grunt, 34 | * gulp 35 | * } 36 | */ 37 | function webhookCallback(data, params) { 38 | console.log('*** gitlabhook ***'); 39 | var repo = data.repository.git_http_url; 40 | var user_name = data.user_name; 41 | //fix gitlab 7.X+ 42 | if (user_name == 'Administrator') { 43 | user_name = repo.match(/.*\/(.*)\/.*\.git/)[1]; 44 | } 45 | 46 | //data 47 | if (data && user_name) { 48 | var path = '/home/'; 49 | console.log('data : '); 50 | console.log(data); 51 | 52 | //user 53 | fs.exists(path + user_name, function (exists) { 54 | path += exists ? user_name : 'public'; 55 | console.log('user path : ' + path); 56 | if (data.repository && data.repository.name) { 57 | var projectName = data.repository.name;//data.repository.name.toLowerCase(); 58 | //repo 59 | var passrepo = false; 60 | for(var i = 0;i