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

Commit 0ec9be2

Browse files
committed
refactor travis script
1 parent b6f3d27 commit 0ec9be2

File tree

4 files changed

+256
-196
lines changed

4 files changed

+256
-196
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-
- yarn export && cd out && NOW_ALIAS=coderplex-app.now.sh node ../scripts/now.js -p
11+
- yarn export && cd out && NOW_ALIAS=coderplex-app.now.sh node ../scripts/now.js -p --team coderplex -d && cd ../
1212
branches:
1313
only:
1414
- master

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "xo",
8-
"lint":
9-
"prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write --single-quote --print-width='80' --trailing-comma='all' && xo --fix",
8+
"lint": "prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write --single-quote --print-width='80' --trailing-comma='all' && xo --fix",
109
"precommit": "lint-staged",
1110
"analyze": "cross-env ANALYZE=1 next build",
1211
"dev": "cross-env NODE_ENV=development next",
@@ -16,15 +15,24 @@
1615
},
1716
"xo": {
1817
"parser": "babel-eslint",
19-
"extends": ["prettier", "prettier/react", "plugin:react/recommended"],
20-
"env": ["browser", "node"],
18+
"extends": [
19+
"prettier",
20+
"prettier/react",
21+
"plugin:react/recommended"
22+
],
23+
"env": [
24+
"browser",
25+
"node"
26+
],
2127
"rules": {
2228
"linebreak-style": 0,
2329
"react/display-name": 0,
2430
"react/prop-types": 0
2531
},
2632
"space:": 2,
27-
"ignores": ["next.config.js"]
33+
"ignores": [
34+
"next.config.js"
35+
]
2836
},
2937
"lint-staged": {
3038
"*.js": [

scripts/deploy.js

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#! /usr/bin/env node
2+
/* eslint-disable camelcase */
3+
4+
const path = require('path');
5+
const fs = require('fs');
6+
7+
const github = require('octonode');
8+
const travisAfterAll = require('travis-after-all');
9+
const urlRegex = require('url-regex');
10+
const normalizeUrl = require('normalize-url');
11+
const axios = require('axios');
12+
13+
const argv = require('yargs')
14+
.option('debug', {
15+
alias: 'd',
16+
description: 'Show debug info',
17+
type: Boolean,
18+
})
19+
.option('public', {
20+
alias: 'p',
21+
description: 'Deployment is public (`/_src` is exposed)',
22+
type: Boolean,
23+
})
24+
.option('team', {
25+
alias: 'T',
26+
description: 'Set a custom team scope',
27+
type: String,
28+
})
29+
.option('folder', {
30+
alias: 'F',
31+
description: 'Set a folder to deploy',
32+
type: String,
33+
})
34+
.option('comment', {
35+
alias: 'c',
36+
description:
37+
'Post a comment to the PR issue summarizing the now deployment results',
38+
default: true,
39+
type: Boolean,
40+
})
41+
.help()
42+
.alias('help', 'h').argv;
43+
44+
const { runNow, runNowAlias } = require('./now');
45+
46+
if (!process.env.CI || !process.env.TRAVIS) {
47+
throw new Error('Could not detect Travis CI environment');
48+
}
49+
50+
const githubToken = process.env.GH_TOKEN;
51+
const nowToken = process.env.NOW_TOKEN;
52+
const discordHook = process.env.DISCORD_HOOK;
53+
const prSha = process.env.TRAVIS_PULL_REQUEST_SHA;
54+
const commitSha = process.env.TRAVIS_COMMIT;
55+
const repoSlug = process.env.TRAVIS_REPO_SLUG;
56+
const aliasUrl = process.env.NOW_ALIAS;
57+
58+
if (!githubToken) {
59+
throw new Error('Missing required environment variable GH_TOKEN');
60+
}
61+
62+
if (!nowToken) {
63+
throw new Error('Missing required environment variable NOW_TOKEN');
64+
}
65+
66+
const ghClient = github.client(githubToken);
67+
const ghRepo = ghClient.repo(repoSlug);
68+
const ghIssue = ghClient.issue(repoSlug, process.env.TRAVIS_PULL_REQUEST);
69+
70+
function getUrl(content) {
71+
const urls = content.match(urlRegex()) || [];
72+
return urls.map(url => normalizeUrl(url.trim().replace(/\.+$/, '')))[0];
73+
}
74+
75+
const baseArgs = ['--token', nowToken];
76+
77+
const nowArgs = ['--no-clipboard'];
78+
79+
if (argv.debug || argv.d) {
80+
baseArgs.push('--debug');
81+
}
82+
83+
if (argv.team || argv.T) {
84+
baseArgs.push('--team');
85+
baseArgs.push(argv.team || argv.T);
86+
}
87+
88+
if (argv.public || argv.p) {
89+
nowArgs.push('--public');
90+
}
91+
92+
if (argv.folder || argv.F) {
93+
const deployPath = path.resolve(argv.folder);
94+
if (fs.statSync(deployPath).isDirectory()) {
95+
nowArgs.push('--name', repoSlug.replace('/', '-'));
96+
nowArgs.push(deployPath);
97+
}
98+
}
99+
100+
function notifyInDiscord(err, res) {
101+
if (err) {
102+
return axios
103+
.post(discordHook, {
104+
username: `${repoSlug.replace('/', '-')}-BOT`,
105+
content: `Deploymet failed check travis logs here https://travis-ci.org/coderplex/coderplex/builds/${process
106+
.env.TRAVIS_BUILD_ID}`,
107+
})
108+
.then(() => {
109+
console.log(`Error posted to discord`);
110+
})
111+
.catch(console.log.bind(console));
112+
}
113+
return axios
114+
.post(discordHook, {
115+
username: `${repoSlug.replace('/', '-')}-BOT`,
116+
content: buildComment(res.context, res.url, 'https://coderplex.org'),
117+
})
118+
.then(() => {
119+
console.log(`Error posted to discord`);
120+
})
121+
.catch(console.log.bind(console));
122+
}
123+
124+
function buildComment(context, url, aliasUrl) {
125+
return `### New Δ Now ${context} deployment complete\n- ✅ **Build Passed**\n- 🚀 **URL** : ${aliasUrl
126+
? aliasUrl
127+
: url}\n---\nNote: **This is autogenerated through travis-ci build**`;
128+
}
129+
130+
function deploy(context, sha) {
131+
// Send error status to github PR
132+
ghRepo.status(
133+
sha,
134+
{
135+
context,
136+
state: 'pending',
137+
description: `Δ Now ${context} deployment pending`,
138+
},
139+
console.log.bind(console),
140+
);
141+
// Initiate deployment process
142+
runNow([...baseArgs, ...nowArgs], (code, res) => {
143+
// Remember, process code: 0 means success else failure in unix/linux
144+
if (code) {
145+
// Send error status to github PR
146+
ghRepo.status(
147+
sha,
148+
{
149+
context,
150+
state: 'error',
151+
description: `Δ Now ${context} deployment failed`,
152+
},
153+
console.log.bind(console),
154+
);
155+
// Notify in discord
156+
notifyInDiscord(true);
157+
return console.log(`now process exited with code ${code}`);
158+
}
159+
160+
// Retrieve now.sh unique url from stdOut
161+
const deployedUrl = getUrl(res);
162+
163+
if (context === 'staging') {
164+
// Send success status to github PR
165+
ghRepo.status(
166+
sha,
167+
{
168+
context,
169+
target_url: deployedUrl,
170+
state: 'success',
171+
description: `Δ Now ${context} deployment complete`,
172+
},
173+
console.log.bind(console),
174+
);
175+
// Check and create comment on github PR abot deployment results
176+
if (argv.comment) {
177+
ghIssue.createComment(
178+
{
179+
body: buildComment(context, deployedUrl),
180+
},
181+
console.log.bind(console),
182+
);
183+
}
184+
return;
185+
}
186+
// In production alias deployment to specified alias url from now.json file or from env variable
187+
if (context === 'production') {
188+
runNowAlias(baseArgs, { deployedUrl, aliasUrl }, code => {
189+
if (code) {
190+
// Notify failure in discord.
191+
notifyInDiscord(true);
192+
return console.log(`now process exited with code ${code}`);
193+
}
194+
// Notify success in discord
195+
notifyInDiscord(false, { context, url: deployedUrl, aliasUrl });
196+
console.log('🎉 Done');
197+
});
198+
}
199+
});
200+
}
201+
202+
travisAfterAll((code, err) => {
203+
if (err || code) return;
204+
switch (process.env.TRAVIS_EVENT_TYPE) {
205+
case 'pull_request':
206+
return deploy('staging', prSha);
207+
case 'push':
208+
return deploy('production', commitSha);
209+
default:
210+
break;
211+
}
212+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy