diff --git a/deploy-to-docker-hub.js b/deploy-to-docker-hub.js index 61e7240..c5455c7 100755 --- a/deploy-to-docker-hub.js +++ b/deploy-to-docker-hub.js @@ -1,47 +1,87 @@ #!/usr/bin/env node -const exec = require('child_process').exec; -const request = require('request'); +const execOriginal = require('child_process').exec; +const requestOriginal = require('request'); -const version = require('./src/utils/CaptainConstants').version; -const publishedNameOnDockerHub = require('./src/utils/CaptainConstants').publishedNameOnDockerHub; +function exec(command) { + return new Promise(function (resolve, reject) { + execOriginal(command, function (err, stdout, stderr) { -console.log(version); -console.log(' '); + if (stderr) { + console.log('stderr'); + console.log(stderr); + } + + if (err) { + reject(err) + return + } + + resolve(stdout) + }) + }) +} -exec('git status', function (err, stdout, stderr) { +function request(url) { + return new Promise(function (resolve, reject) { + requestOriginal(url, function (error, response, body) { + if (body) { + body = JSON.parse(body); + } - if (err) { - console.log(err); - return; - } + if (error || !body) { + console.log('Error while fetching tags from Docker Hub!'); + reject(error); + return; + } - var l1 = 'On branch master'; - var l2 = 'Your branch is up to date with \'origin/master\''; - var l3 = 'nothing to commit, working tree clean'; - if (stdout.indexOf(l1) < 0 || stdout.indexOf(l2) < 0 || stdout.indexOf(l3) < 0) { - console.log('Make sure you are on master branch, in sync with remote, and your working directory is clean'); - return; - } - fetchTagsFromHub(); -}); + resolve(body) + }) + }) +} -function fetchTagsFromHub() { - var URL = 'https://hub.docker.com/v2/repositories/' + publishedNameOnDockerHub + '/tags'; - request(URL, function (error, response, body) { +const publishedNameOnDockerHub = 'caprover/caprover'; +let version = '' - if (body) { - body = JSON.parse(body); +exec('npm run build') + .then(function (data) { + data = (data + '').trim() + console.log('----------') + console.log(data) + console.log('----------') + if (!data.startsWith('> caprover@0.0.0') || !data.endsWith('rm -rf ./built && npx tsc')) { + console.log('Unexpected output:') + throw new Error(data) } - if (error || !body || !body.results || !body.results.length) { + version = require('./built/utils/CaptainConstants').configs.version; + + return exec('git status') + }) + .then(function (data) { + var l1 = 'On branch master'; + var l2 = 'Your branch is up to date with \'origin/master\''; + var l3 = 'nothing to commit, working tree clean'; + + if (data.indexOf(l1) < 0 || data.indexOf(l2) < 0 || data.indexOf(l3) < 0) { + console.log('Make sure you are on master branch, in sync with remote, and your working directory is clean'); + throw new Error(data) + } + + var URL = 'https://hub.docker.com/v2/repositories/' + publishedNameOnDockerHub + '/tags'; + + return request(URL) + + }) + .then(function (body) { + + if (!body.results || !body.results.length) { console.log('Error while fetching tags from Docker Hub!'); - console.log(error); - return; + throw error; } var highestTag = ''; @@ -77,40 +117,26 @@ function fetchTagsFromHub() { } } - if (!isVersionValid || !highestTagValue) { - console.log('The version you are pushing is not valid!'); - return; + if (!isVersionValid || !highestTagValue || !version) { + throw new Error('The version you are pushing is not valid! ' + version); } console.log('Pushing ' + version); - buildAndPush(); + var t1 = publishedNameOnDockerHub + ':' + 'latest'; + var t2 = publishedNameOnDockerHub + ':' + version; - }); -} - - -function buildAndPush() { - - var t1 = publishedNameOnDockerHub + ':' + 'latest'; - var t2 = publishedNameOnDockerHub + ':' + version; - let command = 'docker build -t ' + t1 + ' -t ' + t2 + ' -f dockerfile-captain.release . && docker push ' + t1 + ' && docker push ' + t2; - exec(command, function (err, stdout, stderr) { - - if (err) { - console.log('ERROR'); - console.log(err); - } + return exec(`docker build -t ${t1} -t ${t2} -f dockerfile-captain.release . && docker push ${t1} && docker push ${t2}`) + }) + .then(function (stdout) { if (stdout) { console.log('stdout'); console.log(stdout); } - if (stderr) { - console.log('stderr'); - console.log(stderr); - } - }); - -} \ No newline at end of file + }) + .catch(function (err) { + console.error(err) + process.exit(1) + }) \ No newline at end of file diff --git a/dev-scripts/build_and_push_edge.sh b/dev-scripts/build_and_push_edge.sh new file mode 100755 index 0000000..db8ae0c --- /dev/null +++ b/dev-scripts/build_and_push_edge.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build -t caprover/caprover-edge:latest -f dockerfile-captain.edge . +docker push caprover/caprover-edge:latest diff --git a/dockerfile-captain.edge b/dockerfile-captain.edge new file mode 100644 index 0000000..5779ddc --- /dev/null +++ b/dockerfile-captain.edge @@ -0,0 +1,28 @@ +FROM node:10 +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + + +# Build backend code + +COPY package.json /usr/src/app/ +RUN npm install && npm cache clean --force +COPY . /usr/src/app +RUN npm run build +RUN mv edge-overrride.json overrride.json + + +# Build frontend code + +RUN git clone https://github.com/githubsaturn/caprover-frontend.git && \ + cd caprover-frontend && \ + npm install && npm cache clean --force && \ + npm run build && + mv ./build ../dist-frontend + + +ENV NODE_ENV production +ENV PORT 3000 +EXPOSE 3000 + +CMD ["npm" , "start"] diff --git a/edge-override.json b/edge-override.json new file mode 100644 index 0000000..8403203 --- /dev/null +++ b/edge-override.json @@ -0,0 +1,4 @@ +{ + "publishedNameOnDockerHub": "caprover/caprover-edge", + "version": "0.0.1" +} \ No newline at end of file diff --git a/package.json b/package.json index 0befd5f..e628a79 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "dev": "echo 'RECOMPILING' && npx madge --circular --extensions ts ./ && rm -rf ./built && npx tsc && sudo ./dev-scripts/dev-reset-service.sh", "clean": "sudo ./dev-scripts/dev-clean-run-as-dev.sh", "tslint": "tslint -c tslint.json -p tsconfig.json", - "build": "npx tsc", + "build": "rm -rf ./built && npx tsc", "test": "jest" }, "dependencies": {