Skip to content

Commit 209d809

Browse files
committed
Build on Travis CI
1 parent e028952 commit 209d809

File tree

12 files changed

+629
-0
lines changed

12 files changed

+629
-0
lines changed

.gitignore

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,119 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.nox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*.cover
48+
.hypothesis/
49+
.pytest_cache/
50+
51+
# Translations
52+
*.mo
53+
*.pot
54+
55+
# Django stuff:
56+
*.log
57+
local_settings.py
58+
db.sqlite3
59+
60+
# Flask stuff:
61+
instance/
62+
.webassets-cache
63+
64+
# Scrapy stuff:
65+
.scrapy
66+
67+
# Sphinx documentation
68+
docs/_build/
69+
70+
# PyBuilder
71+
target/
72+
73+
# Jupyter Notebook
74+
.ipynb_checkpoints
75+
76+
# IPython
77+
profile_default/
78+
ipython_config.py
79+
80+
# pyenv
81+
.python-version
82+
83+
# celery beat schedule file
84+
celerybeat-schedule
85+
86+
# SageMath parsed files
87+
*.sage.py
88+
89+
# Environments
90+
.env
91+
.venv
92+
env/
93+
venv/
94+
ENV/
95+
env.bak/
96+
venv.bak/
97+
98+
# Spyder project settings
99+
.spyderproject
100+
.spyproject
101+
102+
# Rope project settings
103+
.ropeproject
104+
105+
# mkdocs documentation
106+
/site
107+
108+
# mypy
109+
.mypy_cache/
110+
.dmypy.json
111+
dmypy.json
112+
113+
114+
# CI/CD Scripts
115+
scripts/node_modules/
116+
!/scripts/build/
117+
118+
1119
nlp_oldbackup.py

.travis.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
14+
jobs:
15+
include:
16+
- stage: Build
17+
name: "Buid Docker image"
18+
install: cd scripts && npm ci && cd ..
19+
script: node scripts/build
20+
env:
21+
# DEPLOY_SETTINGS_JSON='{"CI":[{"type":"docker","options":{"user":"","pass":""}}]}'
22+
- secure: k0PMGpkgDwzCVH4pryUTwjEdgQ5WRfi+HaWmK0+7MjNSgM+Jm+LFTNz5J30/FKfVG9XNMj+NUg4pM1w7CAYaAKr+LE6g6PbVFZzvbNalfWFKUFzGV3dBfkb998g4n3TVf3HvNS/PeOqF/sXVYRY8AF1TzdMokR5ivwjPE9Zq9Fuwd6JlsXuc4uQ0+uXezZnYeFaWtvYlhUUlmIFjNHDbif+OjK2f5ReyVu6zwVnfdX8VN6iDpEZtpRDjGDniEunmYryWHrTw7Ikw9Ad4ZsvTySPbV7oNxOXDAaJLNDNAC+br4n/Mfl/KN+n43HCjHjRX/IrhMBTYGkXggiDhMK6p/lXWYDGLr/VS6W7eBMXmTYcB+O/k327EzZKGedkKkJRi7eM+1NP87VM0g+tMLnH8LYTySBDr5/6tOFifH2UzMFiaqfnBzHAroaAt1CtFwjW1JlS5Gj6OQhOlK70bIK7PQEyDBFw316SDbQp8CZTa7NvUvNNohqJ1ero/CchWT1pxM8JgQhKnylzlJltp8HIArPQ7sBhFLmJzJz/r5u0VfdeprEJQ4RfNbX3Ir9hlETTSaJ9SYoAiAxqBOQTxqEZZK9xUDGbIaY2xWxkvdmFuJpAgU19++CGw3TtZkf0NZ87y7BMACMaJu43hQxu81eTlSXxDqGfokGz3XztEVnk5eO8=
23+
- stage: Test
24+
name: Unit Tests
25+
install: cd scripts && npm ci && cd ..
26+
script: echo ToDo
27+
- stage: Deploy
28+
name: "Deploy"
29+
script: skip
30+
install:
31+
- cd scripts && npm ci && cd ..
32+
- docker pull zevere/nlp-python:unstable
33+
env:
34+
# DEPLOY_SETTINGS_JSON='{"Staging":[
35+
# {"type":"docker","options":{"source":"zevere/nlp-python:unstable","target":"zevere/nlp-python:edge","user":"","pass":""}},
36+
# {"type":"heroku","options":{"app":"zv-s-nlp-python","source":"zevere/nlp-python:edge","dyno":"web","user":"","token":""}}
37+
# ]}'
38+
- secure: PR9fLKRGaNhh+fyAo8u2KehUJk9qUP1oXAxxmexjSJXYNQLURoFF2JSqN4ZgkQ1QVtcSsy4G1jMy+cugqv6E8nyQZiecQOHTL6eQdijnVqb9pDWb0c+ofNtsBHNY/a4PQoFkHJv0s3+P1rdbYXIMv3hMZDciCjfBQS3pZhNnQAzQv+8kzf9wlxYGx4Q4B9en5aDWTUPm1bzz4w5LhnUEXvQjUGdOwJ42bIjY8Rr3lNXzRHegMhXbHwBonFMT3m3tVLPuOKQeAtTE7r7wihZqDAGUiXBm7grID8mYcQDCguecYwQ+U4qmdOpsqAJZcgQzz4lY/YPUbzpET5g7LRYgYvg391aF0r2YsxvhoKcpLwOhqFyN27rd5AKGa/QSpsKdTAjIwzymffpoBHWjSv3VykTklhSmHslTE7WZXHLIEB+ypkEzOXDgOX7jSjUoWsnGJHR7FJfuYSkv+JyGtdnF/ncPjNgE/mMCedfw/T5EQh95GjhKyce4GGRbL9tH9Rf23bSpylYKxxYnbS5PL++8k/RNmt0vPmTAjcrkH2Ckq8253gRt12HF3kVepkIr4ZRrU4TZEmfgvAZnXcUp5uj7st0leHHc/N9CQktIsC7fajfdfmaRKBAAvu3ou8hknHKggA6LGfd98s7s+XyqZgF/9tnAVjpHdGTrVSxnBmB5J9Y=
39+
deploy:
40+
- provider: script
41+
skip_cleanup: true
42+
script: node scripts/deploy Staging
43+
on:
44+
branch: develop
45+
46+
47+
# Disable "Build pushed pull requests"

scripts/build/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const $ = require('shelljs')
2+
const path = require('path')
3+
require('../logging')
4+
5+
$.config.fatal = true
6+
const root = path.resolve(`${__dirname}/../..`)
7+
8+
function get_docker_options() {
9+
const settings_script = require('./../deploy/deploy_settings')
10+
let settings
11+
try {
12+
settings = settings_script.get_deployment_settings()
13+
} catch (e) {
14+
return
15+
}
16+
17+
let docker_options;
18+
for (const prop in settings) {
19+
for (const deployment of settings[prop]) {
20+
if (deployment.type === 'docker') {
21+
docker_options = deployment.options;
22+
}
23+
}
24+
}
25+
return docker_options
26+
}
27+
28+
try {
29+
console.info(`building Docker image`)
30+
$.cd(root)
31+
32+
console.debug(`building with the "zevere-nlp" tag`)
33+
$.exec(
34+
`docker build --tag zevere-nlp --no-cache .`
35+
)
36+
37+
console.debug('reading Docker deployment options')
38+
docker_options = get_docker_options()
39+
if (docker_options) {
40+
console.debug('pushing images to the Docker hub')
41+
const docker_deploy_utility = require('./../deploy/deploy_docker_registry')
42+
43+
docker_deploy_utility.deploy(
44+
'zevere-nlp', 'zevere/nlp-python:unstable', docker_options.user, docker_options.pass
45+
)
46+
} else {
47+
console.warn('Docker deployment options not found. skipping Docker image push...')
48+
}
49+
} catch (e) {
50+
console.error(e)
51+
process.exit(1)
52+
}
53+
54+
console.info(`✅ Build succeeded`)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const $ = require('shelljs')
2+
require('../logging')
3+
4+
$.config.fatal = true
5+
6+
7+
exports.deploy = function (source, target, user, pass) {
8+
console.info(`pushing docker local image ${source} to ${target}`)
9+
10+
$.exec(`docker tag ${source} ${target}`)
11+
$.exec(`docker login --username ${user} --password ${pass}`)
12+
$.exec(`docker push ${target}`)
13+
$.exec('docker logout')
14+
}

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/deploy_settings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exports.get_deployment_settings = () => {
2+
const jsonValue = process.env['DEPLOY_SETTINGS_JSON']
3+
let settings;
4+
try {
5+
settings = JSON.parse(jsonValue)
6+
} catch (e) {
7+
throw `Value of "DEPLOY_SETTINGS_JSON" environment variable is not valid JSON.`
8+
}
9+
10+
return settings
11+
}

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)