├── .gitignore ├── README.md ├── deploy ├── deploy_1602E.sh ├── deploy_1603C.sh ├── deploy_1605A.sh ├── deploy_1606A.sh ├── deploy_1609B.sh └── deploy_1611B.sh └── webhooks ├── deploy.sh ├── git_push_hooks ├── app.js ├── bin │ └── www ├── package.json ├── public │ ├── index.html │ └── stylesheets │ │ └── style.css └── routes │ ├── index.js │ ├── post.js │ └── users.js ├── push_hooks ├── app.js ├── bin │ └── www ├── package.json ├── public │ └── stylesheets │ │ └── style.css ├── routes │ ├── index.js │ ├── post.js │ └── users.js └── views │ ├── error.pug │ ├── index.pug │ └── layout.pug ├── webhooks.js ├── webhooks_1603C.js ├── webhooks_1605A.js ├── webhooks_1606A.js ├── webhooks_1609B.js └── webhooks_1611B.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | node_modules/ 5 | 6 | # vue-element-admin/ 7 | 8 | # testing 9 | /coverage 10 | /service 11 | 12 | # classes 13 | /1601N 14 | /1602B 15 | /1602E 16 | /1603A 17 | /1603C 18 | /1605A 19 | /1605B 20 | /1606A 21 | /1609A 22 | /1609B 23 | /1611B 24 | /1612B 25 | /1701E 26 | /1702E 27 | /1704B 28 | /1704E 29 | /1706B 30 | /1706E 31 | /1707B 32 | /1709A 33 | /1711A 34 | /1802A 35 | /1803B 36 | /1805A 37 | /exam 38 | /8888 39 | 40 | check-score/ 41 | # production 42 | /build 43 | 44 | # �ӿ� 45 | /document 46 | 47 | # misc 48 | .DS_Store 49 | .env.local 50 | .env.development.local 51 | .env.test.local 52 | .env.production.local 53 | 54 | npm-debug.log* 55 | yarn-debug.log* 56 | yarn-error.log* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 我爱学习,学习使我快乐 2 | 3 | 4 | ## git日常操作 5 | - 克隆仓库:git clone git@github.com:jasonandjay/project.git 6 | - 拉取代码:git pull origin master 7 | - 新建分支:git branch chenmanjie 8 | - 删除分支:git branch -d chenmanjie 9 | - 切换分支:git checkout chenmanjie 10 | - 推送远程:git push origin master 11 | - 查看提交记录:git log 12 | - 回滚代码: git reset --hard(--soft) [commit:6] 13 | - 回滚文件: git checkout -- filepath 14 | - 回到最新代码:git reset HEAD | git pull origin master 15 | - 隐藏当前分支改动: git stash 16 | - 回退隐藏:git stash pop 17 | - 合并分支:git merge [your branch] 18 | - 会用PR给他人仓库贡献代码 19 | 20 | ## linux日常操作 21 | - 善用tab健,帮我们补全命令和路径 22 | - 切换文件夹: cd 23 | - 查看当前路径:pwd 24 | - 查看当前文件夹下的文件: ls+ll 25 | - 创建文件夹:mkdir 26 | - 移动文件:mv 27 | - 复制文件:cp 28 | - 删除文件及文件夹:rm -rf(禁止使用) 29 | - 编辑文件:vim 30 | - 查看历史命令:history 31 | 32 | ## vue项目部署到服务器上,服务器使用nginx 33 | ### 简单版 模板为:webpack-simple 34 | - 注释掉.gitignore里的dist,既在dist前面加个#号 35 | - 修改index.html里/dist/build.js为相对路径,在/dist前面加个. 36 | - 修改webpack.config.js里的publickPath为相对路径,在/dist前面加个. 37 | 38 | ### 复杂版 模板为:webpack 39 | - 注释掉.gitignore里的dist,既在dist前面加个#号 40 | - 修改config/index.js里的build/assetsPublicPath为相对路径,在/前面加个. 41 | 42 | ### 遇到404,修改文件夹的权限 43 | - chmod 666 -R [dir] 44 | 45 | ## 服务器配置 46 | ### nginx虚拟域名配置 47 | - vim /etc/nginx/sites-available/default 48 | ### 配置push钩子,自动化部署代码 49 | - 钩子:/home/ubuntu/server/webhooks/1602E/chenmanjie/webhooks.js 50 | - 部署代码:/home/ubuntu/deploy/1602E/chenmanjie/deploy.sh 51 | - 配置教程:https://www.jianshu.com/p/e4cacd775e5b 52 | - 更改bash不能运行: 53 | - 报错/bin/bash^M: bad interpreter: No such file or directory 54 | - 更改文件格式:https://www.cnblogs.com/zyb-pp/p/6429448.html 55 | ### 配置邮件服务器,在push钩子里触发,自动通知收件人 56 | 57 | ### 线上公用接口 58 | - 公共服务域名,支持https service.jasonandjay.com 59 | #### 文件上传 60 | - formData文件上传 post http://123.206.55.50:11000/upload 61 | ```html 62 | 63 | ``` 64 | ```js 65 | 支持文件上传,用post方式提交formData对象,键为文件名,值为文件 66 | eg: 67 | var ele = document.querySelector('input'); 68 | ele.onchange = function(e){ 69 | console.log('e...', e); 70 | let files = e.target.files; 71 | // 创建一个formData 72 | let form = new FormData(); 73 | for (let i=0,len=files.length; i{ 83 | console.log('body...', body); 84 | }).catch(e=>{ 85 | console.log('e..', e); 86 | }) 87 | 88 | // jquery版本 89 | let input = document.querySelector('input'); 90 | input.onchange = function(e){ 91 | console.log('e..', e.target.files); 92 | let form = new FormData(); 93 | for (let i=0,len=e.target.files.length; i 115 | ``` 116 | ``` js 117 | var ele = document.querySelector('input'); 118 | ele.onchange = function(e){ 119 | let files = e.target.files; 120 | var reader = new FileReader(); 121 | reader.onload = function(){ 122 | console.log('result...', this.result); 123 | axios({ 124 | method: 'post', 125 | url: 'http://123.206.55.50:11000/upload_base64', 126 | data: {base64: this.result} 127 | }).then(body=>{ 128 | console.log('body...', body); 129 | }).catch(e=>{ 130 | console.log('e..', e); 131 | }) 132 | } 133 | reader.readAsDataURL(files[0]); 134 | } 135 | ``` 136 | - 图片转成base64 post http://123.206.55.50:11000/tobase64 137 | ``` js 138 | axios({ 139 | method: 'post', 140 | url: 'http://123.206.55.50:11000/tobase64', 141 | data: {url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3360034032,4096528553&fm=26&gp=0.jpg'} 142 | }).then(body=>{ 143 | console.log('body...', body); 144 | }).catch(e=>{ 145 | console.log('e..', e); 146 | }) 147 | ``` 148 | 149 | #### 短信验证码接口 150 | - 发送短信验证码 http://123.206.55.50:11000/smsCode 五分钟有效期 151 | ``` 152 | post phone='your phone' 153 | ``` 154 | - 验证短信验证码 155 | -------------------------------------------------------------------------------- /deploy/deploy_1602E.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1602E/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo "Start deployment" 8 | cd $WEB_PATH 9 | echo "pulling source code..." 10 | git reset --hard origin/master 11 | git clean -f 12 | git pull 13 | git checkout master 14 | echo "changing permissions..." 15 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 16 | echo "Finished." -------------------------------------------------------------------------------- /deploy/deploy_1603C.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1603C/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo $WEB_PATH 8 | echo "Start deployment" 9 | cd $WEB_PATH 10 | echo "pulling source code..." 11 | git reset --hard origin/master 12 | git clean -f 13 | git pull 14 | git checkout master 15 | echo "changing permissions..." 16 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 17 | echo "Finished." -------------------------------------------------------------------------------- /deploy/deploy_1605A.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1605A/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo $WEB_PATH 8 | echo "Start deployment" 9 | cd $WEB_PATH 10 | echo "pulling source code..." 11 | git reset --hard origin/master 12 | git clean -f 13 | git pull 14 | git checkout master 15 | echo "changing permissions..." 16 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 17 | echo "Finished." -------------------------------------------------------------------------------- /deploy/deploy_1606A.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1609B/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo $WEB_PATH 8 | echo "Start deployment" 9 | cd $WEB_PATH 10 | echo "pulling source code..." 11 | git reset --hard origin/master 12 | git clean -f 13 | git pull 14 | git checkout master 15 | echo "changing permissions..." 16 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 17 | echo "Finished." -------------------------------------------------------------------------------- /deploy/deploy_1609B.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1609B/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo $WEB_PATH 8 | echo "Start deployment" 9 | cd $WEB_PATH 10 | echo "pulling source code..." 11 | git reset --hard origin/master 12 | git clean -f 13 | git pull 14 | git checkout master 15 | echo "changing permissions..." 16 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 17 | echo "Finished." -------------------------------------------------------------------------------- /deploy/deploy_1611B.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1611B/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo $WEB_PATH 8 | echo "Start deployment" 9 | cd $WEB_PATH 10 | echo "pulling source code..." 11 | git reset --hard origin/master 12 | git clean -f 13 | git pull 14 | git checkout master 15 | echo "changing permissions..." 16 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 17 | echo "Finished." -------------------------------------------------------------------------------- /webhooks/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | WEB_PATH='/home/ubuntu/1602E/chenmanjie/'$1 4 | WEB_USER='ubuntu' 5 | WEB_USERGROUP='ubuntu' 6 | 7 | echo "Start deployment" 8 | cd $WEB_PATH 9 | echo "pulling source code..." 10 | git reset --hard origin/master 11 | git clean -f 12 | git pull 13 | git checkout master 14 | echo "changing permissions..." 15 | chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH 16 | echo "Finished." -------------------------------------------------------------------------------- /webhooks/git_push_hooks/app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var cookieParser = require('cookie-parser'); 4 | var logger = require('morgan'); 5 | 6 | var indexRouter = require('./routes/index'); 7 | var usersRouter = require('./routes/users'); 8 | var postRouter = require('./routes/post'); 9 | 10 | var app = express(); 11 | 12 | app.use(logger('dev')); 13 | app.use(express.json()); 14 | app.use(express.urlencoded({ extended: false })); 15 | app.use(cookieParser()); 16 | app.use(express.static(path.join(__dirname, 'public'))); 17 | 18 | app.use('/', indexRouter); 19 | app.use('/users', usersRouter); 20 | app.use('/post', postRouter); 21 | 22 | module.exports = app; 23 | -------------------------------------------------------------------------------- /webhooks/git_push_hooks/bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('git-push-hooks:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '3000'); 16 | app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | /** 33 | * Normalize a port into a number, string, or false. 34 | */ 35 | 36 | function normalizePort(val) { 37 | var port = parseInt(val, 10); 38 | 39 | if (isNaN(port)) { 40 | // named pipe 41 | return val; 42 | } 43 | 44 | if (port >= 0) { 45 | // port number 46 | return port; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | /** 53 | * Event listener for HTTP server "error" event. 54 | */ 55 | 56 | function onError(error) { 57 | if (error.syscall !== 'listen') { 58 | throw error; 59 | } 60 | 61 | var bind = typeof port === 'string' 62 | ? 'Pipe ' + port 63 | : 'Port ' + port; 64 | 65 | // handle specific listen errors with friendly messages 66 | switch (error.code) { 67 | case 'EACCES': 68 | console.error(bind + ' requires elevated privileges'); 69 | process.exit(1); 70 | break; 71 | case 'EADDRINUSE': 72 | console.error(bind + ' is already in use'); 73 | process.exit(1); 74 | break; 75 | default: 76 | throw error; 77 | } 78 | } 79 | 80 | /** 81 | * Event listener for HTTP server "listening" event. 82 | */ 83 | 84 | function onListening() { 85 | var addr = server.address(); 86 | var bind = typeof addr === 'string' 87 | ? 'pipe ' + addr 88 | : 'port ' + addr.port; 89 | debug('Listening on ' + bind); 90 | } 91 | -------------------------------------------------------------------------------- /webhooks/git_push_hooks/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "git-push-hooks", 3 | "version": "0.0.0", 4 | "private": true, 5 | "scripts": { 6 | "prod": "cross-env PORT=10001 node bin/www", 7 | "start": "cross-env PORT=10001 node-dev ./bin/www" 8 | }, 9 | "dependencies": { 10 | "cookie-parser": "~1.4.3", 11 | "debug": "~2.6.9", 12 | "express": "~4.16.0", 13 | "github-webhook-handler": "^0.7.1", 14 | "morgan": "~1.9.0" 15 | }, 16 | "devDependencies": { 17 | "cross-env": "^5.2.0", 18 | "node-dev": "^3.1.3" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /webhooks/git_push_hooks/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Express 5 | 6 | 7 | 8 | 9 |

Express

10 |

Welcome to Express

11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /webhooks/git_push_hooks/public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } 9 | -------------------------------------------------------------------------------- /webhooks/git_push_hooks/routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | res.render('index', { title: 'Express' }); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /webhooks/git_push_hooks/routes/post.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | const createHandler = require('github-webhook-handler') 4 | 5 | /* GET users listing. */ 6 | router.get('/', function(req, res, next) { 7 | res.send('respond with a resource'); 8 | }); 9 | 10 | // 执行部署脚本 11 | function run_cmd(cmd, args, callback) { 12 | var spawn = require('child_process').spawn; 13 | var child = spawn(cmd, args); 14 | var resp = ""; 15 | 16 | child.stdout.on('data', function (buffer) { 17 | resp += buffer.toString(); 18 | }); 19 | child.stdout.on('end', function () { 20 | callback(resp) 21 | }); 22 | } 23 | 24 | // WebHooks处理实例 25 | function handlePostHooks(path, req, res){ 26 | const handler = createHandler({ 27 | path: '/1605A', 28 | secret: 'jasonandjay' 29 | }) 30 | handler.on('error', function (err) { 31 | console.error('Error:', err.message) 32 | }) 33 | handler.on('push', function (event) { 34 | console.log('Received a push event for %s to %s', 35 | event.payload.repository.name, 36 | event.payload.ref); 37 | // run_cmd('sh', ['/home/ubuntu/deploy/deploy_1603C.sh', event.payload.repository.name], function (text) { 38 | // console.log(text) 39 | // // 发送邮件给邮件列表联系人,通知有代码发布了 40 | // sendMials(event); 41 | // }); 42 | }) 43 | handler(req, res, function(err){ 44 | console.log('error.., no such location') 45 | res.statusCode = 404; 46 | res.send('no such location'); 47 | }); 48 | } 49 | 50 | // 1605A自动化部署钩子 51 | router.post('/1605A', function (req, res, next) { 52 | console.log('url...', req.url); 53 | handlePostHooks('/1605A', req, res); 54 | }) 55 | 56 | // 1606A自动化部署钩子 57 | router.post('/1606A', function (req, res, next) { 58 | handlePostHooks('/post/1606A', req, res); 59 | }) 60 | 61 | 62 | module.exports = router; -------------------------------------------------------------------------------- /webhooks/git_push_hooks/routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET users listing. */ 5 | router.get('/', function(req, res, next) { 6 | res.send('respond with a resource'); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /webhooks/push_hooks/app.js: -------------------------------------------------------------------------------- 1 | const Koa = require('koa') 2 | const app = new Koa() 3 | const views = require('koa-views') 4 | const json = require('koa-json') 5 | const onerror = require('koa-onerror') 6 | const bodyparser = require('koa-bodyparser') 7 | const logger = require('koa-logger') 8 | 9 | // const index = require('./routes/index') 10 | // const users = require('./routes/users') 11 | const post = require('./routes/post') 12 | 13 | // error handler 14 | onerror(app) 15 | 16 | // middlewares 17 | app.use(bodyparser({ 18 | enableTypes:['json', 'form', 'text'] 19 | })) 20 | app.use(json()) 21 | app.use(logger()) 22 | app.use(require('koa-static')(__dirname + '/public')) 23 | 24 | app.use(views(__dirname + '/views', { 25 | extension: 'pug' 26 | })) 27 | 28 | // logger 29 | app.use(async (ctx, next) => { 30 | const start = new Date() 31 | await next() 32 | const ms = new Date() - start 33 | console.log(`${ctx.method} ${ctx.url} - ${ms}ms`) 34 | }) 35 | 36 | // routes 37 | // app.use(index.routes(), index.allowedMethods()) 38 | // app.use(users.routes(), users.allowedMethods()) 39 | app.use(post.routes(), post.allowedMethods()) 40 | 41 | // error-handling 42 | app.on('error', (err, ctx) => { 43 | console.error('server error', err, ctx) 44 | }); 45 | 46 | module.exports = app 47 | -------------------------------------------------------------------------------- /webhooks/push_hooks/bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('demo:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '3000'); 16 | // app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app.callback()); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | /** 33 | * Normalize a port into a number, string, or false. 34 | */ 35 | 36 | function normalizePort(val) { 37 | var port = parseInt(val, 10); 38 | 39 | if (isNaN(port)) { 40 | // named pipe 41 | return val; 42 | } 43 | 44 | if (port >= 0) { 45 | // port number 46 | return port; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | /** 53 | * Event listener for HTTP server "error" event. 54 | */ 55 | 56 | function onError(error) { 57 | if (error.syscall !== 'listen') { 58 | throw error; 59 | } 60 | 61 | var bind = typeof port === 'string' 62 | ? 'Pipe ' + port 63 | : 'Port ' + port; 64 | 65 | // handle specific listen errors with friendly messages 66 | switch (error.code) { 67 | case 'EACCES': 68 | console.error(bind + ' requires elevated privileges'); 69 | process.exit(1); 70 | break; 71 | case 'EADDRINUSE': 72 | console.error(bind + ' is already in use'); 73 | process.exit(1); 74 | break; 75 | default: 76 | throw error; 77 | } 78 | } 79 | 80 | /** 81 | * Event listener for HTTP server "listening" event. 82 | */ 83 | 84 | function onListening() { 85 | var addr = server.address(); 86 | var bind = typeof addr === 'string' 87 | ? 'pipe ' + addr 88 | : 'port ' + addr.port; 89 | debug('Listening on ' + bind); 90 | } 91 | -------------------------------------------------------------------------------- /webhooks/push_hooks/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "push_hooks", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "cross-env PORT=10001 node bin/www", 7 | "dev": "cross-env PORT=10001 ./node_modules/.bin/nodemon bin/www", 8 | "prd": "pm2 start bin/www", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "dependencies": { 12 | "debug": "^2.6.3", 13 | "github-webhook-handler": "^0.7.1", 14 | "koa": "^2.2.0", 15 | "koa-bodyparser": "^3.2.0", 16 | "koa-convert": "^1.2.0", 17 | "koa-json": "^2.0.2", 18 | "koa-logger": "^2.0.1", 19 | "koa-onerror": "^1.2.1", 20 | "koa-router": "^7.1.1", 21 | "koa-static": "^3.0.0", 22 | "koa-views": "^5.2.1", 23 | "pug": "^2.0.0-rc.1" 24 | }, 25 | "devDependencies": { 26 | "cross-env": "^5.2.0", 27 | "nodemon": "^1.8.1" 28 | } 29 | } -------------------------------------------------------------------------------- /webhooks/push_hooks/public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } 9 | -------------------------------------------------------------------------------- /webhooks/push_hooks/routes/index.js: -------------------------------------------------------------------------------- 1 | const router = require('koa-router')() 2 | 3 | router.get('/', async (ctx, next) => { 4 | await ctx.render('index', { 5 | title: 'Hello Koa 2!' 6 | }) 7 | }) 8 | 9 | router.get('/string', async (ctx, next) => { 10 | ctx.body = 'koa2 string' 11 | }) 12 | 13 | router.get('/json', async (ctx, next) => { 14 | ctx.body = { 15 | title: 'koa2 json' 16 | } 17 | }) 18 | 19 | module.exports = router 20 | -------------------------------------------------------------------------------- /webhooks/push_hooks/routes/post.js: -------------------------------------------------------------------------------- 1 | const router = require('koa-router')() 2 | const createHandler = require('github-webhook-handler') 3 | 4 | // 执行部署脚本 5 | function run_cmd(cmd, args, callback) { 6 | var spawn = require('child_process').spawn; 7 | var child = spawn(cmd, args); 8 | var resp = ""; 9 | 10 | child.stdout.on('data', function (buffer) { 11 | resp += buffer.toString(); 12 | }); 13 | child.stdout.on('end', function () { 14 | callback(resp) 15 | }); 16 | } 17 | 18 | // WebHooks处理实例 19 | function handlePostHooks(path, ctx){ 20 | const handler = createHandler({ 21 | path: path, 22 | secret: 'jasonandjay' 23 | }) 24 | handler.on('error', function (err) { 25 | console.error('Error:', err.message) 26 | }) 27 | handler.on('push', function (event) { 28 | console.log('Received a push event for %s to %s', 29 | event.payload.repository.name, 30 | event.payload.ref); 31 | // run_cmd('sh', ['/home/ubuntu/deploy/deploy_1603C.sh', event.payload.repository.name], function (text) { 32 | // console.log(text) 33 | // // 发送邮件给邮件列表联系人,通知有代码发布了 34 | // sendMials(event); 35 | // }); 36 | }) 37 | let {req, res} = ctx; 38 | // console.log('req...', req); 39 | // console.log('res...', res); 40 | handler(req, ctx.res, function(err){ 41 | console.log('error.., no such location') 42 | // ctx.request.statusCode = 404; 43 | // ctx.body('no such location'); 44 | }); 45 | } 46 | // 添加路由前缀 47 | router.prefix('/post') 48 | 49 | // 1605A自动化部署钩子 50 | router.post('/1605A', function (ctx, next) { 51 | handlePostHooks('/post/1605A', ctx); 52 | }) 53 | 54 | // 1606A自动化部署钩子 55 | router.post('/1606A', function (ctx, next) { 56 | handlePostHooks('/post/1606A', ctx); 57 | }) 58 | 59 | router.get('/', function (ctx, next) { 60 | ctx.body = 'this is a post/bar response' 61 | }) 62 | 63 | module.exports = router 64 | -------------------------------------------------------------------------------- /webhooks/push_hooks/routes/users.js: -------------------------------------------------------------------------------- 1 | const router = require('koa-router')() 2 | 3 | router.prefix('/users') 4 | 5 | router.get('/', function (ctx, next) { 6 | ctx.body = 'this is a users response!' 7 | }) 8 | 9 | router.get('/bar', function (ctx, next) { 10 | ctx.body = 'this is a users/bar response' 11 | }) 12 | 13 | module.exports = router 14 | -------------------------------------------------------------------------------- /webhooks/push_hooks/views/error.pug: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= message 5 | h2= error.status 6 | pre #{error.stack} 7 | -------------------------------------------------------------------------------- /webhooks/push_hooks/views/index.pug: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= title 5 | p Welcome to #{title} 6 | -------------------------------------------------------------------------------- /webhooks/push_hooks/views/layout.pug: -------------------------------------------------------------------------------- 1 | doctype html 2 | html 3 | head 4 | title= title 5 | link(rel='stylesheet', href='/stylesheets/style.css') 6 | body 7 | block content 8 | -------------------------------------------------------------------------------- /webhooks/webhooks.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var createHandler = require('github-webhook-handler') 3 | var nodemailer = require('nodemailer'); 4 | 5 | var handler = createHandler({ 6 | path: '/', 7 | secret: 'jasonandjay' 8 | }) 9 | // 上面的 secret 保持和 GitHub 后台设置的一致 10 | 11 | function run_cmd(cmd, args, callback) { 12 | var spawn = require('child_process').spawn; 13 | var child = spawn(cmd, args); 14 | var resp = ""; 15 | 16 | child.stdout.on('data', function (buffer) { 17 | resp += buffer.toString(); 18 | }); 19 | child.stdout.on('end', function () { 20 | callback(resp) 21 | }); 22 | } 23 | 24 | http.createServer(function (req, res) { 25 | // 自动化部署代码 26 | handler(req, res, function (err) { 27 | res.statusCode = 404 28 | res.end('no such location') 29 | }) 30 | }).listen(10001) 31 | 32 | handler.on('error', function (err) { 33 | console.error('Error:', err.message) 34 | }) 35 | 36 | handler.on('push', function (event) { 37 | console.log('Received a push event for %s to %s', 38 | event.payload.repository.name, 39 | event.payload.ref); 40 | run_cmd('sh', ['/home/ubuntu/deploy/1602E/chenmanjie/deploy.sh', event.payload.repository.name], function (text) { 41 | console.log(text) 42 | // 发送邮件给邮件列表联系人,通知有代码发布了 43 | sendMials(event); 44 | }); 45 | }) 46 | 47 | // 封装发送邮件方法 48 | function sendMials(event){ 49 | // create reusable transporter object using the default SMTP transport 50 | let transporter = nodemailer.createTransport({ 51 | // host: 'qq', 52 | service: 'qq', 53 | port: 465, 54 | secure: true, // true for 465, false for other ports 55 | auth: { 56 | user: '342690199@qq.com', // generated ethereal user 57 | pass: 'dkheisqxisqqcabd' // generated ethereal password 58 | } 59 | }); 60 | 61 | // const receivers = '342690199@qq.com'; 62 | const receivers = `1094702071@qq.com,1193326940@qq.com,229316341@qq.com,15303580082@163.com, 63 | 1149538520@qq.com,1351876883@qq.com, 304126063@qq.com, 1359616037@qq.com, 1430790335@qq.com, 64 | 906380990@qq.com,1729423128@qq.com,3034084652@qq.com,545895878@qq.com,920552800@qq.com, 65 | 1258776978@qq.com,1457680521@qq.com,2286680249@qq.com,aaxuejie@163.com,ll825830@163.com, 66 | zsz270018@163.com,1275942695@qq.com,335136263@qq.com,2659177786@qq.com,1448405680@qq.com, 67 | 15712965708@163.com,940233351@qq.com,330775247@qq.com ,412640480@qq.com, 342690199@qq.com`; 68 | 69 | 70 | let commits = ``; 71 | event.payload.commits.forEach(item=>{ 72 | commits += `
73 |

提交版本:${item.id}

74 |

提交内容:${item.message}

75 |

提交人: ${item.author.email}

76 |

----------------------------

77 |
`; 78 | }) 79 | // 配置收件人 80 | let mailOptions = { 81 | from: '"jason老师👻" <342690199@qq.com>', // sender address 82 | to: receivers, // list of receivers 83 | subject: '代码更新通知 ✔', // Subject line 84 | html: `
85 |

今日代码仓库更新了

86 |
87 |
88 |

代码更新记录列表(倒序)

89 |
更新时间:${new Date()}
90 |

----------------------------

91 | ${commits} 92 | 点击查看 93 |
` // html body 94 | }; 95 | // 拼接提交记录 96 | console.log('部署完成,发送邮件通知用户...'); 97 | transporter.sendMail(mailOptions, (error, info) => { 98 | if (error) { 99 | return console.log(error); 100 | } 101 | console.log('Message sent: %s', info.messageId); 102 | // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com> 103 | }); 104 | } -------------------------------------------------------------------------------- /webhooks/webhooks_1603C.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var createHandler = require('github-webhook-handler') 3 | var nodemailer = require('nodemailer'); 4 | 5 | var handler = createHandler({ 6 | path: '/', 7 | secret: 'jasonandjay' 8 | }) 9 | // 上面的 secret 保持和 GitHub 后台设置的一致 10 | 11 | function run_cmd(cmd, args, callback) { 12 | var spawn = require('child_process').spawn; 13 | var child = spawn(cmd, args); 14 | var resp = ""; 15 | 16 | child.stdout.on('data', function (buffer) { 17 | resp += buffer.toString(); 18 | }); 19 | child.stdout.on('end', function () { 20 | callback(resp) 21 | }); 22 | } 23 | 24 | http.createServer(function (req, res) { 25 | // 自动化部署代码 26 | handler(req, res, function (err) { 27 | res.statusCode = 404 28 | res.end('no such location') 29 | }) 30 | }).listen(10001); 31 | console.log('监听10001端口'); 32 | 33 | handler.on('error', function (err) { 34 | console.error('Error:', err.message) 35 | }) 36 | 37 | handler.on('push', function (event) { 38 | console.log('Received a push event for %s to %s', 39 | event.payload.repository.name, 40 | event.payload.ref); 41 | run_cmd('sh', ['/home/ubuntu/deploy/deploy_1603C.sh', event.payload.repository.name], function (text) { 42 | console.log(text) 43 | // 发送邮件给邮件列表联系人,通知有代码发布了 44 | sendMials(event); 45 | }); 46 | }) 47 | 48 | // 封装发送邮件方法 49 | function sendMials(event){ 50 | // create reusable transporter object using the default SMTP transport 51 | let transporter = nodemailer.createTransport({ 52 | // host: 'qq', 53 | service: 'qq', 54 | port: 465, 55 | secure: true, // true for 465, false for other ports 56 | auth: { 57 | user: '342690199@qq.com', // generated ethereal user 58 | pass: 'dkheisqxisqqcabd' // generated ethereal password 59 | } 60 | }); 61 | 62 | // const receivers = '342690199@qq.com'; 63 | const receivers = `1052577847@qq.com,342690199@qq.com`; 64 | 65 | 66 | let commits = ``; 67 | event.payload.commits.forEach(item=>{ 68 | commits += `
69 |

提交版本:${item.id}

70 |

提交内容:${item.message}

71 |

提交人: ${item.author.email}

72 |

----------------------------

73 |
`; 74 | }) 75 | // 配置收件人 76 | let mailOptions = { 77 | from: '"jason老师👻" <342690199@qq.com>', // sender address 78 | to: receivers, // list of receivers 79 | subject: '代码更新通知 ✔', // Subject line 80 | html: `
81 |

今日代码仓库更新了

82 |
83 |
84 |

代码更新记录列表(倒序)

85 |
更新时间:${new Date()}
86 |

----------------------------

87 | ${commits} 88 | 点击查看 89 |
` // html body 90 | }; 91 | // 拼接提交记录 92 | console.log('部署完成,发送邮件通知用户...'); 93 | transporter.sendMail(mailOptions, (error, info) => { 94 | if (error) { 95 | return console.log(error); 96 | } 97 | console.log('Message sent: %s', info.messageId); 98 | // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com> 99 | }); 100 | } -------------------------------------------------------------------------------- /webhooks/webhooks_1605A.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var createHandler = require('github-webhook-handler') 3 | var nodemailer = require('nodemailer'); 4 | 5 | var handler = createHandler({ 6 | path: '/', 7 | secret: 'jasonandjay' 8 | }) 9 | // 上面的 secret 保持和 GitHub 后台设置的一致 10 | 11 | function run_cmd(cmd, args, callback) { 12 | var spawn = require('child_process').spawn; 13 | var child = spawn(cmd, args); 14 | var resp = ""; 15 | 16 | child.stdout.on('data', function (buffer) { 17 | resp += buffer.toString(); 18 | }); 19 | child.stdout.on('end', function () { 20 | callback(resp) 21 | }); 22 | } 23 | 24 | http.createServer(function (req, res) { 25 | // 自动化部署代码 26 | handler(req, res, function (err) { 27 | res.statusCode = 404 28 | res.end('no such location') 29 | }) 30 | }).listen(10002); 31 | console.log('监听10002端口'); 32 | 33 | handler.on('error', function (err) { 34 | console.error('Error:', err.message) 35 | }) 36 | 37 | handler.on('push', function (event) { 38 | console.log('Received a push event for %s to %s', 39 | event.payload.repository.name, 40 | event.payload.ref); 41 | run_cmd('sh', ['/home/ubuntu/deploy/deploy_1605A.sh', event.payload.repository.name], function (text) { 42 | console.log(text) 43 | // 发送邮件给邮件列表联系人,通知有代码发布了 44 | sendMials(event); 45 | }); 46 | }) 47 | 48 | // 封装发送邮件方法 49 | function sendMials(event){ 50 | // create reusable transporter object using the default SMTP transport 51 | let transporter = nodemailer.createTransport({ 52 | // host: 'qq', 53 | service: 'qq', 54 | port: 465, 55 | secure: true, // true for 465, false for other ports 56 | auth: { 57 | user: '342690199@qq.com', // generated ethereal user 58 | pass: 'fopwnytstewvbhbd' // generated ethereal password 59 | } 60 | }); 61 | 62 | // const receivers = '342690199@qq.com'; 63 | // const receivers = `1052577847@qq.com,342690199@qq.com`; 64 | const receivers = "506321322@qq.com,342690199@qq.com,337951079@qq.com,1727981336@qq.com,1771952134@qq.com,1224303378@qq.com,814983809@qq.com,1017831543@qq.com,1505038064@qq.com,1915289142@qq.com,daydayup0318@163.com,ws03242018@163.com,1239178592@qq.com,lizhaoting027@163.com ,5535162@qq.com,18410105520@163.com,2640649787@qq.com,1207825344@qq.com,747920960@qq.com,m17338109507@163.com,1486911647@qq.com,m15712879594@163.com,2388801730@qq.com,2671086427@qq.com"; 65 | 66 | let commits = ``; 67 | event.payload.commits.forEach(item=>{ 68 | commits += `
69 |

提交版本:${item.id}

70 |

提交内容:${item.message}

71 |

提交人: ${item.author.email}

72 |

----------------------------

73 |
`; 74 | }) 75 | // 配置收件人 76 | let mailOptions = { 77 | from: '"jason老师👻" <342690199@qq.com>', // sender address 78 | to: receivers, // list of receivers 79 | subject: '代码更新通知 ✔', // Subject line 80 | html: `
81 |

今日代码仓库更新了

82 |
83 |
84 |

代码更新记录列表(倒序)

85 |
更新时间:${new Date()}
86 |

----------------------------

87 | ${commits} 88 | 点击查看 89 |
` // html body 90 | }; 91 | // 拼接提交记录 92 | console.log('部署完成,发送邮件通知用户...'); 93 | transporter.sendMail(mailOptions, (error, info) => { 94 | if (error) { 95 | return console.log(error); 96 | } 97 | console.log('Message sent: %s', info.messageId); 98 | // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com> 99 | }); 100 | } -------------------------------------------------------------------------------- /webhooks/webhooks_1606A.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var createHandler = require('github-webhook-handler') 3 | var nodemailer = require('nodemailer'); 4 | 5 | var handler = createHandler({ 6 | path: '/', 7 | secret: 'jasonandjay' 8 | }) 9 | // 上面的 secret 保持和 GitHub 后台设置的一致 10 | 11 | function run_cmd(cmd, args, callback) { 12 | var spawn = require('child_process').spawn; 13 | var child = spawn(cmd, args); 14 | var resp = ""; 15 | 16 | child.stdout.on('data', function (buffer) { 17 | resp += buffer.toString(); 18 | }); 19 | child.stdout.on('end', function () { 20 | callback(resp) 21 | }); 22 | } 23 | 24 | http.createServer(function (req, res) { 25 | console.log('接收到请求...'); 26 | // 自动化部署代码 27 | handler(req, res, function (err) { 28 | res.statusCode = 404 29 | res.end('no such location') 30 | }) 31 | }).listen(10003); 32 | console.log('监听10003端口....'); 33 | 34 | handler.on('error', function (err) { 35 | console.error('Error:', err.message) 36 | }) 37 | 38 | handler.on('push', function (event) { 39 | console.log('Received a push event for %s to %s', 40 | event.payload.repository.name, 41 | event.payload.ref); 42 | run_cmd('sh', ['/home/ubuntu/deploy/deploy_1606A.sh', event.payload.repository.name], function (text) { 43 | console.log(text) 44 | // 发送邮件给邮件列表联系人,通知有代码发布了 45 | sendMials(event); 46 | }); 47 | }) 48 | 49 | // 封装发送邮件方法 50 | function sendMials(event){ 51 | // create reusable transporter object using the default SMTP transport 52 | let transporter = nodemailer.createTransport({ 53 | // host: 'qq', 54 | service: 'qq', 55 | port: 465, 56 | secure: true, // true for 465, false for other ports 57 | auth: { 58 | user: '342690199@qq.com', // generated ethereal user 59 | pass: 'iyorthdinrjrcajh' // generated ethereal password 60 | } 61 | }); 62 | 63 | const receivers = '342690199@qq.com,80645885@qq.com,1332751890@qq.com,2683382021@qq.com'; 64 | // const receivers = `1052577847@qq.com,342690199@qq.com`; 65 | // const receivers = "506321322@qq.com,342690199@qq.com,337951079@qq.com,1727981336@qq.com,1771952134@qq.com,1224303378@qq.com,814983809@qq.com,1017831543@qq.com,1505038064@qq.com,1915289142@qq.com,daydayup0318@163.com,ws03242018@163.com,1239178592@qq.com,lizhaoting027@163.com ,5535162@qq.com,18410105520@163.com,2640649787@qq.com,1207825344@qq.com,747920960@qq.com,m17338109507@163.com,1486911647@qq.com,m15712879594@163.com,2388801730@qq.com,2671086427@qq.com"; 66 | 67 | let commits = ``; 68 | event.payload.commits.forEach(item=>{ 69 | commits += `
70 |

提交版本:${item.id}

71 |

提交内容:${item.message}

72 |

提交人: ${item.author.email}

73 |

----------------------------

74 |
`; 75 | }) 76 | // 配置发件人 77 | let mailOptions = { 78 | from: '"jason老师👻" <342690199@qq.com>', // sender address 79 | to: receivers, // list of receivers 80 | subject: '代码更新通知 ✔', // Subject line 81 | html: `
82 |

今日代码仓库更新了

83 |
84 |
85 |

代码更新记录列表(倒序)

86 |
更新时间:${new Date()}
87 |

----------------------------

88 | ${commits} 89 | 点击查看 90 |
` // html body 91 | }; 92 | // 拼接提交记录 93 | console.log('部署完成,发送邮件通知用户...'); 94 | transporter.sendMail(mailOptions, (error, info) => { 95 | if (error) { 96 | return console.log(error); 97 | } 98 | console.log('Message sent: %s', info.messageId); 99 | // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com> 100 | }); 101 | } -------------------------------------------------------------------------------- /webhooks/webhooks_1609B.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var createHandler = require('github-webhook-handler') 3 | 4 | // 上面的 secret 保持和 GitHub 后台设置的一致 5 | var handler = createHandler({ 6 | path: '/', 7 | secret: 'jasonandjay' 8 | }) 9 | 10 | function run_cmd(cmd, args, callback) { 11 | var spawn = require('child_process').spawn; 12 | var child = spawn(cmd, args); 13 | var resp = ""; 14 | 15 | child.stdout.on('data', function (buffer) { 16 | resp += buffer.toString(); 17 | }); 18 | child.stdout.on('end', function () { 19 | callback(resp) 20 | }); 21 | } 22 | 23 | // 起服务,把请求交给handle处理 24 | http.createServer(function (req, res) { 25 | console.log('接收到请求...'); 26 | // 自动化部署代码 27 | handler(req, res, function (err) { 28 | res.statusCode = 404 29 | res.end('no such location') 30 | }) 31 | }).listen(10004); 32 | console.log('监听10004端口....'); 33 | 34 | // 当请求出错的时候 35 | handler.on('error', function (err) { 36 | console.error('Error:', err.message) 37 | }) 38 | 39 | // 监听到webhooks的push事件 40 | handler.on('push', function (event) { 41 | console.log('Received a push event for %s to %s', 42 | event.payload.repository.name, 43 | event.payload.ref); 44 | run_cmd('sh', ['/home/ubuntu/deploy/deploy_1609B.sh', event.payload.repository.name], function (text) { 45 | console.log(text) 46 | // 发送邮件给邮件列表联系人,通知有代码发布了 47 | // sendMials(event); 48 | }); 49 | }) -------------------------------------------------------------------------------- /webhooks/webhooks_1611B.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var createHandler = require('github-webhook-handler') 3 | 4 | // 上面的 secret 保持和 GitHub 后台设置的一致 5 | var handler = createHandler({ 6 | path: '/', 7 | secret: 'jasonandjay' 8 | }) 9 | 10 | function run_cmd(cmd, args, callback) { 11 | var spawn = require('child_process').spawn; 12 | var child = spawn(cmd, args); 13 | var resp = ""; 14 | 15 | child.stdout.on('data', function (buffer) { 16 | resp += buffer.toString(); 17 | }); 18 | child.stdout.on('end', function () { 19 | callback(resp) 20 | }); 21 | } 22 | 23 | // 起服务,把请求交给handle处理 24 | http.createServer(function (req, res) { 25 | console.log('接收到请求...'); 26 | // 自动化部署代码 27 | handler(req, res, function (err) { 28 | res.statusCode = 404 29 | res.end('no such location') 30 | }) 31 | }).listen(10005); 32 | console.log('监听10004端口....'); 33 | 34 | // 当请求出错的时候 35 | handler.on('error', function (err) { 36 | console.error('Error:', err.message) 37 | }) 38 | 39 | // 监听到webhooks的push事件 40 | handler.on('push', function (event) { 41 | console.log('Received a push event for %s to %s', 42 | event.payload.repository.name, 43 | event.payload.ref); 44 | run_cmd('sh', ['/home/ubuntu/deploy/deploy_1609B.sh', event.payload.repository.name], function (text) { 45 | console.log(text) 46 | // 发送邮件给邮件列表联系人,通知有代码发布了 47 | // sendMials(event); 48 | }); 49 | }) --------------------------------------------------------------------------------