重点在这
使用 TravisCI 配合 Shipit 进行自动化部署
所有代码在这个地方 代码仓库
这里贴出来一些关键性代码
shipitfile.js
'use strict';
module.exports = function(shipit) {
require('shipit-deploy')(shipit);
shipit.initConfig({
default: {
workspace: '/tmp/github-monitor',
deployTo: '/home/nono/app',
repositoryUrl: 'https://github.com/MiYogurt/deploy-egg-sample.git',
ignores: [ '.git', 'node_modules' ],
rsync: [ '--del' ],
keepReleases: 2,
key: './scripts/source.key',
shallowClone: true,
},
staging: {
servers: 'root@139.199.227.41',
},
});
shipit.task('docker', function() {
return shipit.start([ 'build', 'remove', 'create' ]);
});
shipit.blTask('build', function() {
return shipit.remote('docker build -t nodelover:v1 .', {
cwd: '/home/nono/app/current',
});
});
shipit.blTask('create', function() {
return shipit.remote('docker run -d --name app -p 8080:7001 nodelover:v1', {
cwd: '/home/nono/app/current',
});
});
shipit.blTask('remove', function() {
return shipit.remote('docker stop app', {
cwd: '/home/nono/app/current',
}).then(o => shipit.remote('docker rm app', {
cwd: '/home/nono/app/current',
})).catch(err => console.log("no need stop"))
});
};
其实写 sh 脚本也用不了几行,反而更简单些,只是说并不是谁都能写 sh 脚本的,需要一定的 linux 技能。
Dockerfile
from node:9.2.0
add . /app
expose 7001
workdir /app
run npm i
cmd npm run start
.travis.yml
sudo: false
language: node_js
addons:
ssh_known_hosts: 139.199.227.41
node_js:
- '9'
before_install:
- openssl aes-256-cbc -K $encrypted_b8bda4515144_key -iv $encrypted_b8bda4515144_iv -in scripts/source.key.enc -out scripts/source.key -d
install:
- npm i npminstall && npminstall
# script:
# - npm run ci
after_script:
# - npminstall codecov && codecov
- chmod 600 scripts/source.key
- shipit staging deploy
- shipit staging docker
就这3 个文件,不花一分钱就可以实现基于 Docker 的 CI/CD, 当然普通的,回退到 PR 的那一个版本就可以了。