Skip to content

Commit

Permalink
Deployable support
Browse files Browse the repository at this point in the history
  • Loading branch information
solocommand committed Apr 2, 2019
1 parent 713626e commit 6531299
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js
node_js: "10.15"
cache: yarn

stages:
- name: test
- name: deploy
if: tag is present
- name: notify
if: tag is present

jobs:
include:
- stage: test
- stage: notify
script: scripts/deploy-notify.sh

- stage: deploy
name: Deploy Package
script: scripts/deploy.js
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:10.15

ENV NODE_ENV production
WORKDIR /root

ADD ./ /root
RUN yarn --production
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"scripts": {
"lint": "node_modules/.bin/gulp lint",
"dev": "node_modules/.bin/gulp",
"serve": "NODE_ENV=production node src/index"
"serve": "NODE_ENV=production node src/index",
"test": "echo 'No tests specified'; exit 0"
},
"dependencies": {
"@base-cms/object-path": "^0.6.0",
Expand Down
9 changes: 9 additions & 0 deletions scripts/deploy-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

docker build -q -t "body-import-parser:$1" .

docker tag "body-import-parser:$1" "basecms/body-import-parser:$1"
docker push "basecms/body-import-parser:$1"
docker image rm "body-import-parser:$1"
6 changes: 6 additions & 0 deletions scripts/deploy-k8s.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

IMAGE=basecms/body-import-parser:$1
yarn global add @endeavorb2b/rancher2cli
r2 dl basecms-service body-import-parser $IMAGE --namespace=imports
10 changes: 10 additions & 0 deletions scripts/deploy-notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

payload="{
\"attachments\": [{
\"color\": \"good\",
\"text\": \"Deployment of \`$TRAVIS_REPO_SLUG\` @ \`$TRAVIS_TAG\` to production has finished successfully.\"
}]
}"
curl -X POST -H 'Content-type: application/json' --data "$payload" https://hooks.slack.com/services/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
102 changes: 102 additions & 0 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env node
/**
* Deployment tool for project
* Requirements:
* - ENV
* - DOCKERHUB_USERNAME
* - DOCKERHUB_PASSWORD
* - TRAVIS_TAG
* - RANCHER_URL
* - RANCHER_TOKEN
* - RANCHER_CLUSTERID
*/

const { existsSync } = require('fs');
const { join } = require('path');
const { spawnSync } = require('child_process');
const https = require('https');
const lerna = require('../lerna.json');

const { log } = console;
const { TRAVIS_TAG: version } = process.env;
const image = `basecms/body-import-parser`;

const error = (message) => {
log(`ERROR: ${message}`);
const text = `Deployment of \`${image}\` @ \`${version}\` to production FAILED!\n${message}`;
const payload = JSON.stringify({ attachments: [{ color: 'danger', text }] });
const req = https.request({
hostname: 'hooks.slack.com',
path: '/services/TDA6JTAKC/BGCT0SNGY/vJSPL4S2NQN8SDAjCPilP773',
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': payload.length,
}
}, (res) => {
res.on('data', () => {
log('Slack notified.');
process.exit(1);
});
});

req.on('error', e => log(e));
req.write(payload);
req.end();

}

const getJson = (url, reqHeaders) => new Promise((resolve, reject) => {
const headers = { ...reqHeaders, 'Content-Type': 'application/json; charset=utf-8' };
https.get(url, { headers }, (resp) => {
let data = '';
const { statusCode, statusMessage } = resp;
if (statusCode >= 500) return reject(statusMessage);
resp.on('data', chunk => data += chunk);
resp.on('end', () => resolve(JSON.parse(data)));
}).on('error', reject);
});

const getVersions = async () => {
const authUrl = `https://auth.docker.io/token?service=registry.docker.io&scope=repository:${image}:pull`;
const { token } = await getJson(authUrl);
const url = `https://registry.hub.docker.com/v2/${image}/tags/list`;
const list = await getJson(url, { Authorization: `Bearer ${token}`});
return Array.isArray(list.tags) ? list.tags : [];
};

const shouldBuild = async () => {
log(`\nChecking ${image}:${version} on DockerHub`);
const versions = await getVersions();
return !versions.includes(version);
}

/**
* Build docker image and push to docker hub
*/
const build = async () => {
log(`Building ${image}:${version}...\n`);
const { status } = await spawnSync('bash', ['scripts/deploy-image.sh', site, version], { stdio: 'inherit' });
if (status !== 0) error('Image build failed!');
}

const deploy = async () => {
log(`Deploying ${image}:${version} on Kubernertes`);
const { status } = await spawnSync('bash', ['scripts/deploy-k8s.sh', site, version], { stdio: 'inherit' });
if (status !== 0) error('Image deploy failed!');
};

const main = async () => {
if (await shouldBuild()) {
log(` Image was not found, building.`)
await build();
log(' Build complete.');
} else {
log(` Image found, skipping build.`);
}
await deploy();
log(' Deploy complete.\n');
};

main().catch(e => error);

0 comments on commit 6531299

Please sign in to comment.