Skip to content

Commit 7d56566

Browse files
committed
feat: automatically switch between a simple static now.sh deployment vs a full docker-based deployment based on the branch
1 parent 8056db2 commit 7d56566

File tree

4 files changed

+159
-97
lines changed

4 files changed

+159
-97
lines changed

docs-site/src/components/schema-form/component-explorer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ export default class ComponentExplorer extends withContext(withLitHtml()) {
8888
async requestRender(formData) {
8989
const self = this;
9090

91+
// experimental: instead of requiring every branch to do a full docker deployment,
92+
// enable CORS support on the master branch and only require Docker deploys on that one branch
9193
if (formData && formData !== '') {
9294
const res = await fetch(
93-
`/api/render?${qs.stringify({
95+
`https://master.boltdesignsystem.com/api/render?${qs.stringify({
9496
template: this.props.template,
9597
})}`,
9698
{

packages/servers/default-server/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ const port = process.env.PORT || 3123;
99
async function init() {
1010
const config = await getConfig();
1111

12+
// enable CORS to allow other Bolt deployments / instances to use the same Twig rendering service
13+
app.use((req, res, next) => {
14+
res.header('Access-Control-Allow-Origin', '*');
15+
res.header(
16+
'Access-Control-Allow-Headers',
17+
'Origin, X-Requested-With, Content-Type, Accept, Authorization',
18+
);
19+
if (req.method === 'OPTIONS') {
20+
res.header(
21+
'Access-Control-Allow-Methods',
22+
'PUT, POST, PATCH, DELETE, GET',
23+
);
24+
return res.status(200).json({});
25+
}
26+
next();
27+
});
28+
1229
app.get(['/docs', '/docs/', '/docs/index.html'], (req, res) => {
1330
res.redirect('/docs/getting-started/index.html');
1431
});

scripts/deploy.js

Lines changed: 91 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,112 @@
11
#!/usr/bin/env node
2+
const fs = require('fs');
23
const shell = require('shelljs');
3-
const { outputBanner } = require('ci-utils');
4+
const path = require('path');
45
const { gitSha } = require('./utils');
5-
const { setCheckRun } = require('./check-run');
6+
const { branchName } = require('./utils/branch-name');
7+
const { getConfig } = require('../packages/build-tools/utils/config-store');
8+
const { fileExists } = require('../packages/build-tools/utils/general');
69

7-
const {
8-
TRAVIS,
9-
TRAVIS_PULL_REQUEST,
10-
TRAVIS_BRANCH,
11-
TRAVIS_PULL_REQUEST_BRANCH,
12-
TRAVIS_REPO_SLUG,
13-
TRAVIS_TAG,
14-
TRAVIS_BUILD_WEB_URL,
15-
} = require('./utils/travis-vars');
10+
const { TRAVIS, TRAVIS_TAG } = require('./utils/travis-vars');
1611

17-
let { NOW_TOKEN, GITHUB_TOKEN } = process.env;
18-
19-
const baseNowArgs = ['--platform-version=1', '--team=boltdesignsystem'];
20-
21-
if (NOW_TOKEN) baseNowArgs.push(`--token=${NOW_TOKEN}`);
12+
let setCheckRun, outputBanner, deployedUrlPretty, deployedUrl;
2213

2314
async function init() {
2415
try {
25-
// also made in `.travis.yml` during docker tag
26-
// const gitSha = getGitSha(true);
27-
// const gitShaLong = getGitSha();
16+
let config = await getConfig();
17+
if (TRAVIS) {
18+
setCheckRun = require('./check-run').setCheckRun;
19+
outputBanner = require('ci-utils').outputBanner;
2820

29-
console.log({
30-
TRAVIS,
31-
TRAVIS_BRANCH,
32-
TRAVIS_PULL_REQUEST_BRANCH,
33-
TRAVIS_PULL_REQUEST,
34-
TRAVIS_REPO_SLUG,
35-
TRAVIS_TAG,
36-
TRAVIS_BUILD_WEB_URL,
37-
gitSha,
38-
});
39-
40-
await setCheckRun({
41-
name: 'Deploy - now.sh (basic)',
42-
status: 'in_progress',
43-
});
44-
outputBanner('Starting deploy...');
21+
await setCheckRun({
22+
name: 'Deploy - now.sh (basic)',
23+
status: 'in_progress',
24+
});
25+
outputBanner('Starting deploy...');
26+
}
4527

28+
// for non-master / release / tagged version deploys, speed things up.
4629
try {
47-
const deployedUrl = shell.exec(
48-
`now deploy --meta gitSha="${gitSha}" --env GIT_SHA=${gitSha} --build-env GIT_SHA=${gitSha} --platform-version=1 --team=boltdesignsystem --token=${NOW_TOKEN}`,
49-
).stdout;
30+
if (
31+
!TRAVIS_TAG &&
32+
!branchName.includes('master') &&
33+
!branchName.includes('release') &&
34+
!branchName.includes('next')
35+
) {
36+
fs.writeFileSync(
37+
path.join(process.cwd(), `${config.wwwDir}/now.json`),
38+
JSON.stringify({
39+
version: 2,
40+
scope: 'boltdesignsystem',
41+
name: 'boltdesignsystem',
42+
builds: [
43+
{
44+
src: '**/*',
45+
use: '@now/static',
46+
},
47+
],
48+
routes: [
49+
{ src: '/.*', headers: { 'Access-Control-Allow-Origin': '*' } },
50+
],
51+
}),
52+
);
5053

51-
let deployedUrlPretty = deployedUrl.trim();
54+
const nowConfigExists = await fileExists(
55+
path.join(process.cwd(), 'now.json'),
56+
);
5257

53-
await setCheckRun({
54-
status: 'completed',
55-
name: 'Deploy - now.sh (basic)',
56-
conclusion: 'success',
57-
output: {
58-
title: 'Now.sh Basic Deploy',
59-
summary: `
60-
- ${deployedUrlPretty}
61-
`.trim(),
62-
},
63-
});
58+
if (nowConfigExists) {
59+
shell.exec(`rm ${path.join(process.cwd(), 'now.json')}`);
60+
}
61+
62+
shell.exec(
63+
`cd ${path.join(
64+
process.cwd(),
65+
config.wwwDir,
66+
)} && now deploy --meta gitSha="${gitSha}" --scope=boltdesignsystem`,
67+
);
68+
} else {
69+
deployedUrl = shell.exec(
70+
`now deploy --meta gitSha="${gitSha}" --token=${NOW_TOKEN}`,
71+
).stdout;
72+
}
73+
74+
if (TRAVIS) {
75+
deployedUrlPretty = deployedUrl.trim();
6476

65-
// await handleNowDeploy(child);
77+
await setCheckRun({
78+
status: 'completed',
79+
name: 'Deploy - now.sh (basic)',
80+
conclusion: 'success',
81+
output: {
82+
title: 'Now.sh Basic Deploy',
83+
summary: `
84+
- ${deployedUrlPretty}
85+
`.trim(),
86+
},
87+
});
88+
89+
await handleNowDeploy(child);
90+
}
6691
} catch (error) {
92+
console.log(error);
6793
console.error('Error deploying:');
6894
console.log(error.stdout, error.stderr);
6995

70-
await setCheckRun({
71-
status: 'completed',
72-
name: 'Deploy - now.sh (basic)',
73-
conclusion: 'failure',
74-
output: {
75-
title: 'Now.sh Deploy failure',
76-
summary: `
77-
${error.stdout}
78-
${error.stderr}
79-
`.trim(),
80-
},
81-
});
96+
if (TRAVIS) {
97+
await setCheckRun({
98+
status: 'completed',
99+
name: 'Deploy - now.sh (basic)',
100+
conclusion: 'failure',
101+
output: {
102+
title: 'Now.sh Deploy failure',
103+
summary: `
104+
${error.stdout}
105+
${error.stderr}
106+
`.trim(),
107+
},
108+
});
109+
}
82110
process.exit(1);
83111
}
84112
} catch (error) {

scripts/utils/handle-now-aliases.js

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
#!/usr/bin/env node
22
const shell = require('shelljs');
33
const { TRAVIS } = require('./travis-vars');
4-
const { setCheckRun } = require('../check-run');
54
const { normalizeUrlAlias } = require('./normalize-url-alias');
65

7-
let { NOW_TOKEN } = process.env;
8-
const baseNowArgs = ['--platform-version=1', '--team=boltdesignsystem'];
6+
let setCheckRun, outputBanner;
97

10-
if (NOW_TOKEN) baseNowArgs.push(`--token=${NOW_TOKEN}`);
8+
let { NOW_TOKEN } = process.env;
119

1210
async function aliasNowUrl(originalUrl, prefix) {
1311
console.log(
1412
'Creating now.sh alias off of the ' + originalUrl + ' deployment URL.',
1513
);
1614

15+
if (TRAVIS) {
16+
setCheckRun = require('../check-run').setCheckRun;
17+
outputBanner = require('ci-utils').outputBanner;
18+
19+
await setCheckRun({
20+
name: 'Deploy - now.sh (basic)',
21+
status: 'in_progress',
22+
});
23+
outputBanner('Starting deploy...');
24+
}
25+
1726
const deployedUrl = originalUrl.trim();
1827

1928
let aliasedUrl;
@@ -26,50 +35,56 @@ async function aliasNowUrl(originalUrl, prefix) {
2635

2736
console.log(`Attempting to alias ${originalUrl} to ${aliasedUrl}...`);
2837

29-
await setCheckRun({
30-
status: 'in_progress',
31-
name: 'Deploy - now.sh (alias)',
32-
});
38+
if (TRAVIS) {
39+
await setCheckRun({
40+
status: 'in_progress',
41+
name: 'Deploy - now.sh (alias)',
42+
});
43+
}
3344

3445
const aliasOutput = shell.exec(
35-
`npx now alias ${deployedUrl} ${aliasedUrl} --platform-version=1 --team=boltdesignsystem --token=${NOW_TOKEN}`,
46+
`npx now alias ${deployedUrl} ${aliasedUrl} --team=boltdesignsystem --token=${NOW_TOKEN}`,
3647
);
3748

3849
if (aliasOutput.code !== 0) {
3950
console.log('Error aliasing:');
4051
console.log(aliasOutput.stdout, aliasOutput.stderr);
4152

42-
await setCheckRun({
43-
status: 'completed',
44-
name: 'Deploy - now.sh (alias)',
45-
conclusion: 'failure',
46-
output: {
47-
title: 'Now.sh Deploy failure',
48-
summary: `
49-
${aliasOutput.stdout}
50-
${aliasOutput.stderr}
51-
`.trim(),
52-
},
53-
});
53+
if (TRAVIS) {
54+
await setCheckRun({
55+
status: 'completed',
56+
name: 'Deploy - now.sh (alias)',
57+
conclusion: 'failure',
58+
output: {
59+
title: 'Now.sh Deploy failure',
60+
summary: `
61+
${aliasOutput.stdout}
62+
${aliasOutput.stderr}
63+
`.trim(),
64+
},
65+
});
66+
}
5467

5568
process.exit(1);
5669
} else {
5770
// console.log('Success Aliasing!');
5871
console.log(aliasOutput.stdout);
5972
// console.log(deployedUrl);
6073
// console.log(aliasedUrl);
61-
await setCheckRun({
62-
status: 'completed',
63-
name: 'Deploy - now.sh (alias)',
64-
conclusion: 'success',
65-
output: {
66-
title: 'Now.sh Deploy',
67-
summary: `
68-
- ${deployedUrl}
69-
- ${aliasedUrl}
70-
`.trim(),
71-
},
72-
});
74+
if (TRAVIS) {
75+
await setCheckRun({
76+
status: 'completed',
77+
name: 'Deploy - now.sh (alias)',
78+
conclusion: 'success',
79+
output: {
80+
title: 'Now.sh Deploy',
81+
summary: `
82+
- ${deployedUrl}
83+
- ${aliasedUrl}
84+
`.trim(),
85+
},
86+
});
87+
}
7388
}
7489
}
7590

0 commit comments

Comments
 (0)