為 Node.js 專案打造專屬管家進行開發流程整合及健康檢測

Preview:

Citation preview

NODE.JS

CT

• Continuous Integration

= /

= /

DevOps

= /

Continuous Improvement

Sequelize

module.exports = { attributes: { username: { type: Sequelize.STRING, allowNull: false }, email: { type: Sequelize.STRING, allowNull: false }, age: { type: Sequelize.INTEGER } }, associations: function() { User.hasMany(Post); User.hasMany(Passport); },};

FROM mhart/alpine-node:4

RUN apk add --update git RUN apk add --update build-base libffi-dev ruby ruby-dev \ && gem install sass compass --no-ri --no-rdoc \ && apk del build-base libffi-dev ruby-dev \ && rm -rf /var/cache/apk/*

Dockerfile

Edge of Tomorrow

it('register user should success.', async (done) => { try { let newUser = { username: 'newUser', email: 'newUser@gmail.com', password: 'newUser' }

let result = await request(sails.hooks.http.app) .post('/auth/local/register').send(newUser);

let {email} = newUser; let checkUser = await User.findOne({ where: {email}, include: Passport} );

checkUser.email.should.be.equal(newUser.email); result.status.should.be.equal(302); result.headers.location.should.be.equal('/'); done(); } catch (e) { done(e); } });

Mocha

"scripts": { "debug": "node debug app.js", "test": "grunt test", "test-junit": "mocha test --reporter mocha-junit-reporter",

"build": "npm i && node_modules/grunt-cli/bin/grunt build", "start": "node app.js",

"pm2-start": "pm2 start -f app.js --name 'sailsSample'", "pm2-stop": "pm2 stop sailsSample",

"start-prod": "NODE_ENV=production node app.js", "build-prod": "grunt prod",

"build-docker-env": "docker build -t agileworks/sails_sample_env dockers/node", "build-docker-prod-image": "docker build -t agileworks/sails_sample_prod .", }

Package.json

deploy-production-docker:- ssh jenkins@localhost docker rm -f sails_sample_prodssh jenkins@localhost docker run -d --name sails_sample_prod -p 8800:1337 agileworks/sails_sample_prod

deploy-production-legacy:ssh jenkins@localhost mkdir -p ~/deploy/productionscp sailsSampleProd.tar.gz jenkins@localhost:~/deploy/productiontar -C ~/deploy/production/ -vxf ~/deploy/production/

sailsSampleProd.tar.gz

package-production:- rm sailsSampleProd.tar.gztar cvf ./sailsSampleProd.tar.gz ./

restart-production:- ssh jenkins@localhost cd ~/deploy/production && pm2 delete productionssh jenkins@localhost cd ~/deploy/production && NODE_ENV=production pm2 start -f app.js --name 'production'

Makefile

Jenkins 2 Pipeline

node { stage 'checkout project' git url: 'https://github.com/TrunkWorkshop/sailsSample.git'

stage 'check env' sh "node -v"

stage 'install pm2' sh "npm install pm2 -g"

stage 'build project' sh "npm install"

stage 'test project' sh "npm run test-junit" step([$class: 'JUnitResultArchiver', testResults: 'test-results.xml'])}

node { stage 'run project' sh "npm run pm2-start"

try{ stage 'Approve, go production' def url = 'http://localhost:1337/' input message: "$url look good? ", ok: "Deploy to production" }finally{ sh "npm run pm2-stop" }

stage 'package production' sh "make package-production" step([$class: 'ArtifactArchiver', artifacts: 'prod.tar.gz'])

stage 'deploy production' sh "make deploy-production-legacy"

stage 'restart production' sh "make restart-production"

}

CI / CD

有效溝通

累積開發能量

快速迭代驗證

永生

Checking

Cobertura

Integration

Pipeline

Pull Request

Connects

Slack + Github + Jenkins

&

Recommended