Skip to content

Commit

Permalink
Merge branch 'master' into connected-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
thangngoc89 committed Jul 31, 2018
2 parents 74c36ac + 1fccc99 commit 2203c03
Show file tree
Hide file tree
Showing 4 changed files with 569 additions and 8 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ matrix:
script:
- npm run ci:build
# - npm run test
before_deploy: npm install -g now --no-save
after_script:
- cp now.json build
- cd ../github/bot
- npm install
- cd ../../client/build
- node ../../github/bot/deploy.js
deploy:
- provider: script
script: now --token $NOW_TOKEN --public build
skip_cleanup: true
on:
all_branches: true
master: false
- provider: script
script: cp now.json build && cd build && now --token $NOW_TOKEN --public && now alias --token $NOW_TOKEN staging.rtop.khoanguyen.me
script: now alias --token $NOW_TOKEN staging.rtop.khoanguyen.me
skip_cleanup: true
on:
master: true
97 changes: 97 additions & 0 deletions github/bot/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#! /usr/bin/env node

const github = require("octonode");
const normalizeUrl = require("normalize-url");
const spawn = require("cross-spawn");
const urlRegex = require("url-regex");
const fs = require("fs");
const nowCli = require.resolve("now/download/dist/now");

if (!process.env.CI || !process.env.TRAVIS) {
throw new Error("Could not detect Travis CI environment");
}

const githubToken = process.env.GH_TOKEN;
const nowToken = process.env.NOW_TOKEN;

if (!githubToken) {
throw new Error("Missing required environment variable GH_TOKEN");
}

if (!nowToken) {
throw new Error("Missing required environment variable NOW_TOKEN");
}

const client = github.client(githubToken);
const ghRepo = client.repo(process.env.TRAVIS_REPO_SLUG);

function noop() {}

function getUrl(content) {
const urls = content.match(urlRegex()) || [];

return urls.map(url => normalizeUrl(url.trim().replace(/\.+$/, "")))[0];
}

function deploy(context, sha) {
ghRepo.status(
sha,
{
context,
state: "pending",
description: `Δ Now ${context} deployment pending`,
},
noop
);

const args = ["--token", nowToken, "--no-clipboard"];
const alias = context === "production" && process.env.NOW_ALIAS;
let stdout = "";

if (alias) {
args.push(...["--alias", alias]);
}

const child = spawn(nowCli, args);

if (!alias) {
child.stdout.on("data", data => (stdout += data));
}

child.on("error", err => {
console.error(err);
ghRepo.status(
sha,
{
context,
state: "error",
description: `Δ Now deployment failed. See Travis logs for details.`,
},
noop
);
});

child.on("close", () => {
const target_url = alias || getUrl(stdout);
console.log("Target url: ", target_url);
// deployment is now complete

ghRepo.status(
sha,
{
context,
target_url,
state: "success",
description: `Δ Now deployment complete`,
},
noop
);
});
}

switch (process.env.TRAVIS_EVENT_TYPE) {
case "pull_request":
return deploy("PR deployment", process.env.TRAVIS_PULL_REQUEST_SHA);
case "push":
return deploy("push deployment", process.env.TRAVIS_COMMIT);
}
Loading

0 comments on commit 2203c03

Please sign in to comment.