Skip to content

Commit a88348f

Browse files
committed
add cd scripts
1 parent e028952 commit a88348f

File tree

10 files changed

+422
-0
lines changed

10 files changed

+422
-0
lines changed

.travis.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
dist: trusty
2+
sudo: required
3+
language: node_js
4+
node_js:
5+
- node
6+
branches:
7+
except:
8+
- gh-pages
9+
notifications:
10+
email: false
11+
services:
12+
- docker
13+
install:
14+
- cd scripts && npm install && cd ..
15+
16+
script:
17+
- node scripts/build && node scripts/test
18+
19+
deploy:
20+
- provider: script
21+
skip_cleanup: true
22+
script: node scripts/deploy Staging
23+
on:
24+
branch: develop
25+
- provider: script
26+
skip_cleanup: true
27+
script: node scripts/deploy Production
28+
on:
29+
branch: master
30+
31+
32+
# Disable "Build pushed pull requests"
33+
34+
35+
# env:
36+
# DEPLOY_SETTINGS_JSON: A mapping of environment to the deployment types and their options.
37+
# {
38+
# "Staging": [
39+
# { "type": "docker",
40+
# "options": { "source": "zevere/nlp:latest", "target": "zevere/nlp:latest", "user": "foo", "pass": "PASS" }
41+
# },
42+
# { "type": "heroku",
43+
# "options": {
44+
# "app": "nlp-staging", "source": "zevere/nlp:latest", "dyno": "web", "user": "[email protected]", "token": "TOKEN"
45+
# }
46+
# }
47+
# ],
48+
# "Production": [ ]
49+
# }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const $ = require('shelljs')
2+
require('../logging')
3+
4+
$.config.fatal = true
5+
const root = `${__dirname}/../..`
6+
7+
8+
module.exports.build_image = function () {
9+
const image_name = 'zevere/nlp:latest'
10+
console.info(`building Docker Image "${image_name}"`)
11+
$.exec(`docker build -t ${image_name} ${root}/`)
12+
}

scripts/build/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const $ = require('shelljs')
2+
const path = require('path')
3+
require('../logging')
4+
5+
$.config.fatal = true
6+
const root = path.join(__dirname, '..', '..')
7+
8+
const docker_script = require('./build_docker_image')
9+
10+
try {
11+
docker_script.build_image()
12+
} catch (e) {
13+
console.error(e)
14+
process.exit(1)
15+
}
16+
17+
console.info(`Build succeeded: "${root}"`)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const $ = require('shelljs')
2+
require('../logging')
3+
4+
$.config.fatal = true
5+
6+
exports.deploy = function (source, target, user, pass) {
7+
console.info(`pushing docker local image ${source} to ${target}`)
8+
9+
$.exec(`docker tag ${source} ${target}`)
10+
$.exec(`docker login --username ${user} --password ${pass}`)
11+
$.exec(`docker push ${target}`)
12+
$.exec('docker logout')
13+
}

scripts/deploy/deploy_heroku.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const $ = require('shelljs')
2+
require('../logging')
3+
4+
$.config.fatal = true
5+
6+
7+
function validate_args(...args) {
8+
if (!args.every(x => x && x.length)) {
9+
throw `All the required parameters should have value.`
10+
}
11+
}
12+
13+
function push_image_to_heroku(app, source, dyno, user, token) {
14+
console.info('pushing docker image to heroku')
15+
16+
console.debug('connecting to heroku docker registry')
17+
$.exec(`docker login --username ${user} --password ${token} registry.heroku.com`)
18+
19+
console.debug('tagging the image')
20+
$.exec(`docker tag ${source} registry.heroku.com/${app}/${dyno}`)
21+
22+
console.debug('pushing the image')
23+
$.exec(`docker push registry.heroku.com/${app}/${dyno}`)
24+
}
25+
26+
function release_heroku_app(app, source, dyno, token) {
27+
console.info('deploying the image to heroku dyno')
28+
29+
console.debug(`getting docker image ID`)
30+
const image_id = $.exec(`docker inspect ${source} --format={{.Id}}`).stdout.trim()
31+
32+
console.debug(`upgrading to new release`)
33+
const post_data = JSON.stringify({
34+
updates: [{
35+
type: dyno,
36+
docker_image: image_id
37+
}]
38+
})
39+
40+
$.exec(
41+
`curl -X PATCH https://api.heroku.com/apps/${app}/formation ` +
42+
`-H 'Authorization: Bearer ${token}' ` +
43+
`-H "Content-Type: application/json" ` +
44+
`-H "Accept: application/vnd.heroku+json; version=3.docker-releases" ` +
45+
`-d ${JSON.stringify(post_data)}`
46+
)
47+
}
48+
49+
50+
exports.deploy = function (app, source, dyno, user, token) {
51+
validate_args(app, source, dyno, user, token)
52+
push_image_to_heroku(app, source, dyno, user, token)
53+
release_heroku_app(app, source, dyno, token)
54+
}

scripts/deploy/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require('../logging')
2+
3+
4+
function get_environment_name() {
5+
console.info('verifying environment name')
6+
7+
const environment_name = process.argv[process.argv.length - 1]
8+
if (environment_name && environment_name.length) {
9+
console.debug(`environment is ${environment_name}.`)
10+
return environment_name
11+
} else {
12+
throw `No environment name is passed.\n` +
13+
`\tExample: node ci/deploy Staging`
14+
}
15+
}
16+
17+
function get_deployments_for_env(environment_name) {
18+
console.info(`finding deployments for environment ${environment_name}.`)
19+
20+
const jsonValue = process.env['DEPLOY_SETTINGS_JSON']
21+
let deployment_map;
22+
try {
23+
deployment_map = JSON.parse(jsonValue)
24+
} catch (e) {
25+
throw `Value of "DEPLOY_SETTINGS_JSON" environment variable is not valid JSON.`
26+
}
27+
28+
const env_deployments = deployment_map[environment_name]
29+
if (!env_deployments) {
30+
throw `There are no field for environment ${environment_name} in "DEPLOY_SETTINGS_JSON" value.`
31+
}
32+
if (!(Array.isArray(env_deployments) && env_deployments.length)) {
33+
console.warn(`There are deployments specified for environment ${environment_name}.`)
34+
}
35+
36+
console.debug(`${env_deployments.length || 0} deployments found.`)
37+
38+
return env_deployments
39+
}
40+
41+
function deploy(environment_name, deployment) {
42+
console.info(`deploying to ${deployment.type} for environment ${environment_name}.`)
43+
const docker = require('./deploy_docker_registry')
44+
const heorku = require('./deploy_heroku')
45+
46+
if (deployment.type === 'docker') {
47+
docker.deploy(
48+
deployment.options.source,
49+
deployment.options.target,
50+
deployment.options.user,
51+
deployment.options.pass
52+
)
53+
} else if (deployment.type === 'heroku') {
54+
heorku.deploy(
55+
deployment.options.app,
56+
deployment.options.source,
57+
deployment.options.dyno,
58+
deployment.options.user,
59+
deployment.options.token
60+
)
61+
} else {
62+
throw `Invalid deployment type ${deployment.type}.`
63+
}
64+
}
65+
66+
67+
try {
68+
const environment_name = get_environment_name()
69+
get_deployments_for_env(environment_name)
70+
.forEach(d => deploy(environment_name, d))
71+
} catch (e) {
72+
console.error(e)
73+
process.exit(1)
74+
}

scripts/logging.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const chalk = require('chalk');
2+
const $ = require('shelljs')
3+
4+
if (process.env['TRAVIS'] && process.env['CI']) {
5+
console.info = m => $.echo("\033[1;34m", m, "\033[0m")
6+
console.debug = m => $.echo("\033[0;32m", m, "\033[0m")
7+
console.warn = m => $.echo("\033[1;33m", m, "\033[0m")
8+
console.error = m => $.echo("\033[1;31m", m, "\033[0m")
9+
} else {
10+
console.info = m => console.log(chalk.blue.bold(m))
11+
console.debug = m => console.log(chalk.green.bold(m))
12+
console.warn = m => console.log(chalk.yellow.bold(m))
13+
console.error = m => console.log(chalk.red.bold(m))
14+
}

0 commit comments

Comments
 (0)