Docker-Compose
// 强制重新构建并启
# --force-recreate 强制重建容器
# --build 强制编译
docker-compose up -d --force-recreate --build
#docker-compose.yml
version: '3.1'
services:
nginx:
image: nginx:kaikeba
ports:
- 80:80
# 运⾏
docker-compose up
# 后台运⾏
docker-compose up -d
部署Mongo + MongoExpress
#docker-compose.yml
version: '3.1'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
代码中添加Mongoose调⽤
// mongoose.js
const mongoose = require("mongoose");
// 1.连接
mongoose.connect("mongodb://mongo:27017/test", { useNewUrlParser: true });
const conn = mongoose.connection;
conn.on("error", () => console.error("连接数据库失败"));
// app.js
const mongoose = require('mongoose');
mongoose.connect('mongodb://mongo:27017/test', {useNewUrlParser: true});
const Cat = mongoose.model('Cat', { name: String });
Cat.deleteMany({})
const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
app.use(async ctx => {
ctx.body = await Cat.find()
})
Github WebHook实现CI持续集成
启动NodeJS监听
var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler({ path: '/webhooks', secret: 'myHashSecret' })
// 上⾯的 secret 保持和 GitHub 后台设置的⼀致
function run_cmd(cmd, args, callback) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";
child.stdout.on('data', function (buffer) { resp += buffer.toString(); });
child.stdout.on('end', function () { callback(resp) });
}
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(3000)
handler.on('error', function (err) {
console.error('Error:', err.message)
})
handler.on('*', function (event) {
console.log('Received *', event.payload.action);
// run_cmd('sh', ['./deploy-dev.sh'], function(text){ console.log(text)
});
})
handler.on('push', function (event) {
console.log('Received a push event for %s to %s',
event.payload.repository.name,
event.payload.ref);
// 分⽀判断
if(event.payload.ref === 'refs/heads/master'){
console.log('deploy master..')
}
// run_cmd('sh', ['./deploy-dev.sh'], function(text){ console.log(text)
});
})
handler.on('issues', function (event) {
console.log('Received an issue event for % action=%s: #%d %s',
event.payload.repository.name,
event.payload.action,
event.payload.issue.number,
event.payload.issue.title)
})