Skip to content

Commit

Permalink
static github action (#6519)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkettner authored Nov 14, 2022
1 parent 7f5ad13 commit a29136e
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 65 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/release-static-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
env:
APP_ENV: production
AMP_DOC_TOKEN: ${{ secrets.AMP_DOC_TOKEN }}
AMP_DEV_PIXI_APIS_KEY: ${{ secrets.AMP_DEV_PIXI_APIS_KEY }}
run: |
npx gulp buildPrepare
Expand Down Expand Up @@ -167,7 +166,7 @@ jobs:
environment: Production
env:
APP_ENV: production
CLOUDSDK_CORE_DISABLE_PROMPTS: 1
NETLIFY_DEPLOY_TOKEN: ${{ secrets.NETLIFY_DEPLOY_TOKEN }}
steps:
- name: Cloning repository
uses: actions/checkout@v2
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/test-pixi.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
---
name: 'Test: Pixi'

on:
push:
paths:
- 'package.json'
- 'pixi/**'

jobs:
test:
runs-on: ubuntu-latest
Expand Down
10 changes: 1 addition & 9 deletions gulpfile.js/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ function nunjucksEnv() {
}

function optimizeFiles(cb) {
const logger = require('@lib/utils/log')('AMP Optimizer');
return gulp
.src(`${project.paths.PAGES_DEST}/**/*.html`)
.pipe(
Expand All @@ -530,14 +529,7 @@ function optimizeFiles(cb) {
});
})
)
.pipe(
gulp.dest((f) => {
logger.log(
`staticified ${path.relative(project.absolute('.'), f.path)}`
);
return f.base;
})
)
.pipe(gulp.dest((f) => f.base))
.on('end', cb);
}

Expand Down
33 changes: 29 additions & 4 deletions gulpfile.js/deploy-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,37 @@
'use strict';

const {sh} = require('@lib/utils/sh.js');
const {GROW_BUILD_DEST} = require('@lib/utils/project').paths;
const {DIST, PAGES_DEST} = require('@lib/utils/project').paths;
const {NETLIFY_DEPLOY_TOKEN} = process.env;
const SITES = [
{
NAME: 'amp.dev',
ID: 'e571c70e-d23f-4cbf-ac4e-802bb08e5261',
DIR: PAGES_DEST,
},
{
NAME: 'playground.amp.dev',
ID: 'acead270-9404-4dde-81e4-aec0e6884869',
DIR: `${DIST}/playground`,
},
{
NAME: 'preview.amp.dev',
ID: 'caf28d42-024a-4efb-b266-b00cf10847a3',
DIR: `${DIST}/examples`,
},
];

async function staticDeploy() {
return sh('npx netlify deploy --prod --site amp-dev-static', {
workingDir: GROW_BUILD_DEST,
});
for (const SITE of SITES) {
console.log(`attempting to deploy ${SITE.DIR}`);

await sh(
`npx netlify deploy --prod --auth ${NETLIFY_DEPLOY_TOKEN} --site ${SITE.ID}`,
{
workingDir: SITE.DIR,
}
);
}
}

exports.staticDeploy = staticDeploy;
81 changes: 43 additions & 38 deletions gulpfile.js/staticify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
const Cheerio = require('cheerio');
const Vinyl = require('vinyl');
const config = require('@lib/config');
const filter = require('gulp-filter');
const gulp = require('gulp');
const nunjucks = require('nunjucks');
const path = require('path');
const through = require('through2');
const {project} = require('@lib/utils');
const {survey} = require('@lib/templates/SurveyFilter.js');
Expand All @@ -37,18 +35,20 @@ const {
FORMAT_WEBSITES,
SUPPORTED_FORMATS,
} = require('@lib/amp/formatHelper.js');
const coursesPath = '/documentation/courses';
const coursesRegex = new RegExp(`^(.+)(?:${coursesPath})(.*)$`);

const getUpdatedURL = (u, requestedFormat, forcedFormat) => {
return u.replace(
/(.*documentation\/[^/]+)[\/.]([^?]+)(?:\?(?:[^=]*)=(.*))?/,
(match, section, page) => {
/(.*documentation\/[^/]+)[\/.]([^?]+)?(?:\?(?:[^=]*)=(.*))?/,
(match, section, page, embeddedQueryFormat) => {
if (page === 'html') {
page = 'index.html';
}

const hasFormat = forcedFormat || requestedFormat;
const hasFormat = forcedFormat || embeddedQueryFormat || requestedFormat;
const fmt = hasFormat ? `${hasFormat}/` : '';
return `${section}/${fmt}${page}`;
return `${section}/${fmt}${page || ''}`;
}
);
};
Expand Down Expand Up @@ -98,13 +98,8 @@ async function staticify(done) {
const f = (cb) => {
const env = nunjucksEnv();

const f = filter([`${project.paths.PAGES_DEST}/**/*html`, 'index'], {
restore: true,
});

return gulp
.src(`${project.paths.PAGES_DEST}/**/*.html`)
.pipe(f)
.src(`${project.paths.PAGES_DEST}/**/*html`)
.pipe(
// Render a static version of all of our pages

Expand Down Expand Up @@ -133,29 +128,33 @@ async function staticify(done) {
// Rewrite links inside of each of the pages

const $ = Cheerio.load(file.contents.toString());
const $links = $('a.nav-link, a.ap-m-format-toggle-link');

$('a.nav-link, a.ap-m-format-toggle-link').each(function () {
$links.each(function () {
// eslint-disable-next-line no-invalid-this
const $this = $(this);
const origURL = $this.attr('href');
const updatedURL = getUpdatedURL(origURL, format);

$this.attr('href', getUpdatedURL(origURL, format));
$this.attr('href', updatedURL);
});

const renderedPage = $.root().html();

if (
format === FORMAT_WEBSITES &&
!file.path.endsWith('tools.html')
) {
// covering the case where /documentation/tools.html, which matches differently than all other paths
const defaultFormatVersion = new Vinyl({
path: getUpdatedURL(file.path),
contents: Buffer.from(renderedPage),
});
// we need to render a "default" version for the URLs, and treat the "website" version as such
if (format === FORMAT_WEBSITES) {
const {path} = file;
let contents = Buffer.from(renderedPage);

if (file.path.endsWith('tools.html')) {
// In addition to the tools pages for each format, we need to render a seperate main version that
// shows all of them. Rather than render its twice, we can just toggle a classname.
$('.ap-a-pill').addClass('active');
contents = Buffer.from($.root().html());
}

// eslint-disable-next-line no-invalid-this
this.push(defaultFormatVersion);
this.push(new Vinyl({path, contents}));
}

file.path = getUpdatedURL(file.path, format);
Expand All @@ -164,14 +163,7 @@ async function staticify(done) {
callback(null, file);
})
)
.pipe(
gulp.dest((f) => {
logger.log(
`staticified ${path.relative(project.absolute('.'), f.path)}`
);
return f.base;
})
)
.pipe(gulp.dest((file) => file.base))
.on('end', cb);
};

Expand All @@ -183,9 +175,6 @@ async function staticify(done) {
});

const generatedLevels = ['beginner', 'advanced'].map((level) => {
const coursesPath = '/documentation/courses';
const coursesRegex = new RegExp(`^(.+)(?:${coursesPath})(.*)$`);

const f = (cb) => {
const env = nunjucksEnv();

Expand All @@ -198,8 +187,11 @@ async function staticify(done) {
requestPath: `${file.path.replace(requestPathRegex, '')}/`,
level,
format: FORMAT_WEBSITES,
requestedFormat: FORMAT_WEBSITES,
};

logger.log(`trying to rendering ${file.path}`);

env.renderString(srcHTML, configObj, (err, result) => {
if (err) {
return callback(err);
Expand All @@ -215,8 +207,10 @@ async function staticify(done) {
let renderedPage = file.contents.toString();

const $ = Cheerio.load(renderedPage);
const $links = $('.nav-link');
const $levelToggle = $('.toggle-button input');

$('.nav-link').each(function () {
$links.each(function () {
// eslint-disable-next-line no-invalid-this
const $this = $(this);
const origURL = $this.attr('href');
Expand All @@ -228,7 +222,18 @@ async function staticify(done) {
$this.attr('href', updatedURL);
});

renderedPage = $.root().html();
const originalToggleLink = $levelToggle.attr('on');

if (originalToggleLink) {
const updatedToggleLink = originalToggleLink.replace(
/(^[^\/]+\/[^\/]+\/[^\/]+\/)([^?]+)+\?level=([^']+)(.+)$/,
(match, prefix, coursePath, level, suffix) =>
`${prefix}${level}/${coursePath}${suffix}`
);
$levelToggle.attr('on', updatedToggleLink);

renderedPage = $.root().html();
}

if (level === 'beginner') {
// eslint-disable-next-line no-invalid-this
Expand Down Expand Up @@ -300,8 +305,8 @@ async function staticify(done) {

return new Promise((resolve, reject) => {
gulp.series(
gulp.parallel(...generatedFormats),
gulp.parallel(...generatedLevels),
gulp.parallel(...generatedFormats),
(seriesDone) => {
seriesDone();
resolve();
Expand Down
1 change: 1 addition & 0 deletions netlify/configs/playground.amp.dev/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
base = "."
# Directory to change to before starting a build.
# This is where we will look for package.json/.nvmrc/etc.
publish = "."
[[headers]]
for = "/*"

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"gcp-metadata": "4.3.1",
"google-spreadsheet": "3.1.15",
"gulp-file": "0.4.0",
"gulp-filter": "7.0.0",
"helmet-csp": "3.4.0",
"http-proxy": "1.18.1",
"iltorb": "2.4.5",
Expand Down
5 changes: 2 additions & 3 deletions pages/content/amp-dev/documentation/tools.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
<ul class="ap-o-tool-format-filter none" [class]="activeFilter.chosenFilter != 'none' ? 'ap-o-tool-format-filter filtered' : 'ap-o-tool-format-filter none' ">
{% for link in doc.filters %}
<li>
<button class="ap-a-pill {{link|slug}} [% if requestedFormat == '{{link}}'|lower or not requestedFormat%]active[% endif %]"
on="tap:AMP.navigateTo(url='{{ g.doc('/content/amp-dev/documentation/tools.html', locale=doc.locale).url.path }}/{{link|slug}}')">
<a href="{{ g.doc('/content/amp-dev/documentation/tools.html', locale=doc.locale).url.path }}/{{link|slug}}" class="ap-a-pill {{link|slug}} [% if requestedFormat == '{{link}}'|lower or not requestedFormat%]active[% endif %]" >
<div class="ap-a-pill-icon ap-a-ico {{link|lower}}">
<svg><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#amp-{{link|lower}}"></use></svg>
</div>
Expand All @@ -72,7 +71,7 @@
on="tap:AMP.navigateTo(url='{{ g.doc('/content/amp-dev/documentation/tools.html', locale=doc.locale).url.path }}')">
<svg><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#close"></use></svg>
</div>
</button>
</a>
</li>
{% endfor %}
</ul>
Expand Down
2 changes: 0 additions & 2 deletions platform/lib/utils/ampOptimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
* limitations under the License.
*/

const imageOptimizer = require('@lib/utils/imageOptimizer');
const HeadDedupTransformer = require('@lib/utils/HeadDedupTransformer');
const AmpOptimizer = require('@ampproject/toolbox-optimizer');
const CssTransformer = require('@lib/utils/cssTransformer');
const signale = require('signale');

const optimizerConfig = {
imageBasePath: 'pages',
imageOptimizer,
autoExtensionImport: true,
extensionVersions: {
'amp-base-carousel': '0.1',
Expand Down
1 change: 1 addition & 0 deletions playground/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
base = "."
# Directory to change to before starting a build.
# This is where we will look for package.json/.nvmrc/etc.
publish = "."
[[headers]]
for = "/*"

Expand Down

0 comments on commit a29136e

Please sign in to comment.