Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit 4341505

Browse files
committed
add travis deploy script
1 parent 8920f9b commit 4341505

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ before_install:
88
- 'export PATH="$HOME/.yarn/bin:$PATH"'
99
cache: yarn
1010
after_script:
11-
- NOW_ALIAS=coderplex.org node_modules/.bin/now-travis
11+
- yarn export && cd out && NOW_ALIAS=coderplex-app.now.sh ../scripts/now -p
1212
branches:
1313
only:
1414
- master

scripts/now.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#! /usr/bin/env node
2+
3+
const github = require('octonode');
4+
const normalizeUrl = require('normalize-url');
5+
const spawn = require('cross-spawn');
6+
const travisAfterAll = require('travis-after-all');
7+
const urlRegex = require('url-regex');
8+
const argv = require('yargs').argv;
9+
10+
const nowCli = require.resolve('now/download/dist/now');
11+
12+
if (!process.env.CI || !process.env.TRAVIS) {
13+
throw new Error('Could not detect Travis CI environment');
14+
}
15+
16+
const githubToken = process.env.GH_TOKEN;
17+
const nowToken = process.env.NOW_TOKEN;
18+
19+
if (!githubToken) {
20+
throw new Error('Missing required environment variable GH_TOKEN');
21+
}
22+
23+
if (!nowToken) {
24+
throw new Error('Missing required environment variable NOW_TOKEN');
25+
}
26+
27+
const client = github.client(githubToken);
28+
const ghRepo = client.repo(process.env.TRAVIS_REPO_SLUG);
29+
30+
function noop() {}
31+
32+
function getUrl(content) {
33+
const urls = content.match(urlRegex()) || [];
34+
35+
return urls.map(url => normalizeUrl(url.trim().replace(/\.+$/, '')))[0];
36+
}
37+
38+
function deploy(context, sha) {
39+
ghRepo.status(
40+
sha,
41+
{
42+
context,
43+
state: 'pending',
44+
description: `Δ Now ${context} deployment pending`,
45+
},
46+
noop,
47+
);
48+
49+
const args = ['--token', nowToken, '--no-clipboard'];
50+
const alias = context === 'production' && process.env.NOW_ALIAS;
51+
let stdout = '';
52+
53+
if (alias) {
54+
args.push(...['--alias', alias]);
55+
}
56+
57+
if (argv.p || argv.public) {
58+
args.push(...['-p']);
59+
}
60+
61+
if (argv.folder) {
62+
args.push(argv.folder);
63+
}
64+
65+
const child = spawn(nowCli, args);
66+
67+
if (!alias) {
68+
child.stdout.on('data', data => {
69+
stdout += data;
70+
});
71+
}
72+
73+
child.on('error', err => {
74+
console.error(err);
75+
ghRepo.status(
76+
sha,
77+
{
78+
context,
79+
state: 'error',
80+
description: `Δ Now ${context} deployment failed. See Travis logs for details.`,
81+
},
82+
noop,
83+
);
84+
});
85+
86+
child.on('close', () => {
87+
const targetUrl = alias || getUrl(stdout);
88+
89+
ghRepo.status(
90+
sha,
91+
{
92+
context,
93+
targetUrl,
94+
state: 'success',
95+
description: `Δ Now ${context} deployment complete`,
96+
},
97+
noop,
98+
);
99+
});
100+
}
101+
102+
travisAfterAll((code, err) => {
103+
// Don't do anything if there was an error of if the build returned a failing code
104+
if (err || code) {
105+
return;
106+
}
107+
108+
switch (process.env.TRAVIS_EVENT_TYPE) {
109+
case 'pull_request':
110+
return deploy('staging', process.env.TRAVIS_PULL_REQUEST_SHA);
111+
case 'push':
112+
return deploy('production', process.env.TRAVIS_COMMIT);
113+
default:
114+
return '';
115+
}
116+
});

0 commit comments

Comments
 (0)