Skip to content

Commit 76eff20

Browse files
build(firebase): reconfigure site to be hosted by firebase, fixes GoogleChrome#2722, fixes GoogleChrome#4382, fixes GoogleChrome#3913, fixes GoogleChrome#2687 (GoogleChrome#4381)
* build(firebase): reconfigure site to be hosted by firebase * refactor: make tweaks based on feedback * build(deploy): use node 12 * Update src/build/output-permalink.js Co-authored-by: Rob Dodson <[email protected]> * Update src/build/output-permalink.js Co-authored-by: Rob Dodson <[email protected]> * build(firebase): add redirects.yaml and generate firebase.json on the fly Co-authored-by: Rob Dodson <[email protected]>
1 parent cd36c94 commit 76eff20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3605
-2036
lines changed

.cloudbuild/deploy.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
steps:
2+
- name: node:12
3+
entrypoint: npm
4+
args: ['ci']
5+
- name: node:12
6+
entrypoint: npm
7+
args: ['run', 'cloud-secrets']
8+
env:
9+
- 'PROJECT_ID=$PROJECT_ID'
10+
- name: node:12
11+
entrypoint: npm
12+
args: ['run', 'build']
13+
env:
14+
- 'ELEVENTY_ENV=prod'
15+
- name: node:12
16+
entrypoint: npm
17+
args: ['run', 'firebase-config']
18+
- name: 'gcr.io/$PROJECT_ID/firebase'
19+
args: ['deploy']
20+
- name: node:12
21+
entrypoint: npm
22+
args: ['run', 'algolia']
23+
timeout: 1800s

.firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "web-dev-production-1"
4+
}
5+
}

.gcloudignore

-35
This file was deleted.

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ src/site/content/en/authors/ @GoogleChrome/web-dev-eng
3939
src/site/_data/authorsData.json @GoogleChrome/web-dev-content @GoogleChrome/web-dev-approvers
4040
src/site/_data/tagsData.json @GoogleChrome/web-dev-content @GoogleChrome/web-dev-approvers
4141
src/site/_data/paths/*.json @GoogleChrome/web-dev-content @GoogleChrome/web-dev-approvers
42-
src/site/content/_redirects.yaml @GoogleChrome/web-dev-content @GoogleChrome/web-dev-approvers
42+
redirects.yaml @GoogleChrome/web-dev-content @GoogleChrome/web-dev-approvers

.github/workflows/cleanup-versions-workflow.yml

-44
This file was deleted.

.github/workflows/continuous-integration-workflow.yml

-68
This file was deleted.

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,5 @@ entrypoint.hashmanifest.json
8383

8484
# Temporary files
8585
.tmp
86+
.firebase
87+
firebase.json

index-algolia.js renamed to algolia.js

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const primaryIndexName = 'webdev';
2929
const deployIndexName = `webdev_deploy_${revision}`;
3030

3131
async function index() {
32+
if (!process.env.ALGOLIA_APP || !process.env.ALGOLIA_KEY) {
33+
console.warn('Missing Algolia environment variables, skipping indexing.');
34+
return;
35+
}
36+
3237
const client = algoliasearch(
3338
process.env.ALGOLIA_APP,
3439
process.env.ALGOLIA_KEY,

app.yaml

-20
This file was deleted.

cloud-secrets.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @fileoverview This file generates a `.env` file from the lastest active secrets stored
3+
* in the Google Cloud Secret Manager. This is ran from the Cloud Build Deploy script.
4+
*/
5+
6+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
7+
8+
const client = new SecretManagerServiceClient();
9+
10+
const cloudSecrets = async () => {
11+
if (!process.env.PROJECT_ID) {
12+
return console.warn(
13+
'No Google Cloud Project ID found, no .env file is being generated.',
14+
);
15+
}
16+
17+
console.log('Generating .env file.');
18+
19+
const project = `projects/${process.env.PROJECT_ID}`;
20+
let dotenv = '';
21+
const fetchedSecrets = [];
22+
const [secretsList] = await client.listSecrets({parent: project});
23+
24+
for (const secretItem of secretsList) {
25+
const key = secretItem.name.split('/').pop();
26+
const [versions] = await client.listSecretVersions({
27+
parent: secretItem.name,
28+
});
29+
const version = versions.find((v) => v.state === 'ENABLED');
30+
31+
if (version) {
32+
const [accessedSecret] = await client.accessSecretVersion({
33+
name: version.name,
34+
});
35+
const value = accessedSecret.payload.data.toString();
36+
dotenv += `${key}=${value}\n`;
37+
fetchedSecrets.push(key);
38+
}
39+
}
40+
41+
require('fs').writeFileSync('.env', dotenv);
42+
43+
console.log(
44+
`The following environment variables have been added to the generated .env file: ${fetchedSecrets.join(
45+
', ',
46+
)}`,
47+
);
48+
};
49+
50+
cloudSecrets().catch((e) => {
51+
console.warn('Ooops, there was an error in generating the .env file.', e);
52+
});

firebase-config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const yaml = require('js-yaml');
2+
const fs = require('fs');
3+
4+
const redirectsYaml = fs.readFileSync('./redirects.yaml', 'utf8');
5+
const {redirects: parsedRedirects} = yaml.safeLoad(redirectsYaml);
6+
7+
const firebaseJson = require('./firebase.incl.json');
8+
firebaseJson.hosting.redirects = parsedRedirects.reduce(
9+
(redirects, redirect) => {
10+
if (redirect.source && redirect.destination) {
11+
redirects.push({
12+
source: redirect.source,
13+
destination: redirect.destination,
14+
type: 301,
15+
});
16+
}
17+
return redirects;
18+
},
19+
[],
20+
);
21+
22+
fs.writeFileSync('./firebase.json', JSON.stringify(firebaseJson, null, 2));

firebase.incl.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"hosting": {
3+
"public": "dist",
4+
"headers": [
5+
{
6+
"source": "**",
7+
"headers": [
8+
{
9+
"key": "Cache-Control",
10+
"value": "public,max-age=31536000,immutable"
11+
}
12+
]
13+
}
14+
],
15+
"i18n": {
16+
"root": "/i18n"
17+
},
18+
"redirects": []
19+
},
20+
"emulators": {
21+
"hosting": {
22+
"port": 8080
23+
}
24+
}
25+
}

gulpfile.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const imagemin = require('gulp-imagemin');
2121
const rename = require('gulp-rename');
2222
const through2 = require('through2');
2323

24+
const {defaultLocale} = require('./src/site/_data/site');
25+
2426
/* eslint-disable max-len */
2527
const assetTypes =
2628
'jpg,jpeg,png,svg,gif,webp,webm,mp4,mov,ogg,wav,mp3,txt,yaml';
@@ -67,7 +69,12 @@ gulp.task('copy-content-assets', () => {
6769
// they belong to.
6870
.pipe(
6971
rename((assetPath) => {
72+
const defaultLocaleRegExp = new RegExp(`^${defaultLocale}/`);
7073
const parts = assetPath.dirname.split('/');
74+
assetPath.dirname = assetPath.dirname.replace(
75+
defaultLocaleRegExp,
76+
'',
77+
);
7178
// Landing pages should keep their assets.
7279
// e.g. en/vitals, en/about
7380
if (parts.length <= 2) {
@@ -86,11 +93,11 @@ gulp.task('copy-content-assets', () => {
8693
// Some assets are nested under directories which aren't part of
8794
// their url. For example, we have /en/blog/some-post/foo.jpg.
8895
// For these assets we need to remove the /blog/ directory so they
89-
// can live at /en/some-post/foo.jpg since that's what we'll actually
96+
// can live at /some-post/foo.jpg since that's what we'll actually
9097
// serve in production.
91-
// e.g. en/blog/foo/bar.jpg -> en/foo/bar.jpg
98+
// e.g. en/blog/foo/bar.jpg -> /foo/bar.jpg
9299
parts.splice(1, 1);
93-
assetPath.dirname = parts.join('/');
100+
assetPath.dirname = parts.join('/').replace(defaultLocaleRegExp, '');
94101
}),
95102
)
96103
.pipe(gulp.dest('./dist/'))

0 commit comments

Comments
 (0)