Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,28 @@ jobs:
- name: Upload to S3
run: |
aws s3 sync ./build s3://${{ vars.AWS_S3_BUCKET }}/junior/

configure-fastly:
# Runs after deploy so new origin content is uploaded before new routing
# can go live; also skips if the S3 upload failed.
needs: [setup, lint, build, deploy]
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
environment: ${{ needs.setup.outputs.environment }}
steps:
- uses: actions/checkout@v4
- name: Use Node 22x
uses: actions/setup-node@v4
with:
node-version: '22.x'
- name: Install Dependencies
run: npm ci --legacy-peer-deps
- name: Configure Fastly
run: npm run sync-fastly
env:
FASTLY_API_KEY: ${{ secrets.FASTLY_API_KEY }}
FASTLY_SERVICE_ID: ${{ vars.FASTLY_SERVICE_ID }}
# Set this var to "true" in an Environment to have that environment's
# deploys activate the new version; any other value configures and
# validates only.
FASTLY_ACTIVATE_CHANGES: ${{ vars.FASTLY_ACTIVATE_CHANGES }}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Use `^C` to stop the node process `npm start` starts.
| ------------------------ | ------- | ------------------------------------------------ |
| `FASTLY_SERVICE_ID` | `''` | Fastly service ID for `bin/configure-fastly.js` |
| `FASTLY_API_KEY` | `''` | Fastly API key for `bin/configure-fastly.js` |
| `FASTLY_ACTIVATE_CHANGES`| `false` | Activate changes and purge all after configuring |
| `FASTLY_ACTIVATE_CHANGES`| `false` | Set to `true` to activate changes and purge all after configuring |
| `AWS_ACCESS_KEY_ID` | `''` | AWS access key id for S3 |
| `AWS_SECRET_ACCESS_KEY` | `''` | AWS secret access key for S3 |
| `S3_BUCKET_NAME` | `''` | S3 bucket name to deploy into |
108 changes: 12 additions & 96 deletions bin/configure-fastly.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
const defaults = require('lodash.defaults');

const routeJson = require('../src/routes.json');
const {routesToSnippets} = require('./lib/routes-to-vcl');

const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';
const BUCKET_NAME_HEADER_NAME = 'Bucket name';

const fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, FASTLY_SERVICE_ID);

/*
* Translate an express-style pattern e.g. /path/:arg/ to a regex
* all :arguments become .+?
*/
const expressPatternToRegex = pattern => pattern.replace(/(:[^/]+)/gi, '.+?');

const getConditionNameForRoute = (route, type) => `routes/${route.pattern} (${type})`;

const getHeaderNameForRoute = route => {
if (route.name) return `rewrites/${route.name}`;
if (route.redirect) return `redirects/${route.pattern}`;
};

const getResponseNameForRoute = route => `redirects/${route.pattern}`;

const routes = routeJson.map(route => defaults({}, {pattern: expressPatternToRegex(route.pattern)}, route));

// Get the latest version, cloning it first if it is already active or locked.
const getWorkingVersion = async () => {
const response = await fastly.getLatestVersion();
Expand All @@ -40,89 +20,25 @@ const getWorkingVersion = async () => {
return response.number;
};

const setBucketNameHeader = version => fastly.setFastlyHeader(version, {
name: BUCKET_NAME_HEADER_NAME,
action: 'set',
ignore_if_set: 0,
type: 'REQUEST',
dst: 'http.host',
src: `"${S3_BUCKET_NAME}"`,
priority: 1
});

// Create a request condition per route and return them keyed by route index.
const setAppRouteRequestConditions = async version => {
const conditions = [];
await Promise.all(routes.map(async (route, id) => {
conditions[id] = await fastly.setCondition(version, {
name: getConditionNameForRoute(route, 'request'),
statement: `req.url ~ "${route.pattern}"`,
type: 'REQUEST',
// Priority needs to be > 1 to not interact with http->https redirect
priority: 10 + id
});
}));
return conditions;
};

const setRedirectRouteHeader = async (version, route, id) => {
const responseCondition = await fastly.setCondition(version, {
name: getConditionNameForRoute(route, 'response'),
statement: `req.url ~ "${route.pattern}"`,
type: 'RESPONSE',
priority: id
});
await fastly.setResponseObject(version, {
name: getResponseNameForRoute(route),
status: 301,
response: 'Moved Permanently',
request_condition: getConditionNameForRoute(route, 'request')
});
return fastly.setFastlyHeader(version, {
name: getHeaderNameForRoute(route),
action: 'set',
ignore_if_set: 0,
type: 'RESPONSE',
dst: 'http.Location',
src: `"${route.redirect}"`,
response_condition: responseCondition.name
});
};

const setRewriteRouteHeader = (version, route, requestCondition) => fastly.setFastlyHeader(version, {
name: getHeaderNameForRoute(route, 'request'),
action: 'set',
ignore_if_set: 0,
type: 'REQUEST',
dst: 'url',
src: `"/${route.name}.html"`,
request_condition: requestCondition.name,
priority: 10
});

// Create the response/request header for every route (redirects vs. rewrites).
const setAppRouteHeaders = (version, requestConditions) => Promise.all(routes.map((route, id) => {
if (route.redirect) {
return setRedirectRouteHeader(version, route, id);
}
return setRewriteRouteHeader(version, route, requestConditions[id]);
}));
// Render routes.json into VCL snippets and write them to the version.
const setAppRouteSnippets = version => Promise.all(
routesToSnippets(routeJson).map(snippet => fastly.setSnippet(version, snippet))
);

const configureFastly = async () => {
const version = await getWorkingVersion();
// The bucket header and the request conditions depend only on the version,
// so run them together; the route headers depend on the request conditions.
const results = await Promise.all([
setBucketNameHeader(version),
setAppRouteRequestConditions(version)
]);
await setAppRouteHeaders(version, results[1]);
await setAppRouteSnippets(version);
// Compile-check the generated VCL before anything tries to activate it.
const validation = await fastly.validateVersion(version);
if (validation.status !== 'ok') {
throw new Error(`Version ${version} failed validation: ${validation.msg}`);
}
return version;
};

configureFastly()
.then(async version => {
if (!process.env.FASTLY_ACTIVATE_CHANGES) return;
if (process.env.FASTLY_ACTIVATE_CHANGES !== 'true') return;
const response = await fastly.activateVersion(version);
process.stdout.write(`Successfully configured and activated version ${response.number}\n`);
await fastly.purgeAll(FASTLY_SERVICE_ID);
Expand Down
68 changes: 24 additions & 44 deletions bin/lib/fastly-extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const Fastly = require('fastly');
/*
* Fastly configuration helpers built on the official fastly-js client.
*
* Wraps the per-resource API classes and exposes Promise-returning, upsert-by-
* name helpers with stable signatures so callers don't deal with the client's
* create-vs-update split. Authenticates the shared ApiClient on construction.
* Wraps the per-resource API classes and exposes Promise-returning helpers with
* stable signatures. Authenticates the shared ApiClient on construction.
*
* @param {string} apiToken Fastly API token
* @param {string} serviceId Fastly service id
Expand All @@ -14,17 +13,13 @@ module.exports = (apiToken, serviceId) => {
Fastly.ApiClient.instance.authenticate(apiToken);

const versionApi = new Fastly.VersionApi();
const conditionApi = new Fastly.ConditionApi();
const headerApi = new Fastly.HeaderApi();
const responseObjectApi = new Fastly.ResponseObjectApi();
const snippetApi = new Fastly.SnippetApi();
const purgeApi = new Fastly.PurgeApi();

// Upsert-by-name: fastly-js has no upsert, so update (PUT by name) and fall
// back to create (POST) when the resource does not yet exist (404).
const upsert = (update, create) => update().catch(err => {
if (err && err.status === 404) return create();
const ignoreMissing = err => {
if (err && err.status === 404) return null;
throw err;
});
};

const withService = (version, extra) => Object.assign(
{service_id: serviceId, version_id: version},
Expand Down Expand Up @@ -55,45 +50,30 @@ module.exports = (apiToken, serviceId) => {
return versionApi.cloneServiceVersion({service_id: serviceId, version_id: version});
},

// Upsert a Fastly condition entry.
setCondition: (version, condition) => {
if (!serviceId) {
return Promise.reject(new Error('Failed to set condition. No serviceId configured'));
}
const params = withService(version, condition);
return upsert(
() => conditionApi.updateCondition(Object.assign({condition_name: condition.name}, params)),
() => conditionApi.createCondition(params)
);
},

// Upsert a Fastly header entry.
setFastlyHeader: (version, header) => {
// Compile-check a version's generated VCL without activating it. Resolves
// with {status, msg}; status is 'ok' when the version is valid.
validateVersion: version => {
if (!serviceId) {
return Promise.reject(new Error('Failed to set header. No serviceId configured'));
return Promise.reject(new Error('Failed to validate version. No serviceId configured.'));
}
const params = withService(version, header);
return upsert(
() => headerApi.updateHeaderObject(Object.assign({header_name: header.name}, params)),
() => headerApi.createHeaderObject(params)
);
return versionApi.validateServiceVersion({service_id: serviceId, version_id: version});
},

// Upsert a Fastly response object. The client takes the body wrapped in
// create_response_object_request for both create and update.
setResponseObject: (version, responseObject) => {
// Replace a versioned VCL snippet. fastly-js updateSnippet sends no body,
// so delete any existing snippet of this name (ignoring 404) then create.
setSnippet: (version, snippet) => {
if (!serviceId) {
return Promise.reject(new Error('Failed to set response object. No serviceId configured'));
return Promise.reject(new Error('Failed to set snippet. No serviceId configured'));
}
return upsert(
() => responseObjectApi.updateResponseObject(withService(version, {
response_object_name: responseObject.name,
create_response_object_request: responseObject
})),
() => responseObjectApi.createResponseObject(withService(version, {
create_response_object_request: responseObject
}))
);
return snippetApi.deleteSnippet({service_id: serviceId, version_id: version, name: snippet.name})
.catch(ignoreMissing)
.then(() => snippetApi.createSnippet(withService(version, {
name: snippet.name,
type: snippet.type,
content: snippet.content,
priority: snippet.priority,
dynamic: '0'
})));
},

// Activate a version.
Expand Down
Loading
Loading